網(wǎng)站首頁(yè) 編程語(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
相關(guān)推薦
- 2022-11-21 關(guān)于Fragment?already?added問(wèn)題的解決方案_Android
- 2022-12-13 Python操作Excel數(shù)據(jù)的封裝函數(shù)分享_python
- 2022-06-12 Dockerfile文件編寫及構(gòu)建鏡像命令解析_docker
- 2022-02-01 es ik分詞插件安裝
- 2022-11-14 swiftui開(kāi)發(fā)之padding默認(rèn)值設(shè)置詳解_Swift
- 2022-07-06 c#?模擬串口通信?SerialPort的實(shí)現(xiàn)示例_C#教程
- 2022-04-09 DM8 數(shù)據(jù)庫(kù)連接Alibaba druid 提示:dbType not support
- 2022-09-29 Python組合數(shù)據(jù)類型詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支