標籤:
本文由作者參考部分案例後加以修改完成:
參考連結如下:
http://blog.csdn.net/xuemoyao/article/details/8001113
http://wenku.baidu.com/view/1221d6522f60ddccda38a0e7
(1) 最終效果
(2)程式實現分析:
選用Paint事件進行實現繪製,然後加一個定時器設定為1000毫秒更新一次。
(3)具體方法
在Form表單(設定大小為300*300),中添加Time1,屬性Enabled改為true,Interval設定為1000
然後在Designer.cs中Form1的屬性定義下添加
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
在Form1.cs中Form1類下添加函數
Private void Form_Paint(Object Sender,PaintEventArgs e)
{
Graphics g=e.Graphics; //定義g為該表單控制項的畫布
int r=100;
// 繪製數字時鐘
Rectangle r1=new Rectangle(50,10,200,20);
//參數分別為左上方矩形座標,寬度和長度
g.FillRectangle(Brushes.Chocolate,r1);//填充顏色
int ss=DateTime.Now.Second;
int mm=DateTime.Now.Minute;
int hh=DateTime.Now.Hour;
String s=Convert.ToString(ss);
String m=Convert.ToString(mm);
String h=Convert.ToString(hh);
if(ss>=0&&ss<=9)
s=”0”+s;
if(mm>=0&&mm<=9)
m=”0”+m;
if(hh>=0&&hh<=9)
h=”0”+h;
Font f1=new Font(“宋體”,12,FontStyle.Bold);
StringFormat sf1=new StringFormat();
SolidBrush s1=new SolidBrush(Color.White);
g.Drawstring(“目前時間”+h+”:”+m+”:”+s,f1,s1,r1,sf1);
g.DrawString("目前時間 " + h + ":" + m + ":" + s, f1, s1, r1, sf1);
//繪製圓形輪廓
g.FillEllipse(Brushes.White,50,50,200,200)
g.DrawEllipse(New Pen(Color.Red,2),46,46,208,208);
g.DrawEllipse(new Pen(Color.DarkGray, 1), 50,50, 200, 200);
//繪製數字刻度
g.ResetTransform();
g.TranslateTransform(150, 150); //重新置放座標
Font drawFont = new Font("Arial", 12);
SolidBrush drawBrush = new SolidBrush(Color.Black);
e.Graphics.DrawString("6", drawFont, drawBrush,-7,70);
e.Graphics.DrawString("12", drawFont, drawBrush, -9, -80);
e.Graphics.DrawString("3", drawFont, drawBrush, 70, -7);
e.Graphics.DrawString("9", drawFont, drawBrush, -80, -7);
//繪製刻度
for (int z = 0; z < 60; z++)
{
g.SmoothingMode=System.Drawing.Drawing2D
.SmoothingMode.HighQuality;
//使畫出的指標更平滑、高品質
g.ResetTransform;
g.TranslateTransform(150,150); //更改座標原點
g.RotateYransform(z*5); //旋轉,每一秒旋轉6度
if(z%5==0)
g=DrawLine(new Pen(Color.Black,3.0f).r-12,0,r-5,0);
//小時刻度
else
g.DrawLine(new Pen(Color.Black,1.5f),r-8,0,r-5,0);
//分鐘標準刻度
}
//繪製秒針
g.ResetTransform(); //恢複預設狀態
g.TranslateTransform(150,150);
g.RotateTransform(ss*6+270);
//以水平線為x軸,從垂直上方開始旋轉,每次旋轉6度。
Pen secPen=new Pen(Color.Red,1);
secPen.StartCap=System.Drawing.Drawing2D.LineCap.RoundAnchor;
//畫線,從圓點開始
secPen.EndCap=System.Drawing.Drawing2D.LineCap.ArrowAnchor;
//畫線,結束於箭頭
g.DrawLine(secPen,0,0,65,0);//65表示線的長度
//繪製分針
g.ResetTransform();
g.TranslateTransform(150,150);
g.RotateTransform(mm*6+270);
Pen minPen=new Pen(Color.Blue,2);
minPen.StartCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
minPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
g.DrawLine(minPen, 0, 0,50 , 0);
//繪製時針
g.ResetTransform();
g.TranslateTransform(150,150);
g.RotateTransform(hh*30+mm*1/2+270);
Pen hourPen = new Pen(Color.Black,3);
hourPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
g.DrawLine(hourPen, 0, 0, 35, 0);
}
time1的響應事件
private void timer1_Tick(object sender, EventArgs e)
{
Invalidate();
}
C#繪製圓形時鐘