亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

C#時鐘控件的創建與使用

系統 1999 0

C# 時鐘控件的創建與使用

<shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></path><lock v:ext="edit" aspectratio="t"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 162pt; HEIGHT: 173.25pt" type="#_x0000_t75"><imagedata src="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image001.png" o:title="" croptop="14473f" cropbottom="16459f" cropleft="9926f" cropright="29999f"></imagedata></shape>

簡介:效果如圖。

本文假定讀者熟悉基本的 C# 編程過程,但對建立控件的過程不太了解,以一個時鐘控件為例,詳細描述了開發過程,希望對大家有所幫助。

1、 時間變換部分 ----- 只需要比較簡單的數學知識

首先取得當前時間

int hour = dateTime.Hour % 12; // 將小時數( 24 基)轉換成 12 基的

int minute = dateTime.Minute;

int sec = dateTime.Second;

將這些數據轉換成相應的弧度(因為 sin 所)

float hourRadian = hour * 360/12 * PI/180;

float minRadian = minute * 360/60 * PI/180;

float secRadian = sec * 360/60 * PI/180;

根據弧度得出了時針的端點

float hourEndPointX = lengthofHourHand * System.Math.Sin(hourRadian)

float hourEndPointY = lengthofHourHand * System.Math.Cos(hourRadian)

則時針畫法為

    
      
      
      Line(centerX, centerY, hourEndPointX, hourEndPointY)
      

分針、秒針以此類類推。

2、 詳細步驟

<shape id="_x0000_i1026" style="WIDTH: 219pt; HEIGHT: 150pt" type="#_x0000_t75"><imagedata src="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image003.png" o:title="" croptop="15851f" cropbottom="20209f" cropleft="15178f" cropright="15732f"><font face="Times New Roman" size="3"></font></imagedata></shape>

首先創建一個工程命名為 AnalogClockControl. 選擇 Windows Control Library 為模板。

從工程中刪除 usercontrol1.cs 文件,并向其中新增加一個類。

<shape id="_x0000_i1027" style="WIDTH: 219pt; HEIGHT: 246.75pt" type="#_x0000_t75"><imagedata src="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image005.png" o:title="" cropbottom="29412f" cropleft="39843f"><font size="3"></font></imagedata></shape>

向你的控件中拖入一個 timer 控件,將其 Interval 屬性設為 1000 (每 1000 毫秒發出一個 WM_TIMER 消息)。

下面開始編程 , 類的架構:

class AnalogClock : System.Windows.Forms.UserControl

