111,120
社区成员
发帖
与我相关
我的任务
分享
public partial class clock : Form
{
public clock()
{
InitializeComponent();
Timer timer = new Timer();
timer.Enabled = true;
timer.Interval = 100;
timer.Tick += new EventHandler(timer_Tick);
}
void timer_Tick(object sender, EventArgs e)
{
this.paintClock(this.ClientRectangle);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
this.drawClock(e.Graphics, e.ClipRectangle);
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.paintClock(this.ClientRectangle);
}
private void drawClock(Graphics g, Rectangle rectangle)
{
SolidBrush brush1 = new SolidBrush(Color.SteelBlue);
SolidBrush brush2 = new SolidBrush(Color.SkyBlue);
SolidBrush brush3 = new SolidBrush(Color.Teal);
SolidBrush brush4 = new SolidBrush(Color.Teal);
SolidBrush brush5 = new SolidBrush(Color.Black);
Pen pen1 = new Pen(brush1, 2.0f); //"时点"画笔
Pen pen2 = new Pen(brush2, 1.5f); //"分点"画笔
Pen pen3 = new Pen(brush3, 0.2f); //"时针"画笔
Pen pen4 = new Pen(brush4, 0.2f); //"分针"画笔
Pen pen5 = new Pen(brush5, 1); //"秒针"画笔
Point center = new Point(this.ClientRectangle.Left + this.ClientRectangle.Width / 2, this.ClientRectangle.Top + this.ClientRectangle.Height / 2);
//画表盘
for (int i = 1; i <= 60; i++)
{
if (i % 5 == 0)
g.DrawEllipse(pen1, center.X + (int)(Math.Cos(Math.PI / 180 * i * 6) * 60), center.Y + (int)(Math.Sin(Math.PI / 180 * i * 6) * 60), 2.0f, 2.0f);
else
g.DrawEllipse(pen2, center.X + (int)(Math.Cos(Math.PI / 180 * i * 6) * 60), center.Y + (int)(Math.Sin(Math.PI / 180 * i * 6) * 60), 1.5f, 1.5f);
}
//画时针
Point ph = new Point(center.X + (int)(Math.Cos(Math.PI / 180 * ((DateTime.Now.Hour - 3) * 30 + DateTime.Now.Minute / 12 * 6)) * 40), center.Y + (int)(Math.Sin(Math.PI / 180 * ((DateTime.Now.Hour - 3) * 30 + DateTime.Now.Minute / 12 * 6)) * 40));
Point phl = new Point(center.X + (int)(Math.Cos(Math.PI / 180 * ((DateTime.Now.Hour + 3) * 30 + DateTime.Now.Minute / 12 * 6)) * 7), center.Y + (int)(Math.Sin(Math.PI / 180 * ((DateTime.Now.Hour + 3) * 30 + DateTime.Now.Minute / 12 * 6)) * 7));
Point pch1 = new Point(center.X + (int)(Math.Cos(Math.PI / 180 * ((DateTime.Now.Hour) * 30 + DateTime.Now.Minute / 12 * 6)) * -5), center.Y + (int)(Math.Sin(Math.PI / 180 * ((DateTime.Now.Hour) * 30 + DateTime.Now.Minute / 12 * 6)) * -5));
Point pch2 = new Point(center.X + (int)(Math.Cos(Math.PI / 180 * ((DateTime.Now.Hour) * 30 + DateTime.Now.Minute / 12 * 6)) * 5), center.Y + (int)(Math.Sin(Math.PI / 180 * ((DateTime.Now.Hour) * 30 + DateTime.Now.Minute / 12 * 6)) * 5));
g.FillPolygon(brush1, new Point[] { ph, pch1, phl, pch2 });
//画分针
Point pm = new Point(center.X + (int)(Math.Cos(Math.PI / 180 * (DateTime.Now.Minute - 15) * 6) * 48), center.Y + (int)(Math.Sin(Math.PI / 180 * (DateTime.Now.Minute - 15) * 6) * 48));
Point pml = new Point(center.X + (int)(Math.Cos(Math.PI / 180 * (DateTime.Now.Minute + 15) * 6) * 9), center.Y + (int)(Math.Sin(Math.PI / 180 * (DateTime.Now.Minute + 15) * 6) * 9));
Point pcm1 = new Point(center.X + (int)(Math.Cos(Math.PI / 180 * (DateTime.Now.Minute) * 6) * -4), center.Y + (int)(Math.Sin(Math.PI / 180 * (DateTime.Now.Minute) * 6) * -4));
Point pcm2 = new Point(center.X + (int)(Math.Cos(Math.PI / 180 * (DateTime.Now.Minute) * 6) * 4), center.Y + (int)(Math.Sin(Math.PI / 180 * (DateTime.Now.Minute) * 6) * 4));
g.FillPolygon(brush1, new Point[] { pm, pcm1, pml, pcm2 });
//画秒针
Point pc = new Point(center.X, center.Y);
Point ps = new Point(center.X + (int)(Math.Cos(Math.PI / 180 * (DateTime.Now.Second - 15) * 6) * 48), center.Y + (int)(Math.Sin(Math.PI / 180 * (DateTime.Now.Second - 15) * 6) * 48));
g.DrawLine(pen5, pc, ps);
}
internal void paintClock(Rectangle rect)
{
if (!this.IsDisposed && this.Visible)
{
BufferedGraphicsContext myContext = BufferedGraphicsManager.Current;
BufferedGraphics buffer = myContext.Allocate(this.CreateGraphics(), rect);
if (buffer.Graphics != null)
{
buffer.Graphics.Clear(this.BackColor);
this.drawClock(buffer.Graphics, this.ClientRectangle);
}
buffer.Render();
buffer.Dispose();
}
}
}