C#时钟控件
多年前写的时钟控件,发布出来,供大家参考。先看效果:
首先,制作一个从UserControl继承的时钟控件,使用双缓冲绘制。由于是几年前的水平,一些代码写到了属性中,也懒得再改了,感兴趣的,可以改得更规范些。
//ClockControl.cs
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace BrawDraw.Com.PhotoFrame.Net.PublicFunctions
{
public class ClockControl: UserControl
{
DateTime dt;
Pen pen = new Pen(Color.Black, 1);
Pen penSecond = new Pen(Color.Black, 6);
Brush brush = new SolidBrush(Color.Black);
Bitmap m_Bitmap = null;
Graphics g = null;
private Font ClockFont = new Font("Arial", 48, FontStyle.Bold);
public ClockControl()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
this.UpdateStyles();
Enabled = false;
m_Bitmap = new Bitmap(100, 100);
}
public DateTime Time
{
get
{
return dt;
}
set
{
m_Bitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
g = Graphics.FromImage(m_Bitmap);
g.Clear(Color.White);
InitializeCoordinates(g);
DrawDots(g, brush);
DrawHourHand(g, pen);
DrawMinuteHand(g, pen);
DrawNumbers(g);
// DrawSecondHand(g, pen);
// if(g != null) g.Dispose();
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;
Graphics grfx = CreateGraphics();
grfx.DrawImage(m_Bitmap, 0, 0);
this.Validate();
if(grfx != null) grfx.Dispose();
}
}
protected override void OnPaint(PaintEventArgs pea)
{
g = pea.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
DrawClock(g);
base.OnPaint (pea);
}
private void DrawClock(Graphics gg)
{
m_Bitmap = new Bitmap(this.Width, this.Height);
Graphics grfx = Graphics.FromImage(m_Bitmap);
grfx.Clear(Color.White);
InitializeCoordinates(grfx);
DrawDots(grfx, brush);
DrawHourHand(grfx, pen);
DrawMinuteHand(grfx, pen);
DrawSecondHand(grfx, pen);
DrawNumbers(grfx);
gg.DrawImage(m_Bitmap, 0, 0);
grfx.Dispose();
}
void InitializeCoordinates(Graphics grfx)
{
if (Width == 0 || Height == 0) return;
grfx.TranslateTransform(Width / 2, Height / 2);
float fInches = Math.Min(Width / grfx.DpiX, Height / grfx.DpiY);
grfx.ScaleTransform(fInches * grfx.DpiX / 2000,
fInches * grfx.DpiY / 2000);
}
void DrawDots(Graphics grfx, Brush brush)
{
for (int i = 0; i < 60; i++)
{
int iSize = i % 5 == 0 ? 100 : 30;
grfx.FillEllipse(brush, 0 - iSize / 2, - 860 - iSize / 2, iSize, iSize);
grfx.RotateTransform(6);
}
}
protected virtual void DrawHourHand(Graphics grfx, Pen pen)
{
GraphicsState gs = grfx.Save();
grfx.RotateTransform(360f * Time.Hour / 12 + 30f * Time.Minute / 60 + 2);
grfx.DrawPolygon(pen, new Point[]
{
new Point(0, 150), new Point( 100, 0),
new Point(0, -600), new Point(-100, 0)
});
grfx.Restore(gs);
}
protected virtual void DrawMinuteHand(Graphics grfx, Pen pen)
{
GraphicsState gs = grfx.Save();
grfx.RotateTransform(360f * Time.Minute / 60 +
6f * Time.Second / 60 + 2);
grfx.DrawPolygon(pen, new Point[]
{
new Point(0, 200), new Point( 50, 0),
new Point(0, -800), new Point(-50, 0)
});
grfx.Restore(gs);
}
protected virtual void DrawSecondHand(Graphics grfx, Pen pen)
{
GraphicsState gs = grfx.Save();
grfx.RotateTransform(360f * Time.Second / 60 + 6f * Time.Millisecond / 1000 + 2.8f);
grfx.DrawLine(penSecond, 0, 0, 0, -800);
grfx.Restore(gs);
}
private void InitializeComponent()
{
//
// ClockControl
//
this.Name = "ClockControl";
this.Size = new System.Drawing.Size(416, 424);
}
protected void DrawNumbers(Graphics grfx)
补充:软件开发 , C# ,