weixin_43691257
2019-11-29 17:58
采纳率: 100%
浏览 946

WPF中Graphics g = this.CreateGraphics();报错

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;
using System.Windows.Threading;
using System.Drawing;   
using System.Drawing.Imaging;


namespace _2_3
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private DispatcherTimer dispatcherTimer = null;
        int hh, mm, ss;
        public MainWindow()
        {
            InitializeComponent();
            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(OnTimedEvent);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
            dispatcherTimer.Start();
        }
        private void OnTimedEvent(object sender, EventArgs e)
        {
            timelabel.Content = DateTime.Now.ToLongTimeString();
            hh = DateTime.Now.Hour;
            mm = DateTime.Now.Minute;
            ss = DateTime.Now.Second;
            MyDrawClock(hh, mm, ss);
        }

        private void MyDrawClock(int h, int m, int s)
        {
            Graphics g = this.CreateGraphics();
            g.Clear(System.Drawing.Color.White);

            Font font = new Font("黑体", 20, System.Drawing.FontStyle.Bold);
            System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black,1);
            SolidBrush Brush = new SolidBrush(System.Drawing.Color.Black);

            g.DrawString("12", font, Brush, 40, 60);
            g.DrawString("9", font, Brush, 20, 45);
            g.DrawString("3", font, Brush, 60, 45);
            g.DrawString("6", font, Brush, 40, 30);


            System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Black,1);

            System.Drawing.Point centerPoint = new System.Drawing.Point();//表的中心点
            //计算出秒针,时针,分针的另外一个点
            System.Drawing.Point secPoint = new System.Drawing.Point((int)(centerPoint.X + (Math.Sin(s * Math.PI / 30) * 140)), (int)(centerPoint.Y - (Math.Cos(s * Math.PI / 30) * 140)));
            System.Drawing.Point minPoint = new System.Drawing.Point((int)(centerPoint.X + (Math.Sin(m * Math.PI / 30) * 130)), (int)(centerPoint.Y - (Math.Cos(m * Math.PI / 30) * 130)));
            System.Drawing.Point hourPoint = new System.Drawing.Point((int)(centerPoint.X + (Math.Sin(h * Math.PI / 6) * 120) - m * Math.PI / 360), (int)(centerPoint.Y - (Math.Cos(h * Math.PI / 6) * 120) - m * Math.PI / 360));

            //以不同颜色和宽度绘制表针
            myPen = new System.Drawing.Pen(System.Drawing.Color.Red,1);
            g.DrawLine(myPen, centerPoint, secPoint);
            myPen = new System.Drawing.Pen(System.Drawing.Color.Green,2);
            g.DrawLine(myPen, centerPoint, minPoint);
            myPen = new System.Drawing.Pen(System.Drawing.Color.Orange, 1);
            g.DrawLine(myPen, centerPoint, hourPoint);

        }


    }
}

报错信息:错误 1 “_2_3.MainWindow”不包含“CreateGraphics”的定义,并且找不到可接受类型为“_2_3.MainWindow”的第一个参数的扩展方法“CreateGraphics”(是否缺少 using 指令或程序集引用

折腾了半天也没搞好,要崩溃了

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • hxycsdn9159 2019-12-09 14:19
    已采纳

    wpf用的是DirectX进行底层渲染根本没有用GDI那一套,你好好理解一下你代码里面的this.CreateGraphics()中的this指的是谁再说

    点赞 打赏 评论
  • blownewbee 2019-11-30 00:18

    System.Forms.Form包含CreateGraphics
    System.Window不包含
    你的采纳率太低了,之前的都采纳好了才能继续回答你的问题,否则以后不能回答了

    点赞 打赏 评论

相关推荐 更多相似问题