怎样子画出一个闹钟的模型?
想问一下就是怎样子在form中设计出一个闹钟的模型,就是用画布,但是不是只有代码的那种哦。拜托哦程序代码:
private void OnFormPaint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; Pen myPen = new Pen(Color.Blue, 1); //创建一个自定义画笔对象 //创建一个正方形rect int myRect_X = this.ClientRectangle.Right / 6; int myRect_Y = this.ClientRectangle.Bottom / 6; int myRect_Width; int myRect_Height; if (this.ClientRectangle.Height < this.ClientRectangle.Width) { myRect_Width = this.ClientRectangle.Height *2/ 3; myRect_Height = this.ClientRectangle.Height*2 / 3; } else { myRect_Width = this.ClientRectangle.Width / 2; myRect_Height = this.ClientRectangle.Width / 2; } Rectangle rect = new Rectangle(myRect_X, myRect_Y, myRect_Width, myRect_Height); g.DrawEllipse(myPen, rect); //画出一个内切于矩形rect的圆 g.TranslateTransform(ClientSize.Width / 2, ClientSize.Height / 2, MatrixOrder.Append); DrawClock(e.Graphics); } private void DrawClock(Graphics g) { const double pai = Math.PI; Point center = new Point(0, 0); DateTime time = DateTime.Now; //读取时间; double secAng = 2.0 * pai * time.Second / 60.0; double minAng = 2.0 * pai * (time.Minute + time.Second / 60.0) / 60.0; double hourAng = 2.0 * pai * (time.Hour + time.Minute / 60.0) / 12.0; //各指针单位换算; int r = Math.Min(ClientSize.Width, ClientSize.Height) / 2; int secHandLength = (int)(0.6 * r); int minHandLength = (int)(0.4 * r); int hourHandLength = (int)(0.2 * r); //指针的长度定义; Point secHand = new Point((int)(secHandLength * Math.Sin(secAng)), (int)(-secHandLength * Math.Cos(secAng))); Point minHand = new Point((int)(minHandLength * Math.Sin(minAng)), (int)(-minHandLength * Math.Cos(minAng))); Point hourHand = new Point((int)(hourHandLength * Math.Sin(hourAng)), (int)(-hourHandLength * Math.Cos(hourAng))); //刷新指针; Pen SecPen = new Pen(Color.Red, 1); g.DrawLine(SecPen, center, secHand); Pen MinPen = new Pen(Color.RoyalBlue, 3); g.DrawLine(MinPen, center, minHand); Pen HourPen = new Pen(Color.CornflowerBlue, 5); g.DrawLine(HourPen, center, hourHand); //指针的样式定义; }