欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# winform时钟的实现代码

程序员文章站 2024-02-14 23:55:46
代码如下: 复制代码 代码如下:using system; using system.collections.generic; using syste...

代码如下:

复制代码 代码如下:

using system;
 using system.collections.generic;
 using system.componentmodel;
 using system.data;
 using system.drawing;

 using system.text;
 using system.windows.forms;

 namespace simpclock
 {
     public partial class form1 : form
     {
         datetime date = datetime.now;

         public form1()
         {
             initializecomponent();

         }

         private void form1_load(object sender, eventargs e)
         {
             date = datetime.now;

         }

         private void onpaint(object sender, painteventargs e)
         {
             font font = new font("times new roman", 20);
             graphics g = creategraphics();
             g.drawstring(date.tostring(), font, brushes.firebrick, 10,330 );
             g.drawstring(date.dayofweek.tostring(), font, brushes.red, 250, 330);
             drawdial(g);
             drawsecondpointer(g);
             drawminutepointer(g);
             drawhourpointer(g);

         }
         //刷新时间
         private void ontime(object sender, eventargs e)
         {
             date = datetime.now;
             invalidate();
         }
         //画钟表
         //表盘部分
         point getposition(int s, point center, double radius)//定位
         {
             point p = new point();
             double x = center.x + radius * math.sin(math.pi / 30 * s);
             double y = center.y - radius * math.cos(math.pi / 30 * s);
             p.x = (int)x;
             p.y = (int)y;
             return p;
         }

         void drawdial(graphics g)//外圆及刻度
         {
             int n;
             rectangle rect = new rectangle(40, 10, 300, 300);
             //g.fillellipse(brushes.white, 40, 10, 300, 300);
             g.drawellipse(new pen(color.black, 3), rect);
             point p1, p2;
             point center = new point(190, 160);
             for (n = 0; n < 60; n++)
             {
                 p1 = getposition(n, center, 150);
                 if (n % 5 == 0)
                 {
                     p2 = getposition(n, center, 130);
                     g.drawline(new pen(color.black, 2), p1, p2);
                 }
                 else
                 {
                     p2 = getposition(n, center, 140);
                     g.drawline(pens.red, p1, p2);
                 }
             }
             font font = new font("times new roman", 20);
             n = 0;
             p1 = getposition(n, center, 130);
             g.drawstring("xii", font, brushes.black, p1.x - 25, p1.y);
             n += 15;
             p1 = getposition(n, center, 130);
             g.drawstring("iii", font, brushes.black, p1.x - 35, p1.y - 15);
             n += 15;
             p1 = getposition(n, center, 130);
             g.drawstring("vi", font, brushes.black, p1.x - 20, p1.y - 30);
             n += 15;
             p1 = getposition(n, center, 130);
             g.drawstring("ix", font, brushes.black, p1.x, p1.y - 15);
         }
         //秒针部分
         void drawsecondpointer(graphics g)
         {
             point center = new point(190, 160);
             point p;
             p = getposition(date.second, center, 130);
             g.drawline(pens.red, center, p);
             g.fillellipse(brushes.red, new rectangle(p.x - 2, p.y - 2, 4, 4));

         }
         //分针部分
         void drawminutepointer(graphics g)
         {
             point center = new point(190, 160);
             point p;
             p = getposition(date.minute, center, 120);
             g.drawline(pens.blue, center, p);
             //g.fillellipse(brushes.blue, new rectangle(p.x - 4, p.y - 4, 8, 8));
         }
         //时针部分
         point gethourposition(point center, double radius)
         {
             point p = new point();
             int h = date.hour;
             int m = date.minute;
             double t = math.pi / 6 * h + math.pi / 360 * m;
             double x = center.x + radius * math.sin(t);
             double y = center.y - radius * math.cos(t);
             p.x = (int)x;
             p.y = (int)y;
             return p;
         }
         void drawhourpointer(graphics g)
         {
             point center = new point(190, 160);
             point p = gethourposition(center, 100);
             g.drawline(new pen(brushes.black, 2), center, p);
             //去指针圆尖
             // g.fillellipse(brushes.black,
             //              new rectangle(p.x - 6, p.y - 6, 12, 12));
             g.fillellipse(brushes.yellowgreen,
             new rectangle(center.x - 6, center.y - 6, 12, 12));

 
         }

     }
}