窗体设计代码:[Form1.Designer.cs]
namespace
Game_One

...
{
partial class Form1

...{

/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;


/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)

...{
if (disposing && (components != null))

...{
components.Dispose();
}
base.Dispose(disposing);
}


Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码


/**//// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()

...{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.ClockPanel = new System.Windows.Forms.Panel();
this.PicBox = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.TimeBox = new System.Windows.Forms.TextBox();
this.CheckBtn = new System.Windows.Forms.Button();
this.AnswerBtn = new System.Windows.Forms.Button();
this.NameLabel = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.PicBox)).BeginInit();
this.SuspendLayout();
//
// ClockPanel
//
this.ClockPanel.BackColor = System.Drawing.Color.Transparent;
this.ClockPanel.ForeColor = System.Drawing.Color.Transparent;
this.ClockPanel.Location = new System.Drawing.Point(12, 95);
this.ClockPanel.Name = "ClockPanel";
this.ClockPanel.Size = new System.Drawing.Size(498, 498);
this.ClockPanel.TabIndex = 0;
//
// PicBox
//
this.PicBox.BackColor = System.Drawing.Color.Transparent;
this.PicBox.Location = new System.Drawing.Point(516, 95);
this.PicBox.Name = "PicBox";
this.PicBox.Size = new System.Drawing.Size(488, 498);
this.PicBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.PicBox.TabIndex = 1;
this.PicBox.TabStop = false;
//
// label1
//
this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.Color.Transparent;
this.label1.Font = new System.Drawing.Font("隶书", 36F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.ForeColor = System.Drawing.Color.Transparent;
this.label1.Location = new System.Drawing.Point(327, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(404, 48);
this.label1.TabIndex = 2;
this.label1.Text = "你能认出他们吗?";
//
// label2
//
this.label2.AutoSize = true;
this.label2.BackColor = System.Drawing.Color.Transparent;
this.label2.Font = new System.Drawing.Font("新宋体", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.ForeColor = System.Drawing.Color.Transparent;
this.label2.Location = new System.Drawing.Point(140, 647);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(194, 27);
this.label2.TabIndex = 3;
this.label2.Text = "他/她的时间:";
//
// TimeBox
//
this.TimeBox.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.TimeBox.Location = new System.Drawing.Point(317, 647);
this.TimeBox.Name = "TimeBox";
this.TimeBox.Size = new System.Drawing.Size(123, 26);
this.TimeBox.TabIndex = 1;
this.TimeBox.TextChanged += new System.EventHandler(this.TimeBox_TextChanged);
//
// CheckBtn
//
this.CheckBtn.Font = new System.Drawing.Font("宋体", 14F);
this.CheckBtn.Location = new System.Drawing.Point(466, 648);
this.CheckBtn.Name = "CheckBtn";
this.CheckBtn.Size = new System.Drawing.Size(96, 27);
this.CheckBtn.TabIndex = 2;
this.CheckBtn.Text = "确 定";
this.CheckBtn.UseVisualStyleBackColor = true;
this.CheckBtn.Click += new System.EventHandler(this.CheckBtn_Click);
//
// AnswerBtn
//
this.AnswerBtn.Font = new System.Drawing.Font("宋体", 14F);
this.AnswerBtn.Location = new System.Drawing.Point(587, 648);
this.AnswerBtn.Name = "AnswerBtn";
this.AnswerBtn.Size = new System.Drawing.Size(96, 27);
this.AnswerBtn.TabIndex = 3;
this.AnswerBtn.Text = "答 案";
this.AnswerBtn.UseVisualStyleBackColor = true;
this.AnswerBtn.Click += new System.EventHandler(this.AnswerBtn_Click);
//
// NameLabel
//
this.NameLabel.AutoSize = true;
this.NameLabel.BackColor = System.Drawing.Color.Transparent;
this.NameLabel.Font = new System.Drawing.Font("新宋体", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.NameLabel.ForeColor = System.Drawing.Color.Transparent;
this.NameLabel.Location = new System.Drawing.Point(746, 645);
this.NameLabel.Name = "NameLabel";
this.NameLabel.Size = new System.Drawing.Size(124, 27);
this.NameLabel.TabIndex = 4;
this.NameLabel.Text = "姓名答案";
this.NameLabel.Visible = false;
//
// Form1
//
this.AcceptButton = this.CheckBtn;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(1016, 734);
this.Controls.Add(this.NameLabel);
this.Controls.Add(this.AnswerBtn);
this.Controls.Add(this.CheckBtn);
this.Controls.Add(this.TimeBox);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.PicBox);
this.Controls.Add(this.ClockPanel);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "热线(北京)通信技术有限公司";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
((System.ComponentModel.ISupportInitialize)(this.PicBox)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Panel ClockPanel;
private System.Windows.Forms.PictureBox PicBox;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox TimeBox;
private System.Windows.Forms.Button CheckBtn;
private System.Windows.Forms.Button AnswerBtn;
private System.Windows.Forms.Label NameLabel;
}
}
程序资源文件就不贴了,就是一张图片
程序实现代码:[Form1.cs]
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.Configuration;

namespace
Game_One

...
{
public partial class Form1 : Form

...{
private DateTime DT = new DateTime();
private Image ImgBak = Image.FromFile(ConfigurationManager.AppSettings["BackGroundImage"].ToString());
System.IO.MemoryStream MEM = new System.IO.MemoryStream();
bool Saved = false;

public Form1()

...{
InitializeComponent();
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
if (!Saved)

...{
ImgBak.Save(MEM, System.Drawing.Imaging.ImageFormat.Jpeg);
Saved = true;
}
TimeBox.Focus();
PicBox.Visible = false;
}
private void AnswerBtn_Click(object sender, EventArgs e)

...{
NameLabel.Text = ConfigurationManager.AppSettings[PicIndex.ToString()].ToString();
NameLabel.Visible = true;
}

private void DrawClock(Graphics g, Rectangle Rect)

...{
//g.Clear(Color.Empty);
Point L = ClockPanel.Location;
Point A_1 = new Point(0-L.X, 0-L.Y);
Point A_2 = new Point(A_1.X + ClockPanel.Width, A_1.Y);
Point A_3 = new Point(A_1.X, A_1.Y + ClockPanel.Height);

Point[] A = new Point[] ...{ A_1, A_2, A_3 };
Point B_1 = new Point(0, 0);
Point B_2 = new Point(ClockPanel.Width, 0);
Point B_3 = new Point(0, ClockPanel.Height);

Point[] B = new Point[] ...{B_1,B_2,B_3 };
Rectangle NewRect = new Rectangle(L.X, L.Y, ClockPanel.Width, ClockPanel.Height);
g.DrawImage(ImgBak, B, NewRect, GraphicsUnit.Pixel);
//绘制圆周
Pen RedPen = new Pen(Color.Red, 3);
g.DrawEllipse(RedPen, 7, 7, 480, 480);

Point Center = new Point(247, 247);

//绘制钟点
Pen PPen_Ora = new Pen(Color.Blue, 4);
Pen PPen_Special = new Pen(Color.Red, 5);
int SidePoint_X;
int SidePoint_Y;

int MovePoint_X;
int MovePoint_Y;

for (int i = 1; i <= 60; i++)

...{
SidePoint_X = 247 + (int)(Math.Sin(Math.PI / 180 * i * 6) * 240);
SidePoint_Y = 247 + (int)(Math.Cos(Math.PI / 180 * i * 6) * 240);
if (i % 5 == 0)

...{
MovePoint_X = 247 + (int)(Math.Sin(Math.PI / 180 * i * 6) * 220);
MovePoint_Y = 247 + (int)(Math.Cos(Math.PI / 180 * i * 6) * 220);
Point MovePoint = new Point(MovePoint_X, MovePoint_Y);
Point SidePoint = new Point(SidePoint_X, SidePoint_Y);
g.DrawLine(PPen_Special, MovePoint, SidePoint);
}
else

...{
MovePoint_X = 247 + (int)(Math.Sin(Math.PI / 180 * i * 6) * 235);
MovePoint_Y = 247 + (int)(Math.Cos(Math.PI / 180 * i * 6) * 235);
Point MovePoint = new Point(MovePoint_X, MovePoint_Y);
Point SidePoint = new Point(SidePoint_X, SidePoint_Y);
g.DrawLine(PPen_Ora, MovePoint, SidePoint);
}
}
int HourPoint = DT.Hour;
int MinutePoint = DT.Minute;

//三角形顶点
int HourEnd_X = 247 + (int)(Math.Sin(Math.PI / 180 * (HourPoint * 30 + MinutePoint * 0.5)) * 180);
int HourEnd_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (HourPoint * 30 + MinutePoint * 0.5)) * 180);
Point hourEnd = new Point(HourEnd_X, HourEnd_Y);

//三角形右节点
int HourRight_X = 247 + (int)(Math.Sin(Math.PI / 180 * (90 + HourPoint * 30 + MinutePoint * 0.5)) * 12);
int HourRight_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (90 + HourPoint * 30 + MinutePoint * 0.5)) * 12);
Point RightSide = new Point(HourRight_X, HourRight_Y);
//三角形左节点
int HourLeft_X = 247 + (int)(Math.Sin(Math.PI / 180 * (270 + HourPoint * 30 + MinutePoint * 0.5)) * 12);
int HourLeft_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (270 + HourPoint * 30 + MinutePoint * 0.5)) * 12);
Point LeftSide = new Point(HourLeft_X, HourLeft_Y);
//用画刷填充区域
SolidBrush HourBrush = new SolidBrush(Color.Gray);

