C井编程,一个简单的小时钟程序,就是界面刷新有点闪烁

小微 科技C井编程,一个简单的小时钟程序,就是界面刷新有点闪烁已关闭评论110字数 2358阅读模式
摘要开发工具:VS2010;.net framework 3.5程序:小时钟功能:显示时间目的:学习GDI+绘图步骤:1、打开VS20102、新建WinForm项目3、窗体放一个定时器...

开发工具: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井编程,一个简单的小时钟程序,就是界面刷新有点闪烁”的详细内容,希望对大家有所帮助!

继续阅读
 
小微
  • 版权声明: 本文部分文字与图片资源来自于网络,转载此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请立即通知我们(管理员邮箱:81118366@qq.com),情况属实,我们会第一时间予以删除,并同时向您表示歉意,谢谢!
  • 转载请务必保留本文链接:https://93wg.com/11779.html