C#實現(xiàn)鐘表程序設(shè)計
更新時間:2022年06月14日 09:27:44 作者:步北宸
這篇文章主要為大家詳細介紹了C#實現(xiàn)鐘表程序設(shè)計,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C#實現(xiàn)鐘表程序設(shè)計的具體代碼,供大家參考,具體內(nèi)容如下
工作空間:
代碼如下:
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();//調(diào)用窗體的Paint事件,根據(jù)調(diào)制的時間來刷新paint時間 ? ? ? ? } ? ? ? ? private void fDrawDot(Graphics aGraphics, double aValue, int aRadius)//畫出表盤刻度 ? ? ? ? { ? ? ? ? ? ? double lAngle = ((360 - aValue * 6) + 90) * Math.PI / 180;//根據(jù)角度繪畫 ? ? ? ? ? ? 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為內(nèi)部填充顏色方法,Rectangle為繪畫圓,前倆為圓心坐標(biāo),后倆為寬和高 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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;//角度轉(zhuǎn)化為數(shù)值 ? ? ? ? ? ? Point lPoint = new Point((int)(200 + aLength * Math.Cos(lAngle)),//指針線的終點,坐標(biāo)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;//使指針轉(zhuǎn)動顯得平滑 ? ? ? ? ? ? e.Graphics.DrawImage(this.BackgroundImage, 0, 0, 400, 400);//規(guī)定表盤的大小和位置 ? ? ? ? ? ? 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);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c# socket網(wǎng)絡(luò)編程接收發(fā)送數(shù)據(jù)示例代碼
這篇文章主要介紹了c# socket網(wǎng)絡(luò)編程,server端接收,client端發(fā)送數(shù)據(jù),大家參考使用吧2013-12-12C#中Convert.ToInt32()和int.Parse()的區(qū)別介紹
Convert是一個類,繼承自system.Object;int是值類型,在本文為大家詳細介紹下它與int.Parse()的區(qū)別,感興趣的朋友可以參考下2013-10-10C#調(diào)用百度翻譯API實現(xiàn)一個翻譯功能
一直喜歡用Google Translate API進行在線翻譯,但是服務(wù)越來越慢這篇文章,所以只能換一個了,主要給大家介紹了關(guān)于C#調(diào)用百度翻譯API實現(xiàn)一個翻譯功能的相關(guān)資料,需要的朋友可以參考下2021-06-06