📄 clock.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Chapter15.Windows.Forms
{
public partial class Clock : Control
{
private System.Drawing.Point center;
//hand lengths
private int hourHand;
private int minuteHand;
//points
private System.Drawing.Point hour;
private System.Drawing.Point minute;
private System.Drawing.Point second;
//diameter
private int shortestSide;
//length of quarter hour notches
private int hourMarks;
private Bitmap doubleBuffer;
public Clock()
{
InitializeComponent();
}
protected override void OnResize(EventArgs e)
{
//since all our logic depends on the control size store key dimensions when the control is resized
if(Width > Height)
{
shortestSide = this.Height;
}
else
{
shortestSide = this.Width;
}
center = new Point(shortestSide / 2, shortestSide / 2);
//dispose old buffer
if (doubleBuffer != null)
{
doubleBuffer.Dispose();
}
doubleBuffer = new Bitmap(shortestSide, shortestSide);
minuteHand = (shortestSide / 2) - 4;
hourHand = (shortestSide / 4);
hourMarks = (shortestSide / 8);
base.OnResize(e);
}
protected override void OnPaint(PaintEventArgs pe)
{
// draw the previously created buffer
pe.Graphics.DrawImage(doubleBuffer, 0, 0);
// Calling the base class OnPaint
base.OnPaint(pe);
}
private void timerClock_Tick(object sender, EventArgs e)
{
//update the hands
DateTime d = DateTime.Now;
double angle = d.Second * (Math.PI / 30);
second = GetHandPoint(center, angle, minuteHand);
angle = d.Minute * (Math.PI / 30);
minute = GetHandPoint(center, angle, minuteHand);
angle = d.Hour * (Math.PI / 6);
hour = GetHandPoint(center, angle, hourHand);
Graphics g = Graphics.FromImage(doubleBuffer);
g.Clear(this.BackColor);
Pen handPen = new Pen(colorScheme.HandColor, 2);
Pen borderPen = new Pen(colorScheme.BorderColor);
g.FillEllipse(new SolidBrush(colorScheme.FaceColor), 0, 0, shortestSide, shortestSide);
g.DrawEllipse(borderPen, 0, 0, shortestSide, shortestSide);
//draw quarter marks
g.DrawLine(borderPen, center.X, 2, center.X, hourMarks);
g.DrawLine(borderPen, 2, center.Y, hourMarks, center.Y);
g.DrawLine(borderPen, shortestSide - hourMarks, center.Y, shortestSide - 2, center.Y);
g.DrawLine(borderPen, center.X, shortestSide - hourMarks, center.X, shortestSide - 2);
//draw hands
g.DrawLine(handPen, center.X, center.Y, hour.X, hour.Y);
g.DrawLine(handPen, center.X, center.Y, minute.X, minute.Y);
g.DrawLine(borderPen, center.X, center.Y, second.X, second.Y);
g.FillEllipse(new SolidBrush(colorScheme.HandColor), center.X - 2, center.Y - 2, 4, 4);
g.Dispose();
Refresh();
}
private static Point GetHandPoint(Point center, double angle, int handlength)
{
Point p;
if (angle > 270)
{
angle = angle - 270;
int height = Convert.ToInt32(Math.Sin(angle) * handlength);
int width = Convert.ToInt32(Math.Cos(angle) * handlength);
p = new Point(center.X + width, center.Y + height);
}
else if (angle > 180)
{
angle = angle - 180;
int width = Convert.ToInt32(Math.Sin(angle) * handlength);
int height = Convert.ToInt32(Math.Cos(angle) * handlength);
p = new Point(center.X - width, center.Y + height);
}
else if (angle > 90)
{
angle = angle - 90;
int height = Convert.ToInt32(Math.Sin(angle) * handlength);
int width = Convert.ToInt32(Math.Cos(angle) * handlength);
p = new Point(center.X + width, center.Y + height);
}
else
{
int width = Convert.ToInt32(Math.Sin(angle) * handlength);
int height = Convert.ToInt32(Math.Cos(angle) * handlength);
p = new Point(center.X + width, center.Y - height);
}
return p;
}
#region ColorScheme
private ColorScheme colorScheme = new ColorScheme();
public ColorScheme ColorScheme
{
get
{
return colorScheme;
}
set
{
colorScheme = value;
}
}
#endregion
}
public class ColorScheme
{
private Color borderColor = Color.Gray;
public Color BorderColor
{
get
{
return borderColor;
}
set
{
borderColor = value;
}
}
private Color faceColor = Color.LightBlue;
public Color FaceColor
{
get
{
return faceColor;
}
set
{
faceColor = value;
}
}
private Color handColor = Color.Black;
public Color HandColor
{
get
{
return handColor;
}
set
{
handColor = value;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -