你的位置:首页 > ASP.net教程

[ASP.net教程]C#绘制圆形时钟


本文由作者参考部分案例后加以修改完成:

参考链接如下:

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();

}