开发工具:VS2010;.net framework 3.5
程序:小时钟文章源自微观生活(93wg.com)微观生活-https://93wg.com/11779.html
功能:显示时间文章源自微观生活(93wg.com)微观生活-https://93wg.com/11779.html
目的:学习GDI+绘图文章源自微观生活(93wg.com)微观生活-https://93wg.com/11779.html
步骤:文章源自微观生活(93wg.com)微观生活-https://93wg.com/11779.html
1、打开VS2010文章源自微观生活(93wg.com)微观生活-https://93wg.com/11779.html
2、新建WinForm项目文章源自微观生活(93wg.com)微观生活-https://93wg.com/11779.html
3、窗体放一个定时器Timer控件、一个Panel控件。文章源自微观生活(93wg.com)微观生活-https://93wg.com/11779.html
4、Timer控件Tick事件输入显示时钟走动的代码文章源自微观生活(93wg.com)微观生活-https://93wg.com/11779.html
5、完成文章源自微观生活(93wg.com)微观生活-https://93wg.com/11779.html
重点:Graphics的使用文章源自微观生活(93wg.com)微观生活-https://93wg.com/11779.html
一、程序运行截图:
二、主要代码:
private void timerClock_Tick(object sender, EventArgs e)
{
Graphics thisGraphics = this.pnlClock.CreateGraphics();
thisGraphics.SmoothingMode = SmoothingMode.AntiAlias;
thisGraphics.SmoothingMode = SmoothingMode.HighQuality;//消锯齿
int clock_width = this.pnlClock.Width;//时钟宽度
int clock_height = this.pnlClock.Height;//时钟高度
thisGraphics.FillEllipse(new SolidBrush(Color.White), 0, 0, clock_width - 4, clock_height - 4);//整个表盘的填充圆
Pen thisPen = new Pen(new SolidBrush(Color.Yellow), 2);
thisGraphics.DrawEllipse(thisPen, 0, 0, clock_width - 4, clock_height - 4);//表盘边框圆
thisGraphics.TranslateTransform(clock_width / 2, clock_height / 2);//设置新坐标原点到圆心位置
thisGraphics.FillEllipse(Brushes.Green, -10, -10, 20, 20);//画表心
for (int x = 0; x < 60; x++) //画分钟/秒刻度线
{
int rect_y = (clock_height - 8) / 2 - 2;
Rectangle rect = new Rectangle(-2, -rect_y, 3, 10);
thisGraphics.FillRectangle(new SolidBrush(Color.Olive), rect);
thisGraphics.RotateTransform(6);//分秒刻度为60个,每一个刻度偏移角度为360/60 = 6 度 及为分、秒偏移角度
}
for (int i = 12; i > 0; i--) //画小时刻度线
{
//绘制整点刻度
int rect_y = (clock_height - 8) / 2 - 2;
Rectangle rect = new Rectangle(-3, -rect_y, 6, 20);
thisGraphics.FillRectangle(new SolidBrush(Color.DarkGreen), rect);
//绘制数值
Font font = new Font(new FontFamily(\"Arial\"), 40, FontStyle.Bold, GraphicsUnit.Pixel);
SolidBrush brush = new SolidBrush(Color.DarkGreen);
int p_x = -16;
if (i > 9) p_x = -30;
int p_y = (clock_height - 8) / -2 + 26;
PointF point = new PointF(p_x, p_y);
thisGraphics.DrawString(i.ToString(), font, brush, point);
//顺时针旋转30度
thisGraphics.RotateTransform(-30);
}
//取得当前时间
int second = DateTime.Now.Second;
int minute = DateTime.Now.Minute;
int hour = DateTime.Now.Hour;
float line_y = (clock_height / 2) - 60;//指针的长度
//绘秒针
thisPen = new Pen(Color.Blue, 1);
thisGraphics.RotateTransform(6 * second);//每一秒偏移6度
thisGraphics.DrawLine(thisPen, new PointF(0, 0), new PointF(0, -line_y));
//绘分针
thisPen = new Pen(Color.Green, 4);
thisGraphics.RotateTransform(-6 * second);
thisGraphics.RotateTransform((float)(second * 0.1 + minute * 6));//每一分偏移角度
thisGraphics.DrawLine(thisPen, new PointF(0, 0), new PointF(0, -(line_y-30)));
//绘时针
thisPen = new Pen(Color.Red, 8);
thisGraphics.RotateTransform((float)(-second * 0.1 - minute * 6));
thisGraphics.RotateTransform((float)(second * 0.01 + minute * 0.5 + hour * 30));//每一小时偏移角度
thisGraphics.DrawLine(thisPen, new PointF(0, 0), new PointF(0, -(line_y-60)));
}
以上就是微观生活(93wg.com)关于“C井编程,一个简单的小时钟程序,就是界面刷新有点闪烁”的详细内容,希望对大家有所帮助!
评论