1. Form1窗体的属性 this.FormBorderStyle = FormBorderStyle.None;
2.添加事件
private void MyDrawClock(int h, int m, int s) { Graphics g = this.CreateGraphics(); Rectangle rect = this.ClientRectangle; this.FormBorderStyle = FormBorderStyle.None; g.Clear(Color.White); Pen myPen = new Pen(Color.Black, 1); //g.DrawEllipse(myPen, this.ClientRectangle.Width / 2 - 50, this.ClientRectangle.Height / 2 - 50, 100, 100);//画表盘 Point centerPoints = new Point(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);//表的中心点 for (int i = 0; i < 12; i++) { Point sp12 = new Point((int)(centerPoints.X + (Math.Sin(i*5 * Math.PI / 30) * 44)), (int)(centerPoints.Y - (Math.Cos(i*5 * Math.PI / 30) * 44))); Point sp121 = new Point((int)(sp12.X + (Math.Sin(i*5 * Math.PI / 30) * 5)), (int)(sp12.Y - (Math.Cos(i*5 * Math.PI / 30) * 5))); myPen = new Pen(Color.Gray, 1); if (i % 3 == 0) { myPen = new Pen(Color.Gray, 2); } g.DrawLine(myPen, sp12, sp121); } int hh = ((int)(m / 10)) + h * 5; Point centerPoint = new Point(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);//表的中心点 //计算出秒针,时针,分针的另外一个商点 //Point secPoint = new Point((int)(centerPoint.X + (Math.Sin(s * Math.PI / 30) * 50)), (int)(centerPoint.Y - (Math.Cos(s * Math.PI / 30) * 50))); Point minPoint = new Point((int)(centerPoint.X + (Math.Sin(m * Math.PI / 30) * 40)), (int)(centerPoint.Y - (Math.Cos(m * Math.PI / 30) * 40))); //Point hourPoint = new Point((int)(centerPoint.X + (Math.Sin(h * Math.PI / 6) * 30) - m * Math.PI / 360), (int)(centerPoint.Y - (Math.Cos(h * Math.PI / 6) * 30) - m * Math.PI / 360)); Point newhourPoint = new Point((int)(centerPoint.X + (Math.Sin(hh * Math.PI / 30) * 30)), (int)(centerPoint.Y - (Math.Cos(hh * Math.PI / 30) * 30))); //以不同颜色和宽度绘制表针 //myPen = new Pen(Color.Red, 1); //g.DrawLine(myPen, centerPoint, secPoint); myPen = new Pen(Color.Orange, 1); g.DrawLine(myPen, centerPoint, minPoint); myPen = new Pen(Color.Goldenrod, 2); g.DrawLine(myPen, centerPoint, newhourPoint); g.FillEllipse(new SolidBrush(Color.OrangeRed), (this.ClientRectangle.Width / 2) - 3, (this.ClientRectangle.Height / 2) - 3, 6, 6); } protected override void OnPaint(PaintEventArgs e) { MyDrawClock(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); this.TransparencyKey = Color.White; base.OnPaint(e); } private void timer1_Tick(object sender, EventArgs e) { MyDrawClock(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); }
3.添加拖动窗体事件
bool ismove = false; int _x; int _y; private void Form1_MouseDown(object sender, MouseEventArgs e) { _x = e.X; _y = e.Y; ismove = true; } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (ismove) { int dx = e.X - _x; int dy = e.Y - _y; this.Location = new Point(this.Left + dx, this.Top + dy); } } private void Form1_MouseUp(object sender, MouseEventArgs e) { ismove = false; }
4.效果图:
还可以改装一下
替换方法代码:
private void MyDrawClock(int h, int m, int s) { Graphics g = this.CreateGraphics(); Rectangle rect = this.ClientRectangle; g.Clear(Color.Green); g.FillRectangle(new SolidBrush(Color.White), 60, 40, 34, 40); g.DrawRectangle(new Pen(Color.Black), 60, 40, 34, 40); g.DrawString("闹钟.exe", new Font("宋体", 10, FontStyle.Regular), new SolidBrush(Color.White), 50, 86); Pen myPen = new Pen(Color.Black, 1); //g.DrawEllipse(myPen, this.ClientRectangle.Width / 2 - 50, this.ClientRectangle.Height / 2 - 50, 100, 100);//画表盘 Point centerPoints = new Point(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);//表的中心点 for (int i = 0; i < 12; i++) { Point sp12 = new Point((int)(centerPoints.X + (Math.Sin(i*5 * Math.PI / 30) * 12)), (int)(centerPoints.Y - (Math.Cos(i*5 * Math.PI / 30) * 12))); Point sp121 = new Point((int)(sp12.X + (Math.Sin(i*5 * Math.PI / 30) * 2)), (int)(sp12.Y - (Math.Cos(i*5 * Math.PI / 30) * 2))); myPen = new Pen(Color.Gray, 1); if (i % 3 == 0) { myPen = new Pen(Color.Black, 2); } g.DrawLine(myPen, sp12, sp121); } int hh = ((int)(m / 10)) + h * 5; Point centerPoint = new Point(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);//表的中心点 //计算出秒针,时针,分针的另外一个商点 //Point secPoint = new Point((int)(centerPoint.X + (Math.Sin(s * Math.PI / 30) * 50)), (int)(centerPoint.Y - (Math.Cos(s * Math.PI / 30) * 50))); Point minPoint = new Point((int)(centerPoint.X + (Math.Sin(m * Math.PI / 30) * 10)), (int)(centerPoint.Y - (Math.Cos(m * Math.PI / 30) * 10))); //Point hourPoint = new Point((int)(centerPoint.X + (Math.Sin(h * Math.PI / 6) * 30) - m * Math.PI / 360), (int)(centerPoint.Y - (Math.Cos(h * Math.PI / 6) * 30) - m * Math.PI / 360)); Point newhourPoint = new Point((int)(centerPoint.X + (Math.Sin(hh * Math.PI / 30) * 8)), (int)(centerPoint.Y - (Math.Cos(hh * Math.PI / 30) * 8))); //以不同颜色和宽度绘制表针 //myPen = new Pen(Color.Red, 1); //g.DrawLine(myPen, centerPoint, secPoint); myPen = new Pen(Color.Orange, 1); g.DrawLine(myPen, centerPoint, minPoint); myPen = new Pen(Color.Goldenrod, 2); g.DrawLine(myPen, centerPoint, newhourPoint); g.FillEllipse(new SolidBrush(Color.OrangeRed), (this.ClientRectangle.Width / 2) - 1, (this.ClientRectangle.Height / 2) - 1, 3, 3); }
效果图(模仿文件形式):