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

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指的是谁再说

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)