參考了好多資料,終於做了一個winform時鍾出來,效果圖如下:
1,首先在winfrom項目中添加了一個用戶控件ClockControl,實現過程可見代碼注釋,代碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace WinClock { public partial class ClockControl : UserControl { const int screenWidth = 200; //屏幕寬度 const int screenHeight = 200; //屏幕高度 public ClockControl() { InitializeComponent(); this.Width = screenWidth + 1; this.Height = screenHeight + 1; this.DoubleBuffered = true; //控件緩沖,避免閃爍 this.SetStyle(ControlStyles.ResizeRedraw, true); clockTimer.Start(); } private void clockTimer_Tick(object sender, EventArgs e) { Invalidate(); } protected override void OnPaint(PaintEventArgs e) { DateTime dtNow = DateTime.Now; string dayOfWeek = dtNow.ToString("dddd", new System.Globalization.CultureInfo("zh-cn"));//星期幾 Brush brush = new SolidBrush(Color.Black); //填充圖形 Pen pen = new Pen(Color.Black); //畫筆 Font hourFont = new Font("Arial", 10, FontStyle.Bold);//時鍾數字的字體 Font dateFont = new Font("Arial", 9); //日期的字體 int dialRadius = Math.Min(screenWidth, screenHeight) / 2; //圓的半徑 Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.HighQuality; //默認坐標系統原點是左上角,現在把原點移到屏幕中心, 右下左上對應的軸:x,y,-x,-y g.TranslateTransform(dialRadius, dialRadius); //畫時鍾最外層的圓線(pen,x,y,width,height) //圓的中心點坐標計算:(width/2+x,height/2+y),據此可得出要使圓在坐標原點(0,0)的x,y坐標值 g.DrawEllipse(pen, -screenWidth / 2, -dialRadius, screenWidth, screenHeight); GraphicsState state = g.Save(); //畫矩形、日期、星期幾 int rectWidth = 70; int rectHeight = 30; g.DrawRectangle(pen, -rectWidth / 2, rectHeight, rectWidth, rectHeight); g.DrawString(dtNow.ToString("yyyy-MM-dd"), dateFont, brush, -rectWidth / 2, rectHeight + 2); g.DrawString(dayOfWeek.PadLeft(8, ' '), dateFont, brush, -rectWidth / 2, rectHeight + 15); g.Restore(state); // 畫時鍾的60個圓點 //Save(),Restore(state)配合使用,使得平移、縮放、旋轉等操作只對它們作用域之間的代碼有效, //save開始到restore之間這繪畫,就像有繪制了一個圖層,restore之后將兩個圖層放到一起 state = g.Save(); for (int i = 0; i < 60; i++) { int w = i % 5 == 0 ? 5 : 3; g.FillEllipse(brush, 0, -dialRadius, w, w); //圍繞指定點按照順時針方向旋轉角度360 / 60 = 6度 g.RotateTransform(6); } g.Restore(state); //畫時鍾的12個數字,如果用上面RotateTransform方法則數字會傾斜、倒立,故不用 state = g.Save(); for (int i = 0; i < 12; i++) { //已知圓中心占坐標(x0,y0),半徑r,角度a0,則圓上任一點坐標(x,y)計算: //x = x0 + r * cos(ao * 3.14 /180) //y = y0 + r * sin(ao * 3.14 /180) Point point = new Point(-6, -6); //當為(0,0)時全部數字偏右下移,故手動調整 double dd = Math.PI / 180 * i * (360 /12); //每次轉360/12度 float x = point.X + (float)((dialRadius - 12) * Math.Cos(dd)); float y = point.Y + (float)((dialRadius - 12) * Math.Sin(dd)); //因為是從順時鍾3點鍾開始畫,所以索引i需要加上3 int j = i + 3; if (j > 12) j = j - 12; g.DrawString(j.ToString(), hourFont, brush, x, y); } g.Restore(state); // 畫時鍾的圖形 state = g.Save(); g.RotateTransform((dtNow.Hour - 12 + dtNow.Minute / 60f) * 360f / 12f); //時鍾指針默認指向12點鍾方向,分鍾指針也一樣 g.DrawPolygon(new Pen(brush), new Point[] { new Point(0, 20), new Point( 10, 0), new Point(0, -60), new Point(-10, 0) }); g.Restore(state); // 畫分鍾的圖形 state = g.Save(); g.RotateTransform((dtNow.Minute + dtNow.Second / 60f) * 360f / 60f); g.DrawPolygon(new Pen(brush), new Point[] { new Point(0, 20), new Point( 6, 0), new Point(0, -80), new Point(-6, 0) }); g.Restore(state); // 畫秒鍾的圖形 state = g.Save(); g.RotateTransform(dtNow.Second * 360f / 60f); g.FillRectangle(brush, -1, -dialRadius + 10, 2, dialRadius); g.Restore(state); } } }
2,新建一個窗體,拉一個Panel控件上去,在后台代碼中寫上2行代碼,按F5即可看到效果。
private void Form10_Load(object sender, EventArgs e) { ClockControl clock = new ClockControl(); panel1.Controls.Add(clock); }