{

const float PI=<chmetcnv unitname="F" sourcevalue="3.141592654" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">3.141592654F</chmetcnv>;

DateTime dateTime;

float fRadius;

float fCenterX;

float fCenterY;

float fCenterCircleRadius;

float fHourLength;

float fMinLength;

float fSecLength;

float fHourThickness;

float fMinThickness;

float fSecThickness;

bool bDraw5MinuteTicks=true;

bool bDraw1MinuteTicks=true;

float fTicksThickness=1;

Color hrColor=Color.DarkMagenta ;

Color minColor=Color.Green ;

Color secColor=Color.Red ;

Color circleColor=Color.Red;

Color ticksColor=Color.Black;

...

...

為了確??丶怀醮渭虞d及被改變大小時都能正常顯示時間,我們有必要在 Load Resize 事件發生時予以響應。

程序如下:

private void AnalogClock_Load(object sender, System.EventArgs e)

{

dateTime=DateTime.Now;

this.AnalogClock_Resize(sender,e);

}

private void AnalogClock_Resize(object sender, System.EventArgs e)

{

this.Width = this.Height;

this.fRadius = this.Height/2;

this.fCenterX = this.ClientSize.Width/2;

this.fCenterY = this.ClientSize.Height/2;

this.fHourLength = (float)this.Height/3/<chmetcnv unitname="F" sourcevalue="1.65" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.65F</chmetcnv>;

this.fMinLength = (float)this.Height/3/<chmetcnv unitname="F" sourcevalue="1.2" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.20F</chmetcnv>;

this.fSecLength = (float)this.Height/3/<chmetcnv unitname="F" sourcevalue="1.15" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.15F</chmetcnv>;

this.fHourThickness = (float)this.Height/100;

this.fMinThickness = (float)this.Height/150;

this.fSecThickness = (float)this.Height/200;

this.fCenterCircleRadius = this.Height/50;

this.Refresh();

}

為完成時鐘效果,程序應該每秒鐘刷新一次時、分、秒針的位置。雙擊 timer 控件,在其響應函數中加入如下代碼:

private void timer1_Tick(object sender, System.EventArgs e)

{

this.dateTime=DateTime.Now;

this.Refresh();

}

下面來寫一下兩個與圖形有關的函數, DrawLine() DrawPolygon(), 它們的作用是在窗體上畫出秒針與時分針。

代碼如下:

private void DrawLine(float fThickness, float fLength, Color color,

float fRadians, System.Windows.Forms.PaintEventArgs e)

{

e.Graphics.DrawLine(new Pen( color, fThickness ),

fCenterX - (float)(fLength/9*System.Math.Sin(fRadians)),

fCenterY + (float)(fLength/9*System.Math.Cos(fRadians)),

fCenterX + (float)(fLength*System.Math.Sin(fRadians)),

fCenterY - (float)(fLength*System.Math.Cos(fRadians)));

}

private void DrawPolygon(float fThickness, float fLength, Color color,

float fRadians, System.Windows.Forms.PaintEventArgs e)

{

PointF A=new PointF( (float)(fCenterX+

fThickness*2*System.Math.Sin(fRadians+PI/2)),

(float)(fCenterY -

fThickness*2*System.Math.Cos(fRadians+PI/2)) );

PointF B=new PointF( (float)(fCenterX+

fThickness*2*System.Math.Sin(fRadians-PI/2)),

(float)(fCenterY -

fThickness*2*System.Math.Cos(fRadians-PI/2)) );

PointF C=new PointF( (float)(fCenterX+

fLength*System.Math.Sin(fRadians)),

(float) (fCenterY -

fLength*System.Math.Cos(fRadians)) );

PointF D=new PointF( (float)(fCenterX-

fThickness*4*System.Math.Sin(fRadians)),

(float)(fCenterY +

fThickness*4*System.Math.Cos(fRadians) ));

PointF[] points={A,D,B,C};

e.Graphics.FillPolygon( new SolidBrush(color), points );

}

為了使控件的用戶能夠控制這個時鐘控件的啟停,我們在其中加入另外兩個函數。

public void Start()

{

timer1.Enabled=true;

this.Refresh();

}

public void Stop()

{

timer1.Enabled=false;

}

控件對于 Paint 消息的響應函數是這個控件中最核心的程序,完成了時間提取、弧度轉換及畫指針這一系列活動。

代碼如下:

private void AnalogClock_Paint(object sender,

System.Windows.Forms.PaintEventArgs e)

{

float fRadHr=(dateTime.Hour%12+dateTime.Minute/<chmetcnv unitname="F" sourcevalue="60" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">60F</chmetcnv>) *30*PI/180;

float fRadMin=(dateTime.Minute)*6*PI/180;

float fRadSec=(dateTime.Second)*6*PI/180;

DrawPolygon(this.fHourThickness,

this.fHourLength, hrColor, fRadHr, e);

DrawPolygon(this.fMinThickness,

this.fMinLength, minColor, fRadMin, e);

DrawLine(this.fSecThickness,

this.fSecLength, secColor, fRadSec, e);

for(int i=0;i<60;i++)

{

if ( this.bDraw5MinuteTicks==true && i%5==0 )

// Draw 5 minute ticks

{

e.Graphics.DrawLine( new Pen( ticksColor, fTicksThickness ),

fCenterX +

(float)( this.fRadius/<chmetcnv unitname="F" sourcevalue="1.5" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.50F</chmetcnv>*System.Math.Sin(i*6*PI/180) ),

fCenterY -

(float)( this.fRadius/<chmetcnv unitname="F" sourcevalue="1.5" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.50F</chmetcnv>*System.Math.Cos(i*6*PI/180) ),

fCenterX +

(float)( this.fRadius/<chmetcnv unitname="F" sourcevalue="1.65" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.65F</chmetcnv>*System.Math.Sin(i*6*PI/180) ),

fCenterY -

(float)( this.fRadius/<chmetcnv unitname="F" sourcevalue="1.65" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.65F</chmetcnv>*System.Math.Cos(i*6*PI/180)) );

}

else if ( this.bDraw1MinuteTicks==true ) // draw 1 minute ticks

{

e.Graphics.DrawLine( new Pen( ticksColor, fTicksThickness ),

fCenterX +

(float) ( this.fRadius/<chmetcnv unitname="F" sourcevalue="1.5" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.50F</chmetcnv>*System.Math.Sin(i*6*PI/180) ),

fCenterY -

(float) ( this.fRadius/<chmetcnv unitname="F" sourcevalue="1.5" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.50F</chmetcnv>*System.Math.Cos(i*6*PI/180) ),

fCenterX +

(float) ( this.fRadius/<chmetcnv unitname="F" sourcevalue="1.55" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.55F</chmetcnv>*System.Math.Sin(i*6*PI/180) ),

fCenterY -

(float) ( this.fRadius/<chmetcnv unitname="F" sourcevalue="1.55" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.55F</chmetcnv>*System.Math.Cos(i*6*PI/180) ) );

}

}

//draw circle at center

e.Graphics.FillEllipse( new SolidBrush( circleColor ),

fCenterX-fCenterCircleRadius/2,

fCenterY-fCenterCircleRadius/2,

fCenterCircleRadius, fCenterCircleRadius);

}

最后,還須為此控件設置個三個屬性,使用戶可以更改其顏色。

代碼如下:

public Color HourHandColor

{

get { return this.hrColor; }

set { this.hrColor=value; }

}

public Color MinuteHandColor

{

get { return this.minColor; }

set { this.minColor=value; }

}

public Color SecondHandColor

{

get { return this.secColor; }

set { this.secColor=value;

this.circleColor=value; }

}

3、 在其它工程中應用此控件

在對上述控件工程進行正確編譯后,得到一個 DLL 文件,可以將其拷入其它任意工程文件夾中,右鍵工具欄,選 choose Items…

<shape id="_x0000_i1028" style="WIDTH: 157.5pt; HEIGHT: 228pt" type="#_x0000_t75"><imagedata src="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image007.png" o:title="" cropbottom="36222f" cropright="49314f"><font face="Times New Roman" size="3"></font></imagedata></shape>

在其后出現的對話框中選 browse 鈕,選擇相應的 dll, 相應的時鐘控件即出現在工具欄中,我們可以對它進行任意拖動。 Enjoy it !

譯自:

例程下載:

C#時鐘控件的創建與使用


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 今天的恋爱未删减在线观看 | 久久这里只有精品免费看青草 | 久久99热成人精品国产 | 天天噜夜夜操 | 一级爱爱 | 青青青视频精品中文字幕 | 日韩一区二区三区不卡视频 | 精品中文字幕在线观看 | 一级毛片成人午夜 | 日韩欧美一级毛片在线 | 人人澡人人干 | 日本免费在线视频 | 5g影院天天爽 | 自拍视频精品 | 亚洲图片二区 | 国产成人精品在视频 | 国产区综合另类亚洲欧美 | 日本精品一区二区三区在线观看 | 欧美日韩国产另类一区二区三区 | 亚洲成a人v | 91福利社在线观看 | 亚洲精品第一国产麻豆 | 99免费视频 | 国产精品福利视频一区二区三区 | 97视频在线视频 | 日本精品一区二区三区在线观看 | 欧洲精品视频在线观看 | 中文字幕一区二区在线观看 | 呦系列视频一区二区三区 | 国产精品亚洲二区在线 | 国产精品国产精品国产专区不卡 | 免费观看黄色a一级录像 | 鲁丝一区 | 久久成人18 | 国产精品亚洲精品观看不卡 | 国产成人久久精品二区三区 | 免费一级欧美毛片 | 久久中文字幕日韩精品 | 成人a毛片在线看免费全部播放 | 久久亚洲国产欧洲精品一 | 久久精品综合国产二区 |