C#画的时钟控件

时钟控件代码:

 

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  testgrid
{
    
public partial class ClockControl : UserControl
    
{
        DateTime dt;
        
public ClockControl()
        
{
            InitializeComponent();
            ResizeRedraw 
= true;
            Enabled 
= false;
        }

        
public DateTime Time
        
{
            
get
            
{
                
return dt;
            }

            
set
            
{
                Graphics g 
= CreateGraphics();
                InitializeCoordinates(g);
                Pen pen 
= new Pen(BackColor);
                
if (dt.Hour != value.Hour)
                
{
                    DrawHourHand(g, pen);
                }

                
if (dt.Minute != value.Minute)
                
{
                    DrawHourHand(g, pen);
                    DrawMinuteHand(g, pen);
                }

                
if (dt.Second != value.Second)
                
{
                    DrawMinuteHand(g, pen);
                    DrawSecondHand(g, pen);
                }

                
if (dt.Millisecond != value.Millisecond)
                
{
                    DrawSecondHand(g, pen);
                }

                dt 
= value;
                pen 
= new Pen(ForeColor);
                DrawHourHand(g, pen);
                DrawMinuteHand(g, pen);
                DrawSecondHand(g, pen);
                g.Dispose();
            }

        }

        
protected override void OnPaint(PaintEventArgs e)
        
{
            
//base.OnPaint(e);
            Graphics g = e.Graphics;
            Pen pen 
= new Pen(ForeColor);
            Brush br 
= new SolidBrush(ForeColor);
            InitializeCoordinates(g);
            DrawDots(g, br);
            DrawHourHand(g, pen);
            DrawMinuteHand(g, pen);
            DrawSecondHand(g, pen);
        }

        
void InitializeCoordinates(Graphics g)
        
{
            
if (Width == 0 || Height == 0)
                
return;
            g.TranslateTransform(Width 
/ 2, Height / 2);
            
float fInches = Math.Min(Width / g.DpiX, Height / g.DpiY);
            g.ScaleTransform(fInches 
* g.DpiX / 2000, fInches * g.DpiY / 2000);

        }

        
void DrawDots(Graphics g, Brush br)
        
{
            
for (int i = 0; i < 60; i++)
            
{
                
int iSize = i % 5 == 0 ? 100 : 30;
                g.FillEllipse(br, 
0 - iSize / 2-900 - iSize / 2, iSize, iSize);
                g.RotateTransform(
6);
            }

        }

        
protected virtual void DrawHourHand(Graphics g, Pen pen)
        
{
            GraphicsState gs 
= g.Save();
            g.RotateTransform(360f 
* Time.Hour / 12 + 30f * Time.Minute / 60);
            g.DrawPolygon(pen, 
new Point[] new Point(0150), new Point(1000), new Point(0-600), new Point(-1000) });
            g.Restore(gs);
        }

        
protected virtual void DrawMinuteHand(Graphics g, Pen pen)
        
{
            GraphicsState gs 
= g.Save();
            g.RotateTransform(360f 
* Time.Minute / 60 + 6f * Time.Second / 60);
            g.DrawPolygon(pen, 
new Point[] new Point(0200), new Point(500), new Point(0-800), new Point(-500) });
            g.Restore(gs);
        }

        
protected virtual void DrawSecondHand(Graphics g, Pen pen)
        
{
            GraphicsState gs 
= g.Save();
            g.RotateTransform(360f 
* Time.Second / 60 + 6f * Time.Millisecond / 1000);
            g.DrawLine(pen, 
000-800);
            g.Restore(gs);
        }

    }


}

窗体代码:

 

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

namespace  testgrid
{
    
public partial class AnalogClock : Form
    
{
        ClockControl clk;
        
public AnalogClock()
        
{
            InitializeComponent();
            clk 
= new ClockControl();
            clk.Parent 
= this;
            clk.Time 
= DateTime.Now;
            clk.Dock 
= DockStyle.Fill;
            clk.BackColor 
= Color.Black;
            clk.ForeColor 
= Color.White;
            Timer timer 
= new Timer();
            timer.Interval 
= 100;
            timer.Tick 
+= new EventHandler(timer_Tick);
            timer.Start();
        }


        
void timer_Tick(object sender, EventArgs e)
        
{
            
//throw new Exception("The method or operation is not implemented.");
            clk.Time = DateTime.Now;
        }

    }

}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值