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 指令或程序集引用
折腾了半天也没搞好,要崩溃了