用.net GDI+制作时钟

GDI+ 是 Windows XP 操作系统中提供二维矢量图形、图像处理和版式的部分。GDI+ 在 GDI(较早版本的 Windows 中提供的 Graphics Device Interface)的基础上进行了改进,添加了新功能并优化了现有功能。

利用GDI+可以很方面的画图绘图,并且也能很方便地操作图形数据。下面本人以一个简单的例子来展示如果用C#和GDI+来制作一个时钟。

运行结果如下:

程序代码如下:

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Drawing.Drawing2D;
using  System.Drawing.Imaging;
using  System.Text;
using  System.Threading;
using  System.Windows.Forms;

namespace  Clock
{
    
public   partial   class  MainForm : Form
    {
        
private  PointF center;
        
private   float  r;
        
private  Pen hourPen;
        
private  Pen minutePen;
        
private  Pen secondPen;
        
private  Thread timeThread;

        
public  MainForm()
        {
            InitializeComponent();
            ShowInTaskbar 
=   false ; // 不在任务栏中显示
            SetStyle(ControlStyles.DoubleBuffer,  true );
            SetStyle(ControlStyles.UserPaint,
true );
            SetStyle(ControlStyles.AllPaintingInWmPaint,
true );

            center 
=   new  PointF(ClientSize.Width /   2.0f , ClientSize.Height /   2.0f );
            r 
=  Math.Min(ClientSize.Width  /   2.0f , ClientSize.Height  /   2.0f );
            hourPen 
=   new  Pen(Color.Black,  2.5f );
            minutePen 
=   new  Pen(Color.Black,  1.5f );
            secondPen 
=   new  Pen(Color.Black,  0.5f );
            timeThread
= new  Thread( new  ThreadStart(DrawTime));
            timeThread.Start();
        }

        
protected   override   void  OnPaint(PaintEventArgs e)
        {
            Graphics g 
=  e.Graphics;
            g.Clear(BackColor);
            
// g.SmoothingMode = SmoothingMode.AntiAlias;
            g.SmoothingMode  =  SmoothingMode.HighQuality;
            g.DrawArc(secondPen, 
new  RectangleF(center.X  -  r, center.Y  -  r,  2   *  r - 3 2   *  r - 3 ),  0 360 );

            GraphicsPath round 
=   new  GraphicsPath();
            round.AddArc(
new  RectangleF(center.X  -  r, center.Y  -  r,  2   *  r  -   3 2   *  r  -   3 ),  0 360 );
            Region 
=   new  Region(round);

            Pen pen
= new  Pen(Color.Blue, 3.0f );
            
for  ( int  i  =   0 ; i  <   59 ; i ++ )
            {
                g.ResetTransform();
                g.TranslateTransform(center.X, center.Y);
                g.RotateTransform(i 
*   6 );
                
if  ((i  ==   0 ||  (i  %   5   ==   4 ))
                {
                    g.DrawLine(hourPen, r 
-   12 0 , r  -   5 0 );
                }
                
else
                {
                    g.DrawLine(secondPen, r 
-   6 0 , r  -   5 0 );
                }
            }
            
            
            
float  hour,minute,second;
            hour
= DateTime.Now.Hour;
            minute
= DateTime.Now.Minute;
            second
= DateTime.Now.Second;
            hour
= hour + minute / 60f + second / 3600f;
            minute
= minute + second / 60f;
            
// 画时针
            g.ResetTransform();
            g.TranslateTransform(center.X, center.Y);
            g.RotateTransform(hour 
*   30 + 270 - 6 );
            g.DrawLine(hourPen, 
0 0 , r  *   0.5f , 0f);
            
// 画分针
            g.ResetTransform();
            g.TranslateTransform(center.X, center.Y);
            g.RotateTransform(minute 
*   6 + 270 - 6 );
            g.DrawLine(minutePen, 
0 0 , r  *   0.6f , 0f);
            
// 画秒针
            g.ResetTransform();
            g.TranslateTransform(center.X, center.Y);
            g.RotateTransform(second 
*   6 + 270 - 6 );
            g.DrawLine(secondPen, 
0 0 , r  *   0.7f , 0f);
            
base .OnPaint(e);
        }

        
private   void  DrawTime()
        {
            
while  ( true )
            {
                Invalidate();
                Thread.Sleep(
900 ); // 为避免某些延时影响,900ms画一次时钟指针
            }
        }

        
protected   void  Dispose( object  sender,EventArgs e)
        {
            Dispose();
            
base .Dispose(Disposing);
            Application.Exit();
        }

        
private   void  tsmExit_Click( object  sender, EventArgs e)
        {
            Close();
            Dispose();
            
base .Dispose(Disposing);
            Application.Exit();
        }
    }
}
程序代码比较简单,而且也做了注释,估计大家都看得懂,我就不罗唆了。以后有时间再做难度稍大一点的例子。
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周公

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值