欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#繪制時(shí)鐘的方法

 更新時(shí)間:2022年06月13日 15:49:35   作者:貓貓的小茶館  
這篇文章主要為大家詳細(xì)介紹了C#繪制時(shí)鐘的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了使用C#寫一個(gè)時(shí)鐘,供大家參考,具體內(nèi)容如下

時(shí)鐘是這樣的

一共使用四個(gè)控件即可:

WinFrom窗體應(yīng)用程序代碼:

using SpeechLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyClockTest
{
? ? public partial class Form1 : Form
? ? {
? ? ? ? //創(chuàng)建對(duì)象myclock
? ? ? ? MyClock myclock = new MyClock();

? ? ? ? //定義date1,共電子表使用 -- 獲取到當(dāng)下時(shí)間點(diǎn)
? ? ? ? DateTime date1 = DateTime.Now; //Now 為靜態(tài)只讀屬性(屬性可計(jì)算)

? ? ? ? //定義中心點(diǎn)center和半徑radius -- 時(shí)鐘的圓心和半徑大小
? ? ? ? Point center = new Point(160, 200); // 圓心點(diǎn)的坐標(biāo)
? ? ? ? int radius = 150; // 半徑 150
? ? ? ? //定義判定是否擊中表盤的bool變量 -- 鼠標(biāo)點(diǎn)擊拖拽功能
? ? ? ? bool down = false;
? ? ? ? //記錄鼠標(biāo)點(diǎn)擊的點(diǎn)
? ? ? ? Point pre;

? ? ? ? //程序入口
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }

? ? ? ? //開(kāi)始畫時(shí)鐘
? ? ? ? private void OnPaint(object sender, PaintEventArgs e)
? ? ? ? {
? ? ? ? ? ? //這里不使用Graphics,而選擇使用BufferedGraphics:
? ? ? ? ? ? //Graphics和BufferedGraphics對(duì)比:
? ? ? ? ? ? //5000次下時(shí)Graphics效率高一些;5000次以后BufferedGraphics效率高一些
? ? ? ? ? ? //Bitmap bmp = new Bitmap(width ,height );
? ? ? ? ? ? //Graphics g2 = Graphics.FromImage(bmp );
? ? ? ? ? ? //myclock.Draw(g2);
? ? ? ? ? ? //g2.CreateGraphics();
? ? ? ? ? ? //g2.DrawImage(bmp,0,0);
? ? ? ? ? ? //bmt.dispose();

? ? ? ? ? ? //Image iamge = new Bitmap(this.Width ,this .Height );
? ? ? ? ? ? //Graphics g1 = Graphics.FromImage(iamge);
? ? ? ? ? ? //g1.Clear(Form .DefaultBackColor );

? ? ? ? ? ? //C#雙緩沖解釋:
? ? ? ? ? ? //簡(jiǎn)單說(shuō)就是當(dāng)我們?cè)谶M(jìn)行畫圖操作時(shí),系統(tǒng)并不是直接把內(nèi)容呈現(xiàn)到屏幕上,而是先在內(nèi)存中保存,然后一次性把結(jié)果輸出來(lái)。
? ? ? ? ? ? //如果沒(méi)用雙緩沖的話,你會(huì)發(fā)現(xiàn)在畫圖過(guò)程中屏幕會(huì)閃的很厲害,因?yàn)楹笈_(tái)一直在刷新,
? ? ? ? ? ? //而如果等用戶畫完之后再輸出就不會(huì)出現(xiàn)這種情況,具體的做法,其實(shí)也就是先創(chuàng)建一個(gè)位圖對(duì)象,然后把內(nèi)容保存在里面,最后把圖呈現(xiàn)出來(lái)。

? ? ? ? ? ? //解決空間重繪時(shí)閃爍的問(wèn)題,并在改變大小時(shí)重繪圖形
? ? ? ? ? ? this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
? ? ? ? ? ? //矩形繪圖區(qū)域
? ? ? ? ? ? Rectangle rect = e.ClipRectangle;

? ? ? ? ? ? //關(guān)于BufferedGraphicsContext類:
? ? ? ? ? ? // ? ?手工設(shè)置雙緩沖.netframework提供了一個(gè)類BufferedGraphicsContext負(fù)責(zé)單獨(dú)分配和管理圖形緩沖區(qū)。
? ? ? ? ? ? // ? ?每個(gè)應(yīng)用程序域都有自己的默認(rèn) BufferedGraphicsContext 實(shí)例來(lái)管理此應(yīng)用程序的所有默認(rèn)雙緩沖。
? ? ? ? ? ? // ? ?大多數(shù)情況下,每個(gè)應(yīng)用程序只有一個(gè)應(yīng)用程序域,所以每個(gè)應(yīng)用程序通常只有一個(gè)默認(rèn) BufferedGraphicsContext。

? ? ? ? ? ? //關(guān)于此處的繪圖:
? ? ? ? ? ? //通過(guò)管理BufferedGraphicsContext實(shí)現(xiàn)雙緩沖的步驟如下:

? ? ? ? ? ? //(1)獲得對(duì) BufferedGraphicsContext 類的實(shí)例的引用。

? ? ? ? ? ? //(2)通過(guò)調(diào)用 BufferedGraphicsContext.Allocate 方法創(chuàng)建 BufferedGraphics 類的實(shí)例。

? ? ? ? ? ? //(3)通過(guò)設(shè)置 BufferedGraphics.Graphics 屬性將圖形繪制到圖形緩沖區(qū)。

? ? ? ? ? ? //(4)當(dāng)完成所有圖形緩沖區(qū)中的繪制操作時(shí),可調(diào)用 BufferedGraphics.Render 方法將緩沖區(qū)的內(nèi)容呈現(xiàn)到與該緩沖區(qū)關(guān)聯(lián)的繪圖圖面或者指定的繪圖圖面。

? ? ? ? ? ? //(5)完成呈現(xiàn)圖形之后,對(duì) BufferedGraphics 實(shí)例調(diào)用釋放系統(tǒng)資源的 Dispose 方法。

? ? ? ? ? ? BufferedGraphicsContext current = BufferedGraphicsManager.Current; //(1)
? ? ? ? ? ? BufferedGraphics bg;
? ? ? ? ? ? bg = current.Allocate(e.Graphics, e.ClipRectangle); //(2)
? ? ? ? ? ? Graphics g = bg.Graphics; //(3)
? ? ? ? ? ? //定義畫布?
? ? ? ? ? ? //Graphics g = CreateGraphics();
? ? ? ? ? ? //消除鋸齒
? ? ? ? ? ? //g.SmoothingMode = SmoothingMode.AntiAlias;
? ? ? ? ? ? g.SmoothingMode = SmoothingMode.HighQuality; //高質(zhì)量
? ? ? ? ? ? g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移質(zhì)量
? ? ? ? ? ? //每次繪畫后的背景色填充 && 定義畫電子鐘的字體
? ? ? ? ? ? g.Clear(this.BackColor);
? ? ? ? ? ? Font font = new Font("Times New Roman", 20);
? ? ? ? ? ? //在每次繪畫時(shí),更新時(shí)間,半徑及中心點(diǎn)
? ? ? ? ? ? myclock.Date = DateTime.Now;
? ? ? ? ? ? myclock.Week = DateTime.Now;
? ? ? ? ? ? myclock.P = center;
? ? ? ? ? ? myclock.R = radius;

? ? ? ? ? ? //調(diào)用myclock中的Draw函數(shù)畫出鐘
? ? ? ? ? ? myclock.Draw(g);
? ? ? ? ? ? //直接調(diào)用DrawString函數(shù),顯示電子表,并且設(shè)置顯示格式
? ? ? ? ? ? g.DrawString(date1.ToString(), font, Brushes.Black, 10, 10);//10,10表示字的左上角在哪
? ? ? ? ? ? bg.Render(e.Graphics); //(4)---> 開(kāi)啟繪制
? ? ? ? ? ? bg.Dispose(); //(5)---> 繪制完成,釋放資源

? ? ? ? ? ? //如果為整點(diǎn),則報(bào)時(shí)
? ? ? ? ? ? if (Convert.ToString(DateTime.Now.Minute) == "0" && Convert.ToString(DateTime.Now.Second ) == "0")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? SpVoice voice = new SpVoice();
? ? ? ? ? ? ? ? voice.Speak("現(xiàn)在時(shí)間為" + Convert.ToString(DateTime.Now.Month) + "月" + Convert.ToString(DateTime.Now.Day) + "日" + Convert.ToString(DateTime.Now.Hour) + "點(diǎn)" + Convert.ToString(DateTime.Now.Minute) + "分", SpeechVoiceSpeakFlags.SVSFDefault);
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? //更新時(shí)鐘的時(shí)間
? ? ? ? private void OnTime(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? //使用timer控件來(lái)更新時(shí)間 ---> 1s/次

? ? ? ? ? ? //更新鐘的時(shí)間
? ? ? ? ? ? myclock.Date = DateTime.Now;
? ? ? ? ? ? myclock.Week = DateTime.Now;
? ? ? ? ? ? //更新電子表的時(shí)間
? ? ? ? ? ? date1 = DateTime.Now;
? ? ? ? ? ? //刷新 --- 每走動(dòng)一秒都更新畫布(重繪)
? ? ? ? ? ? Invalidate();
? ? ? ? ? ? //Refresh();
? ? ? ? }

? ? ? ? //鼠標(biāo)單擊到時(shí)鐘上的坐標(biāo)
? ? ? ? private void Form1_MouseDown(object sender, MouseEventArgs e)
? ? ? ? {
? ? ? ? ? ? //記錄點(diǎn)擊的點(diǎn),并且計(jì)算是否擊中時(shí)鐘 -- 記錄點(diǎn)擊時(shí)的坐標(biāo)
? ? ? ? ? ? pre = new Point(e.X, e.Y);
? ? ? ? ? ? down = myclock.HitTest(pre, center, radius);
? ? ? ? }

? ? ? ? //在點(diǎn)擊后,鼠標(biāo)拖動(dòng)時(shí)
? ? ? ? private void Form1_MouseMove(object sender, MouseEventArgs e)
? ? ? ? {
? ? ? ? ? ? //使用if來(lái)判斷是否擊中,若擊中,更新中心點(diǎn)
? ? ? ? ? ? Point p = new Point(e.X, e.Y);
? ? ? ? ? ? //如果點(diǎn)擊持續(xù)
? ? ? ? ? ? if (down)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //更新圓心坐標(biāo)點(diǎn)
? ? ? ? ? ? ? ? center = new Point(center.X + p.X - pre.X, center.Y + p.Y - pre.Y);
? ? ? ? ? ? ? ? //pre = 當(dāng)前移動(dòng)的點(diǎn)的坐標(biāo)
? ? ? ? ? ? ? ? pre = p;
? ? ? ? ? ? ? ? //重繪
? ? ? ? ? ? ? ? Invalidate();
? ? ? ? ? ? ? ? //Refresh();
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? private void Form1_MouseUp(object sender, MouseEventArgs e)
? ? ? ? {
? ? ? ? ? ? //取消擊中 -- 拖拽結(jié)束
? ? ? ? ? ? down = false;
? ? ? ? }

? ? ? ? private void trackBar1_Scroll(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? //使用 trackBar1來(lái)控制半徑大小,并且更新 ---> trackBar控件改變時(shí)鐘的大小
? ? ? ? ? ? radius = trackBar1.Value;
? ? ? ? ? ? Invalidate();
? ? ? ? ? ? //refresh
? ? ? ? }

? ? ? ? //單擊:報(bào)時(shí)功能
? ? ? ? private void button1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? DateTime da = new DateTime();
? ? ? ? ? ? da = DateTime.Now;
? ? ? ? ? ? //SpVoice報(bào)時(shí)
? ? ? ? ? ? SpVoice voice = new SpVoice();
? ? ? ? ? ? voice.Speak("現(xiàn)在時(shí)間為" + Convert.ToString(da.Month) + "月" + Convert.ToString(da.Day) + "日" + Convert.ToString(da.Hour) + "點(diǎn)" + Convert.ToString(da.Minute) + "分", SpeechVoiceSpeakFlags.SVSFDefault);
? ? ? ? }
? ? }

? ? //初始化時(shí)鐘:類應(yīng)該寫在外面(此處不規(guī)范)
? ? public class MyClock
? ? {
? ? ? ? enum time { XII, III, VI, IX } //定義結(jié)構(gòu)體 -- 時(shí)鐘的4頂點(diǎn)
? ? ? ? public DateTime date; //Now 為靜態(tài)只讀{get}屬性(屬性可計(jì)算),智能作為右值 -- 電子表時(shí)間
? ? ? ? public DateTime week; // 周
? ? ? ? public Point p; // 圓心點(diǎn)的坐標(biāo)
? ? ? ? public int r; // 半徑

? ? ? ? public MyClock()
? ? ? ? {
? ? ? ? ? ? //定義一個(gè)構(gòu)造函數(shù),賦予date當(dāng)前時(shí)間
? ? ? ? ? ? date = DateTime.Now;
? ? ? ? ? ? week = DateTime.Now;
? ? ? ? ? ? p = new Point(160, 200); // 圓心點(diǎn)的坐標(biāo)
? ? ? ? ? ? r = 150; // 半徑
? ? ? ? }

? ? ? ? //畫表盤
? ? ? ? public void DrawDial(Graphics g)
? ? ? ? {
? ? ? ? ? ? //定義兩個(gè)pen,p1用來(lái)畫小刻度及表盤,p2用來(lái)畫大刻度
? ? ? ? ? ? Pen p1 = new Pen(Color.Black, 2);
? ? ? ? ? ? Pen p2 = new Pen(Color.Green, 2);
? ? ? ? ? ? //時(shí)鐘頂點(diǎn)時(shí)間的字體大小
? ? ? ? ? ? float f = r / 10;
? ? ? ? ? ? //字體樣式 && 字體大小
? ? ? ? ? ? Font font = new Font("Times New Roman", f);

? ? ? ? ? ? //定義兩個(gè)點(diǎn)數(shù)組來(lái)存儲(chǔ)刻度線的兩點(diǎn) --- [60] 數(shù)組中共存放了60個(gè)點(diǎn)的坐標(biāo)
? ? ? ? ? ? PointF[] pointf1 = new PointF[60];
? ? ? ? ? ? PointF[] pointf2 = new PointF[60];
? ? ? ? ? ? //?
? ? ? ? ? ? for(int i = 0; i < 59; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //通過(guò)for循環(huán)來(lái)給pointf1賦值 ---> f1點(diǎn)就在圓形的邊上,r
? ? ? ? ? ? ? ? float x1 = (float)(p.X + r * Math.Sin((i + 1) * Math.PI * 2 / 60));
? ? ? ? ? ? ? ? float y1 = (float)(p.Y + r * Math.Cos((i + 1) * Math.PI * 2 / 60));
? ? ? ? ? ? ? ? pointf1[i] = new PointF(x1, y1);

? ? ? ? ? ? ? ? //使用if來(lái)判斷,能整除5就畫大刻度,不能就畫小刻度
? ? ? ? ? ? ? ? if ((i + 1) % 5 == 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? float x2 = (float)(p.X + (r - r / 20) * Math.Sin((i + 1) * 2 * Math.PI / 60));
? ? ? ? ? ? ? ? ? ? float y2 = (float)(p.Y + (r - r / 20) * Math.Cos((i + 1) * 2 * Math.PI / 60));
? ? ? ? ? ? ? ? ? ? pointf2[i] = new PointF(x2, y2);
? ? ? ? ? ? ? ? ? ? //畫大刻度
? ? ? ? ? ? ? ? ? ? g.DrawLine(p2, pointf1[i], pointf2[i]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? float x2 = (float)(p.X + (r - r / 40) * Math.Sin((i + 1) * 2 * Math.PI / 60));
? ? ? ? ? ? ? ? ? ? float y2 = (float)(p.Y + (r - r / 40) * Math.Cos((i + 1) * 2 * Math.PI / 60));
? ? ? ? ? ? ? ? ? ? pointf2[i] = new PointF(x2, y2);
? ? ? ? ? ? ? ? ? ? //畫小刻度
? ? ? ? ? ? ? ? ? ? g.DrawLine(p2, pointf1[i], pointf2[i]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? //用pointf3來(lái)儲(chǔ)存四個(gè)點(diǎn)的位置 表盤的4個(gè)頂點(diǎn)位置 ---> 橫坐標(biāo) && 縱坐標(biāo)調(diào)整距離到合適的位置上
? ? ? ? ? ? PointF[] pointf3 =
? ? ? ? ? ? {
? ? ? ? ? ? new PointF (pointf1 [30].X-f/4,pointf1 [30].Y +r/20),
? ? ? ? ? ? new PointF (pointf1 [15].X-r/20-2*f ,pointf1 [15].Y+f/3 ),
? ? ? ? ? ? new PointF (pointf1 [0].X-2*f ,pointf1 [0].Y -r/20-4*f/3),
? ? ? ? ? ? new PointF (pointf1 [45].X+r/20 ,pointf1 [45].Y -r/20-4*f/3),
? ? ? ? ? ? };

? ? ? ? ? ? //畫出四個(gè)點(diǎn)相應(yīng)的時(shí)間值
? ? ? ? ? ? for (int m = 0; m < 4; m++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? g.DrawString(((time)m).ToString(), font, Brushes.Black, pointf3[m]);
? ? ? ? ? ? }

? ? ? ? ? ? //代碼重復(fù) ---> 代碼保持高內(nèi)聚低耦合 ---> (time)將阿拉伯?dāng)?shù)字改成時(shí)間類型
? ? ? ? ? ? //g.DrawString(((time)0).ToString(), font, Brushes.Black, pointf3[0]);
? ? ? ? ? ? //g.DrawString(time.III.ToString(), font, Brushes.Black, pointf3[1]);
? ? ? ? ? ? //g.DrawString(time.VI.ToString(), font, Brushes.Black, pointf3[2]);
? ? ? ? ? ? //g.DrawString(time.IX.ToString(), font, Brushes.Black, pointf3[3]);

? ? ? ? ? ? //使用DrawEllipse畫表盤 --- pen 左上角的x坐標(biāo) 左上角的y坐標(biāo) 寬度 高度 ---> 畫圓
? ? ? ? ? ? g.DrawEllipse(p1, p.X - r, p.Y - r, 2 * r, 2 * r);
? ? ? ? }

? ? ? ? //畫時(shí)針
? ? ? ? public void DrawHourPointer(Graphics g)
? ? ? ? {
? ? ? ? ? ? //定義畫筆p1,設(shè)置其屬性,使其帶有箭頭,圓點(diǎn)尾部
? ? ? ? ? ? Pen p1 = new Pen(Color.Green, 4);
? ? ? ? ? ? p1.EndCap = LineCap.ArrowAnchor; // 箭頭
? ? ? ? ? ? p1.StartCap = LineCap.Round; // 尾部

? ? ? ? ? ? //獲取分鐘,轉(zhuǎn)化為int型
? ? ? ? ? ? int i = Convert.ToInt32(date.Minute);
? ? ? ? ? ? //獲取小時(shí),并轉(zhuǎn)換為int型,同時(shí)對(duì)12取余,獲得十二小時(shí)制的小時(shí)數(shù)
? ? ? ? ? ? int j = (Convert.ToInt32(date.Hour)) % 12;

? ? ? ? ? ? //計(jì)算點(diǎn)值 --- (2 * Math.PI) * j / 720) 根據(jù)分鐘表示時(shí)針的位置 ---> 1分鐘再小時(shí)里占比1/720 ---> 60分鐘*12個(gè)小時(shí)數(shù) = 720
? ? ? ? ? ? //點(diǎn)1的坐標(biāo)
? ? ? ? ? ? float x1 = (float)(p.X + (r - r / 3) * Math.Sin((j * 2 * Math.PI / 12) + (2 * Math.PI) * i / 720));
? ? ? ? ? ? float y1 = (float)(p.Y - (r - r / 3) * Math.Cos((j * 2 * Math.PI / 12) + (2 * Math.PI) * i / 720));
? ? ? ? ? ? //點(diǎn)2的坐標(biāo)
? ? ? ? ? ? float x2 = (float)(p.X - r / 30 * Math.Sin((j * 2 * Math.PI / 12) + (2 * Math.PI) * i / 720));
? ? ? ? ? ? float y2 = (float)(p.Y + r / 30 * Math.Cos((j * 2 * Math.PI / 12) + (2 * Math.PI) * i / 720));
? ? ? ? ? ? PointF pointf1 = new PointF(x1, y1);
? ? ? ? ? ? PointF pointf2 = new PointF(x2, y2);

? ? ? ? ? ? //用DrawLine畫時(shí)針
? ? ? ? ? ? g.DrawLine(p1, pointf2, pointf1);
? ? ? ? }

? ? ? ? //畫分針
? ? ? ? public void DrawMinutePointer(Graphics g)
? ? ? ? {
? ? ? ? ? ? //定義畫筆p1,設(shè)置其屬性,使其帶有箭頭,圓點(diǎn)尾部
? ? ? ? ? ? //畫筆寬度為3 ---> 分針比時(shí)針細(xì)一點(diǎn)
? ? ? ? ? ? Pen p1 = new Pen(Color.Blue, 3);
? ? ? ? ? ? p1.EndCap = LineCap.ArrowAnchor;
? ? ? ? ? ? p1.StartCap = LineCap.Round;

? ? ? ? ? ? //獲取當(dāng)前分鐘,并轉(zhuǎn)換為int型
? ? ? ? ? ? int i = Convert.ToInt32(date.Minute);

? ? ? ? ? ? //計(jì)算點(diǎn)值
? ? ? ? ? ? //點(diǎn)1
? ? ? ? ? ? float x1 = (float)(p.X + (r - r / 5) * Math.Sin(i * 2 * Math.PI / 60));
? ? ? ? ? ? float y1 = (float)(p.Y - (r - r / 5) * Math.Cos(i * 2 * Math.PI / 60));
? ? ? ? ? ? //點(diǎn)2
? ? ? ? ? ? float x2 = (float)(p.X - r / 20 * Math.Sin(i * 2 * Math.PI / 60));
? ? ? ? ? ? float y2 = (float)(p.Y + r / 20 * Math.Cos(i * 2 * Math.PI / 60));
? ? ? ? ? ? PointF pointf1 = new PointF(x1, y1);
? ? ? ? ? ? PointF pointf2 = new PointF(x2, y2);

? ? ? ? ? ? //用DrawLine畫分針
? ? ? ? ? ? g.DrawLine(p1, pointf2, pointf1);
? ? ? ? }

? ? ? ? //畫秒針
? ? ? ? public void DrawSecondPointer(Graphics g)
? ? ? ? {
? ? ? ? ? ? Pen p1 = new Pen(Color.Red, 2);
? ? ? ? ? ? p1.EndCap = LineCap.ArrowAnchor;
? ? ? ? ? ? p1.StartCap = LineCap.Round;

? ? ? ? ? ? //獲取當(dāng)前秒,并轉(zhuǎn)換為int型
? ? ? ? ? ? int i = Convert.ToInt32(date.Second);

? ? ? ? ? ? //計(jì)算點(diǎn)值
? ? ? ? ? ? float x1 = (float)(p.X + (r - r / 20) * Math.Sin(i * 2 * Math.PI / 60));
? ? ? ? ? ? float y1 = (float)(p.Y - (r - r / 20) * Math.Cos(i * 2 * Math.PI / 60));
? ? ? ? ? ? float x2 = (float)(p.X - r / 15 * Math.Sin(i * 2 * Math.PI / 60));
? ? ? ? ? ? float y2 = (float)(p.Y + r / 15 * Math.Cos(i * 2 * Math.PI / 60));
? ? ? ? ? ? PointF pointf1 = new PointF(x1, y1);
? ? ? ? ? ? PointF pointf2 = new PointF(x2, y2);
? ? ? ? ? ? //用DrawLine畫秒針 ---> 根據(jù)兩點(diǎn)之間畫線
? ? ? ? ? ? g.DrawLine(p1, pointf2, pointf1);
? ? ? ? }

? ? ? ? //周和日期顯示 ---> ?表盤上
? ? ? ? public void DrawWeeks(Graphics g)
? ? ? ? {
? ? ? ? ? ? //字體大小
? ? ? ? ? ? float f = r / 15;
? ? ? ? ? ? Font font = new Font("Times New Roman", f);
? ? ? ? ? ? //放置位置
? ? ? ? ? ? float f1 = (float)(p.X + r * Math.Sin(Math.PI) / 2);
? ? ? ? ? ? float f2 = (float)(p.Y - r * Math.Cos(Math.PI) / 2);
? ? ? ? ? ? //DrawString ---> 畫字符串
? ? ? ? ? ? g.DrawString(week.DayOfWeek.ToString(), font, Brushes.Black, f1, f2);
? ? ? ? ? ? g.DrawString(week.Day.ToString(), font, Brushes.Black, f1 + 80, f2);
? ? ? ? }

? ? ? ? public void Draw(Graphics g)
? ? ? ? {
? ? ? ? ? ? //定義函數(shù)Draw,畫出時(shí)鐘

? ? ? ? ? ? //畫表盤
? ? ? ? ? ? DrawDial(g);
? ? ? ? ? ? //畫時(shí)針
? ? ? ? ? ? DrawHourPointer(g);
? ? ? ? ? ? //畫分針
? ? ? ? ? ? DrawMinutePointer(g);
? ? ? ? ? ? //畫秒針
? ? ? ? ? ? DrawSecondPointer(g);
? ? ? ? ? ? //畫周和日期
? ? ? ? ? ? DrawWeeks(g);
? ? ? ? }

? ? ? ? public bool HitTest(Point p, Point center, int r)
? ? ? ? {
? ? ? ? ? ? //HitTest函數(shù)表示鼠標(biāo)是否點(diǎn)在時(shí)鐘里面

? ? ? ? ? ? //通過(guò)計(jì)算鼠標(biāo)點(diǎn)擊的點(diǎn)到中心點(diǎn)的距離和半徑的比較來(lái)判定是否擊中時(shí)鐘
? ? ? ? ? ? //圓心到半徑的距離畫圓,便是圓的全部面積
? ? ? ? ? ? double f1 = (double)(p.X - center.X);
? ? ? ? ? ? double f2 = (double)(p.Y - center.Y);

? ? ? ? ? ? //test即為鼠標(biāo)點(diǎn)擊的點(diǎn)到中心點(diǎn)的距離
? ? ? ? ? ? //根據(jù)勾股定理,求距離,直角三角形 a2 + b2 = c2
? ? ? ? ? ? double test = Math.Sqrt(f1 * f1 + f2 * f2);

? ? ? ? ? ? //斜邊長(zhǎng)于圓的半徑,則在圓的范圍之外;斜邊小于圓的半徑,則在圓的范圍之內(nèi)
? ? ? ? ? ? if (test <= r)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? #region 屬性
? ? ? ? //此處設(shè)置成了只讀 ---> 不可寫 ---> 導(dǎo)致時(shí)鐘的DateTime時(shí)間無(wú)法寫入進(jìn)來(lái)
? ? ? ? //public DateTime Date { get; internal set; }
? ? ? ? //public DateTime Week { get; internal set; }
? ? ? ? //public Point P { get; internal set; }
? ? ? ? //public int R { get; internal set; }

? ? ? ? public DateTime Date
? ? ? ? {
? ? ? ? ? ? set { date = value; }
? ? ? ? }
? ? ? ? public Point P
? ? ? ? {
? ? ? ? ? ? set { p = value; }
? ? ? ? }
? ? ? ? public int R
? ? ? ? {
? ? ? ? ? ? set { r = value; }
? ? ? ? }
? ? ? ? public DateTime Week
? ? ? ? {
? ? ? ? ? ? set { week = value; }
? ? ? ? }
? ? ? ? #endregion
? ? }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#使用ImitateLogin模擬登錄百度

    C#使用ImitateLogin模擬登錄百度

    這篇文章主要介紹了C#使用ImitateLogin模擬登錄百度 的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • Winform項(xiàng)目中使用FastReport.Net報(bào)表控件

    Winform項(xiàng)目中使用FastReport.Net報(bào)表控件

    這篇文章介紹了Winform項(xiàng)目中使用FastReport.Net報(bào)表控件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘四 雙向鏈表

    C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘四 雙向鏈表

    上節(jié)說(shuō)過(guò)這節(jié)會(huì)講雙向鏈表,環(huán)形鏈表和應(yīng)用舉例,我們開(kāi)始吧?。。。?/div> 2012-11-11
  • c#關(guān)于JWT跨域身份驗(yàn)證的實(shí)現(xiàn)代碼

    c#關(guān)于JWT跨域身份驗(yàn)證的實(shí)現(xiàn)代碼

    這篇文章主要介紹了c#關(guān)于JWT跨域身份驗(yàn)證的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • C#實(shí)現(xiàn)推送釘釘消息的方法示例

    C#實(shí)現(xiàn)推送釘釘消息的方法示例

    這篇文章主要介紹了C#實(shí)現(xiàn)推送釘釘消息的方法,結(jié)合實(shí)例形式分析了C#使用釘釘API實(shí)現(xiàn)消息推送的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-02-02
  • vscode設(shè)置Fira_Code字體及改變編輯器字體、背景顏色的代碼詳解

    vscode設(shè)置Fira_Code字體及改變編輯器字體、背景顏色的代碼詳解

    這篇文章主要介紹了vscode設(shè)置Fira_Code字體及改變編輯器字體、背景顏色,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 如何在C# 中查找或結(jié)束程序域中的主、子進(jìn)程

    如何在C# 中查找或結(jié)束程序域中的主、子進(jìn)程

    這篇文章主要介紹了如何在C# 中查找或結(jié)束程序域中的主、子進(jìn)程,幫助大家更好的理解和使用c#編程語(yǔ)言,感興趣的朋友可以了解下
    2020-11-11
  • C#實(shí)現(xiàn)觀察者模式(Observer?Pattern)的兩種方式

    C#實(shí)現(xiàn)觀察者模式(Observer?Pattern)的兩種方式

    這篇文章介紹了C#實(shí)現(xiàn)觀察者模式(Observer?Pattern)的兩種方式,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • Unity 如何獲取鼠標(biāo)停留位置下的物體

    Unity 如何獲取鼠標(biāo)停留位置下的物體

    這篇文章主要介紹了Unity 如何獲取鼠標(biāo)停留位置下的物體,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • C#使用iTextSharp操作PDF

    C#使用iTextSharp操作PDF

    這篇文章介紹了C#使用iTextSharp操作PDF的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06

最新評(píng)論