日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#實現鐘表程序設計_C#教程

作者:步北宸 ? 更新時間: 2022-08-05 編程語言

本文實例為大家分享了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);

原文鏈接:https://blog.csdn.net/qq_38534627/article/details/80626329

欄目分類
最近更新