Point[] HourA = new Point[] ...{ RightSide, LeftSide, hourEnd };
g.FillPolygon(HourBrush, HourA);
//箭头中节点
int HHeadMiddle_X = 247 + (int)(Math.Sin(Math.PI / 180 * (HourPoint * 30 + MinutePoint * 0.5)) * 135);
int HHeadMiddle_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (HourPoint * 30 + MinutePoint * 0.5)) * 135);
Point HHeadMiddle = new Point(HHeadMiddle_X, HHeadMiddle_Y);
//箭头左节点
int HHeadLeft_X = 247 + (int)(Math.Sin(Math.PI / 180 * (HourPoint * 30 + MinutePoint * 0.5 - 2)) * 150);
int HHeadLeft_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (HourPoint * 30 + MinutePoint * 0.5 - 2)) * 150);
Point HHeadLeft = new Point(HHeadLeft_X, HHeadLeft_Y);
//箭头右节点
int HHeadRight_X = 247 + (int)(Math.Sin(Math.PI / 180 * (HourPoint * 30 + MinutePoint * 0.5 + 2)) * 150);
int HHeadRight_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (HourPoint * 30 + MinutePoint * 0.5 + 2)) * 150);
Point HHeadRight = new Point(HHeadRight_X, HHeadRight_Y);

