日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

C#時(shí)鐘控件的創(chuàng)建與使用

系統(tǒng) 2244 0

C# 時(shí)鐘控件的創(chuàng)建與使用

<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>

簡(jiǎn)介:效果如圖。

本文假定讀者熟悉基本的 C# 編程過(guò)程,但對(duì)建立控件的過(guò)程不太了解,以一個(gè)時(shí)鐘控件為例,詳細(xì)描述了開(kāi)發(fā)過(guò)程,希望對(duì)大家有所幫助。

1、 時(shí)間變換部分 ----- 只需要比較簡(jiǎn)單的數(shù)學(xué)知識(shí)

首先取得當(dāng)前時(shí)間

int hour = dateTime.Hour % 12; // 將小時(shí)數(shù)( 24 基)轉(zhuǎn)換成 12 基的

int minute = dateTime.Minute;

int sec = dateTime.Second;

將這些數(shù)據(jù)轉(zhuǎn)換成相應(yīng)的弧度(因?yàn)? sin 所)

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

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

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

根據(jù)弧度得出了時(shí)針的端點(diǎn)

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

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

則時(shí)針畫(huà)法為

    
      
      
      Line(centerX, centerY, hourEndPointX, hourEndPointY)
      

分針、秒針以此類類推。

2、 詳細(xì)步驟

<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>

首先創(chuàng)建一個(gè)工程命名為 AnalogClockControl. 選擇 Windows Control Library 為模板。

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

<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>

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

下面開(kāi)始編程 , 類的架構(gòu):

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;

...

...

為了確保控件被初次加載及被改變大小時(shí)都能正常顯示時(shí)間,我們有必要在 Load Resize 事件發(fā)生時(shí)予以響應(yīng)。

程序如下:

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();

}

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

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

{

this.dateTime=DateTime.Now;

this.Refresh();

}

下面來(lái)寫(xiě)一下兩個(gè)與圖形有關(guān)的函數(shù), DrawLine() DrawPolygon(), 它們的作用是在窗體上畫(huà)出秒針與時(shí)分針。

代碼如下:

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 );

}

為了使控件的用戶能夠控制這個(gè)時(shí)鐘控件的啟停,我們?cè)谄渲屑尤肓硗鈨蓚€(gè)函數(shù)。

public void Start()

{

timer1.Enabled=true;

this.Refresh();

}

public void Stop()

{

timer1.Enabled=false;

}

控件對(duì)于 Paint 消息的響應(yīng)函數(shù)是這個(gè)控件中最核心的程序,完成了時(shí)間提取、弧度轉(zhuǎn)換及畫(huà)指針這一系列活動(dòng)。

代碼如下:

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);

}

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

代碼如下:

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、 在其它工程中應(yīng)用此控件

在對(duì)上述控件工程進(jìn)行正確編譯后,得到一個(gè) 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>

在其后出現(xiàn)的對(duì)話框中選 browse 鈕,選擇相應(yīng)的 dll, 相應(yīng)的時(shí)鐘控件即出現(xiàn)在工具欄中,我們可以對(duì)它進(jìn)行任意拖動(dòng)。 Enjoy it !

譯自:

例程下載:

C#時(shí)鐘控件的創(chuàng)建與使用


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 历史| 惠来县| 黄石市| 修水县| 丰原市| 五寨县| 农安县| 新竹县| 宿州市| 巴彦淖尔市| 临西县| 平罗县| 灵台县| 合作市| 上蔡县| 调兵山市| 晋城| 安龙县| 新郑市| 河曲县| 余姚市| 射洪县| 界首市| 开阳县| 乐昌市| 曲周县| 卫辉市| 庄浪县| 定边县| 天气| 扎兰屯市| 丹江口市| 陆丰市| 崇义县| 栖霞市| 利川市| 仙居县| 开鲁县| 繁峙县| 桂阳县| 枣庄市|