C#实现钟表程序设计
短信预约 -IT技能 免费直播动态提醒
本文实例为大家分享了C#实现钟表程序设计的具体代码,供大家参考,具体内容如下
工作空间:
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace 钟表
{
public partial class frmMain : Form
{
bool flsMouseDown = false;
Point fDownPosition;
public frmMain()
{
InitializeComponent();
}
private void ctsmiExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void frmMain_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
flsMouseDown = true;
fDownPosition = e.Location;
//Debug.WriteLine(e.Location);
}
}
private void frmMain_MouseMove(object sender, MouseEventArgs e)
{
if (flsMouseDown)
{
this.Left = this.Location.X + (e.X - fDownPosition.X);
this.Top = this.Location.Y + (e.Y - fDownPosition.Y);
}
}
private void frmMain_Mouseup(object sender, MouseEventArgs e)
{
flsMouseDown = false;
}
private void tmrMain_Tick(object sender, EventArgs e)
{
this.Refresh();//调用窗体的Paint事件,根据调制的时间来刷新paint时间
}
private void fDrawDot(Graphics aGraphics, double aValue, int aRadius)//画出表盘刻度
{
double lAngle = ((360 - aValue * 6) + 90) * Math.PI / 180;//根据角度绘画
Point lPoint = new Point((int)(200 + 180 * Math.Cos(lAngle)),//刻度所在的为值
(int)(200 - 180 * Math.Sin(lAngle)));
aGraphics.FillEllipse(Brushes.Black, new Rectangle(lPoint.X - aRadius,//FillEllipse为内部填充颜色方法,Rectangle为绘画圆,前俩为圆心坐标,后俩为宽和高
lPoint.Y - aRadius, 2 * aRadius, 2 * aRadius));
}
private void fDrawLine(Graphics aGraphics, double aValue, double aLength, Pen aPen)//画出时,分,秒的线
{//aValue为角度值,小时12个刻度每个占30°,分和秒60个刻度每个站6°avalue按照的是一个圆分成60个换成角度乘于6
//aLength是指针的长度
double lAngle = ((360 - aValue * 6) + 90) * Math.PI / 180;//角度转化为数值
Point lPoint = new Point((int)(200 + aLength * Math.Cos(lAngle)),//指针线的终点,坐标x,y通过直角三角求得
(int)(200 - aLength * Math.Sin(lAngle)));
aGraphics.DrawLine(aPen, new Point(200, 200), lPoint);//绘制俩点连成一条直线,起点相同都为(200,200)
}
private void frmMain_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//使指针转动显得平滑
e.Graphics.DrawImage(this.BackgroundImage, 0, 0, 400, 400);//规定表盘的大小和位置
for (int i = 0; i < 60; i++)
if (i % 5 == 0) fDrawDot(e.Graphics, i, 6);//绘制钟表刻度,大点和小点
else fDrawDot(e.Graphics, i, 2);
DateTime dt = DateTime.Now;//获取时间
Pen pSecond = new Pen(Color.Black, 2);
Pen pMinute = new Pen(Color.Red, 2);
Pen pHour = new Pen(Color.Blue, 4);
fDrawLine(e.Graphics, ((dt.Hour % 12) + dt.Minute / 60.0) * 5, 100, pHour);//绘画时,分,秒指针
fDrawLine(e.Graphics, dt.Minute+dt.Second/60.0,160, pMinute);
fDrawLine(e.Graphics, dt.Second+dt.Millisecond/1000.0, 170, pSecond);
}
Form1.Designer.cs
需添加:(以下代码)
this.DoubleBuffered = true;
this.Deactivate += new System.EventHandler(this.ctsmiExit_Click);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.frmMain_Paint);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.frmMain_MouseDown);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.frmMain_MouseMove);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.frmMain_Mouseup);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341