bluefin
unread,Jun 25, 2008, 2:33:09 PM6/25/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to C# Guide
写个指针时钟程序
第一步:新建一个WinForm的工程,添加一个UserControl的派生类取名ClockControl.
第二步:DoubleBuffered属性设为true, 防止闪烁,也可以自己用MemBitmap来做,不过.NET提供了方便的
DoubleBuffered,这点比C++好方便太多了。
第三步:添加一个Timer,定时时间为1000(1 Second),即每秒刷新一次,取当前的时间。Timer的Tick事件代码如下:
private void clockTimer_Tick(object sender, EventArgs e)
{
Invalidate();
}
第四步:Overright OnPaint 直接看代码吧
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
// init the origin
g.TranslateTransform(this.Width / 2.0f, this.Height / 2.0f);
int dialRadius = Math.Min(this.Width, this.Height) / 2;
// Draw the clock dial
GraphicsState state = g.Save();
for (int i = 0; i<60; i++)
{
int radius = 15;
if (i % 5 == 0)
radius = 25;
g.FillEllipse(Brushes.Blue, new Rectangle(-radius / 2, -
dialRadius, radius, radius));
g.RotateTransform(360 / 60);
}
g.Restore(state);
// Get current time
DateTime now = DateTime.Now;
// Draw hour hand
state = g.Save();
g.RotateTransform((Math.Abs(now.Hour - 12) + now.Minute / 60f ) *
360f / 12f);
g.FillRectangle(Brushes.Black, new Rectangle(-5, -dialRadius +
50, 10, dialRadius - 40));
g.Restore(state);
// Draw Minute hand
state = g.Save();
g.RotateTransform((now.Minute + now.Second / 60f) * 360f / 60f);
g.FillRectangle(Brushes.DarkGreen, new Rectangle(-3, -dialRadius
+ 30, 6, dialRadius - 15));
g.Restore(state);
// Draw Second hand
state = g.Save();
g.RotateTransform(now.Second * 360f / 60f);
g.FillRectangle(Brushes.Red, new Rectangle(-1, -dialRadius + 10,
2, dialRadius));
g.Restore(state);
}
简单说明一下,这个Control主要使用GDI+的Transform函数, 用
g.TranslateTransform(this.Width / 2.0f, this.Height / 2.0f);把坐标原点移动到中心,
使用RotateTransform来旋转角度实现时钟的表皮和指针的不同位置。具体位置的计算,大家一看就知道了。
最后再加一行代码:
public ClockControl()
{
InitializeComponent();
this.SetStyle(ControlStyles.ResizeRedraw, true);
}
有2个办法可以解决不直的问题,
1,设置Graphics的属性,g.SmoothingMode = SmoothingMode.HighQuality;
2,用Png图片来做指针和表盘,效果虽然很棒,但是GDI+画PNG的速度比较慢。可以把用PNG文件创建TextureBrush,用这个
brush可以提高速度。