Point[] HourHead = new Point[] ...{ HHeadLeft, HHeadMiddle, HHeadRight, hourEnd, };
g.FillPolygon(HourBrush, HourHead);
//绘制分针
int MinuteEnd_X = 247 + (int)(Math.Sin(Math.PI / 180 * MinutePoint * 6) * 230);
int MinuteEnd_Y = 247 - (int)(Math.Cos(Math.PI / 180 * MinutePoint * 6) * 230);
Point MinuteEnd = new Point(MinuteEnd_X, MinuteEnd_Y);
//三角形右节点
int MinuteRight_X = 247 + (int)(Math.Sin(Math.PI / 180 * (90 + MinutePoint * 6)) * 9);
int MinuteRight_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (90 + MinutePoint * 6)) * 9);
Point MinuteRightSide = new Point(MinuteRight_X, MinuteRight_Y);
//三角形左节点
int MinuteLeft_X = 247 + (int)(Math.Sin(Math.PI / 180 * (270 + MinutePoint * 6)) * 9);
int MinuteLeft_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (270 + MinutePoint * 6)) * 9);
Point MinuteLeftSide = new Point(MinuteLeft_X, MinuteLeft_Y);
//用画刷填充区域
SolidBrush MinuteBrush = new SolidBrush(Color.Green);

