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

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

C#實(shí)現(xiàn)鐘表程序設(shè)計(jì)_C#教程

作者:步北宸 ? 更新時(shí)間: 2022-08-05 編程語(yǔ)言

本文實(shí)例為大家分享了C#實(shí)現(xiàn)鐘表程序設(shè)計(jì)的具體代碼,供大家參考,具體內(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)制的時(shí)間來(lái)刷新paint時(shí)間
? ? ? ? }
? ? ? ? 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)//畫出時(shí),分,秒的線
? ? ? ? {//aValue為角度值,小時(shí)12個(gè)刻度每個(gè)占30°,分和秒60個(gè)刻度每個(gè)站6°avalue按照的是一個(gè)圓分成60個(gè)換成角度乘于6
? ? ? ? ? ? //aLength是指針的長(zhǎng)度
? ? ? ? ? ? double lAngle = ((360 - aValue * 6) + 90) * Math.PI / 180;//角度轉(zhuǎn)化為數(shù)值
? ? ? ? ? ? Point lPoint = new Point((int)(200 + aLength * Math.Cos(lAngle)),//指針線的終點(diǎn),坐標(biāo)x,y通過(guò)直角三角求得
? ? ? ? ? ? ? ? ? ? ? ? (int)(200 - aLength * Math.Sin(lAngle)));
? ? ? ? ? ? aGraphics.DrawLine(aPen, new Point(200, 200), lPoint);//繪制倆點(diǎn)連成一條直線,起點(diǎn)相同都為(200,200)
? ? ? ? }
? ? ? ? private void frmMain_Paint(object sender, PaintEventArgs e)
? ? ? ? {
? ? ? ? ? ? e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//使指針轉(zhuǎn)動(dòng)顯得平滑
? ? ? ? ? ? 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);//繪制鐘表刻度,大點(diǎn)和小點(diǎn)
? ? ? ? ? ? ? ? else fDrawDot(e.Graphics, i, 2);
? ? ? ? ? ? DateTime dt = DateTime.Now;//獲取時(shí)間
? ? ? ? ? ? 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);//繪畫時(shí),分,秒指針
? ? ? ? ? ? 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

欄目分類
最近更新