首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

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

开发工具:VS2010;.net framework 3.5

程序:小时钟

功能:显示时间

目的:学习GDI+绘图

步骤:

1、打开VS2010

2、新建WinForm项目

3、窗体放一个定时器Timer控件、一个Panel控件。

4、Timer控件Tick事件输入显示时钟走动的代码

5、完成

重点:Graphics的使用

一、程序运行截图:

二、主要代码:

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

{

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)));

}

三、提示:

查看操作实录视频请关注“业余码农说”,查看发布的同名视频即可

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190113A0YTKB00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券