Point[] MinuteA = new Point[] ...{ MinuteRightSide, MinuteLeftSide, MinuteEnd };
g.FillPolygon(MinuteBrush, MinuteA);
//箭头左节点
int MHeadLeft_X = 247 + (int)(Math.Sin(Math.PI / 180 * (MinutePoint * 6 - 2)) * 190);
int MHeadLeft_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (MinutePoint * 6 - 2)) * 190);
Point MHeadLeft = new Point(MHeadLeft_X, MHeadLeft_Y);
//箭头右节点
int MHeadRight_X = 247 + (int)(Math.Sin(Math.PI / 180 * (MinutePoint * 6 + 2)) * 190);
int MHeadRight_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (MinutePoint * 6 + 2)) * 190);
Point MHeadRight = new Point(MHeadRight_X, MHeadRight_Y);
//箭头中节点
int MHeadMiddle_X = 247 + (int)(Math.Sin(Math.PI / 180 * (MinutePoint * 6)) * 180);
int MHeadMiddle_Y = 247 - (int)(Math.Cos(Math.PI / 180 * (MinutePoint * 6)) * 180);
Point MHeadMiddle = new Point(MHeadMiddle_X, MHeadMiddle_Y);

Point[] MinuteHead = new Point[] ...{ MHeadLeft, MHeadMiddle, MHeadRight, MinuteEnd };
g.FillPolygon(MinuteBrush, MinuteHead);

//绘制轴心
Pen CPen = new Pen(Color.Red, 14);
g.DrawEllipse(CPen, 240, 240, 14, 14);
}

private void Form1_Paint(object sender, PaintEventArgs e)

...{
paintClock(this.ClientRectangle);
}

private static int PicIndex = 0;

private void CheckBtn_Click(object sender, EventArgs e)

...{
try

...{
DT = Convert.ToDateTime(TimeBox.Text.Trim() + ":00");
this.OnPaint(new PaintEventArgs(this.CreateGraphics(),this.ClientRectangle));

if (DT.Hour >= 12)

...{
PicIndex = (DT.Hour - 12) * 3 + DT.Minute / 20 + 1;
}
else

...{
PicIndex = DT.Hour * 3 + DT.Minute / 20 + 1;
}
PicBox.ImageLocation = ConfigurationManager.AppSettings["ManPic"].ToString() + PicIndex.ToString() + ".jpg";
PicBox.Visible = true;
PicBox.Show();
}
catch

...{
}
}

internal void paintClock(Rectangle rect)

...{
if (!this.IsDisposed && this.Visible)

...{
BufferedGraphicsContext myContext = BufferedGraphicsManager.Current;
BufferedGraphics buffer = myContext.Allocate(ClockPanel.CreateGraphics(), rect);
if (buffer.Graphics != null)

...{
buffer.Graphics.Clear(this.BackColor);
this.DrawClock(buffer.Graphics, this.ClientRectangle);
}
buffer.Render();
buffer.Dispose();
}
}

private void TimeBox_TextChanged(object sender, EventArgs e)

...{
NameLabel.Visible = false;
PicBox.ImageLocation = "";
PicBox.Visible = false;
}
}
}
主要实现思路:程序每刷新一次,就重新绘制一次表盘,同时显示时间对应的图片(图片预设36张)
绘制表盘的时候,Panel的背景需要被Cache 一次,
然后再在Panel上重新绘制,这样可以避免重新绘制整个
Form的背景带来的消耗