網站首頁 編程語言 正文
本文實例為大家分享了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
相關推薦
- 2023-01-26 redis性能優化之生產中實際遇到的問題及排查總結_Redis
- 2022-05-14 .NetCore?Web?Api?利用ActionFilterAttribute統一接口返回值格式及
- 2023-03-15 Python多進程協作模擬實現流程_python
- 2022-11-20 CPython?垃圾收集器檢測循環引用詳解_python
- 2022-06-21 詳解C#中檢查null的語法糖_C#教程
- 2022-07-26 使用Docker將容器目錄掛載到主機上的實現方法_docker
- 2023-12-24 npm安裝element ui出錯的問題--版本不匹配
- 2022-07-19 使用普通指針實現數組倒敘和字符串的壓縮
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支