自己動手用C#寫控件

2010-08-28 10:49:44來源:西部e網(wǎng)作者:

我平時比較喜歡使用delphi,小生不才,我隨然喜歡delphi,平時開發(fā)(至少現(xiàn)在)多用delphi,但是不怕各位高手笑話,我沒有用delphi寫過控件,雖然原理上知道,但總感覺不知無從下手:L

但是自從接觸了c#,她哪優(yōu)美的身姿(代碼風(fēng)格),風(fēng)騷而不放縱的性格(對面向?qū)ο蟮捏w現(xiàn)比較好,要比delphi強),深深打動了我。經(jīng)過一段時間的操練,我發(fā)現(xiàn)在開發(fā)控件及組件上(別的方面,小生我不敢妄斷),其簡便性真令我耳目一新。怎么樣,試一把吧.J

對了,我的開發(fā)平臺是windows 2000 server+.vs.net 正式版

我所實現(xiàn)的這個控件,是從窗體控件Button繼乘的,能夠?qū)崿F(xiàn)漸變背景,實現(xiàn)圖案及紋理填充文字.

好了,我們開在開始吧

1 首先打個vs.net

2在“文件”菜單中,指向“新建”,然后選擇“項目”以打開“新建項目”對話框。從“C# 項目”列表中選擇“Windows 控件庫”項目模板,然后在“名稱”框中鍵入LinearGradientButtonLib,然后點確定。

3 在解決方案資源管理器中,右擊 UserControl1.cs,并從快捷菜單中選擇“查看代碼”。

4 找到 class 語句 public class UserControl1,將 UserControl1 更改為 LinearGradientButton以更改組件的名稱。找到構(gòu)造函數(shù) public UserControl1(),將它更改為 public LinearGradientButton ()。

5 在 Class 語句,將該控件從 System.Windows.Forms.UserControl 繼承的類型更改為 System.Windows.Forms.Button。這允許繼承的控件繼承 Button 控件的所有功能。

6 在解決方案資源管理器中,單擊 UserControl1.cs,并在“屬性”窗口中,將 FileName 屬性更改為LinearGradientButton.cs.

好了,到現(xiàn)在工作就告一段落了,下面的工作,是向咱們的控件添加屬性了。喝口水,繼續(xù)!

先加上名字空間using System.Drawing.Drawing2D;

1找到 class 語句。在緊靠 { 的后面鍵入下面的代碼:

private Color froColor; //漸變前景色

          private Color backColor;//漸變背景色

          private bool isUseFloat;//是否使用角度轉(zhuǎn)變

          private float angle;    //放置角度

          private LinearGradientMode mode;//設(shè)定漸變的角度

          private HatchStyle hatchStyle; //設(shè)定文本的填充圖案

          private bool isUseStyle;//設(shè)定是否用圖案填充圖案

 

上面這些是我們控件需要的私有域,下面開始為每個私有域做它們對應(yīng)的屬性.在以上代碼的下面,寫入以下代碼:

    [Description("設(shè)定按鈕漸變的前景色"),Category("Appearance")]

         public Color FrontColor

         {

              get

              {

                   return froColor;

              }

              set

              {

                   froColor=value;

              }

         }

          [Description("設(shè)定按鈕漸變的背景色"),Category("Appearance")]

         public Color BackGroundColor

         {

              get

              {

                   return backColor;

              }

              set

              {

                   backColor=value;

              }

         }

          [DefaultValue(false),Description("設(shè)定是否人工設(shè)定角度")]

         public bool UseFloat

         {

              get

              {

                   return isUseFloat;

              }

              set

              {

                   isUseFloat=value;

              }

         }

          [DefaultValue(false),Description("設(shè)定是否使用圖案填充文本")]

         public bool UseStyle

         {

              get

              {

                   return isUseStyle;

              }

              set

              {

                   isUseStyle=value;

              }

         }

          [DefaultValue(0),Description("定義漸變方向的角度,以度為單位從 X 軸順時針測量。 "),Category("Appearance")]

         public float Angle

         {

              get

              {

                   return angle;

              }

              set

              {

                   angle=value;

              }

         }

          [DefaultValue(0),Description("當(dāng)UseFloat設(shè)為false時,設(shè)定漸變方向。 "),Category("Appearance")]

         public LinearGradientMode Mode

         {

              get

              {

                   return mode;

              }

              set

              {

                   mode=value;

              }

         }

          [DefaultValue(false),Description("設(shè)定文本要填充的圖案"),Category("Appearance")]

         public HatchStyle FillStyle

         {

              get

              {

                   return hatchStyle;

              }

              set

              {

                   hatchStyle=value;

              }

         }

好了,我們將控件的屬性設(shè)計好了,下面就要我們寫事件了.


因為我們這個控件是實現(xiàn)背景漸變及文字填充,所以override Paint事件以完成自畫。

為了完成override,現(xiàn)在以下的準(zhǔn)備工作(寫幾個在Paint事件用的著的事件).

//使用角度的方法漸近重畫Button

          private void DrawButtonWithAngle(Graphics dbg)

         {

              LinearGradientBrush brush=new LinearGradientBrush(new Rectangle(0,0,this.Width,this.Height),froColor,backColor,angle);

              dbg.FillRectangle(brush,0,0,this.Width,this.Height);

              brush.Dispose();

         }

         ////使用模式的方法漸近重畫Button

          private void DrawButtonWithMode(Graphics dbg,LinearGradientMode Mode)

         {

              LinearGradientBrush brush=new LinearGradientBrush(new Rectangle(0,0,this.Width,this.Height),froColor,backColor,Mode);

              dbg.FillRectangle(brush,0,0,this.Width,this.Height);

              brush.Dispose();

         }

         //重畫Button的文本(Text),不使用圖案填充

          private void DrawButtonText(Graphics dbg)

         {

              StringFormat format=new StringFormat();

              format.LineAlignment=StringAlignment.Center;

              format.Alignment=StringAlignment.Center;

              dbg.DrawString(this.Text,this.Font,new SolidBrush(this.ForeColor),new Rectangle(0,0,this.Width,this.Height),format);

         }

          //override DrawButtonText函數(shù),使之可以用圖案填充文本

          private  void DrawButtonText(Graphics dbg, HatchStyle hs)

         {

              StringFormat format=new StringFormat();

              format.LineAlignment=StringAlignment.Center;

              format.Alignment=StringAlignment.Center;

              dbg.DrawString(this.Text,this.Font,new HatchBrush(hs,this.ForeColor,Color.Aquamarine),new Rectangle(0,0,this.Width,this.Height),format);

     }

好了,現(xiàn)在開始重寫Paint事件了.

protected override void OnPaint(PaintEventArgs pe)

         {

             

              Graphics g=pe.Graphics;

              base.OnPaint(pe); //調(diào)用父控件的方法

              if(isUseFloat==true) //假如使用角度控制漸變的角度

                   DrawButtonWithAngle(g);

              if(isUseFloat==false)

                   DrawButtonWithMode(g,mode);

              if(isUseStyle==true)//假如使用圖案填充文字

             DrawButtonText(g,hatchStyle);

              else

                   DrawButtonText(g);

     }

好了,現(xiàn)在大功告成了,進行保存,生成。

創(chuàng)建測試項目

1.        在“文件”菜單上,指向“添加項目”,然后單擊“新建項目”以打開“添加新項目”對話框。

2.        選擇“Visual C# 項目”節(jié)點,然后單擊“Windows 應(yīng)用程序”。

3.        在“名稱”框中鍵入 Test。

4.        在解決方案資源管理器中,右擊測試項目的“引用”節(jié)點,然后從快捷菜單中選擇“添加引用”以顯示“添加引用”對話框。

5.        單擊標(biāo)記為“項目”的選項卡。

6.        雙擊 LinearGradientButtonLib 項目,并注意該項目此時出現(xiàn)在“選定的組件”窗格中。

添加引用后,應(yīng)將新控件添加到工具箱。如果您的控件已經(jīng)出現(xiàn)在工具箱中,則應(yīng)該跳過下一節(jié)。

將控件添加到工具箱

1.        右擊工具箱,然后從快捷菜單中選擇“自定義工具箱”。

“自定義工具箱”對話框打開。

2.        選擇“.NET 框架組件”選項卡并單擊“瀏覽”。瀏覽到 LinearGradientButtonLib\bin\debug 文件夾并選擇 LinearGradientButtonLib.dll。

LinearGradientButton 出現(xiàn)在“自定義工具箱”對話框的組件列表中。

3.        在“自定義工具箱”對話框中,單擊 LinearGradientButton 旁的框并關(guān)閉窗口。

LinearGradientButton 被添加到選定的工具箱的選項卡上。

將控件添加到窗體

1.        在解決方案資源管理器中,右擊“Form1.cs”,然后從快捷菜單中選擇“視圖設(shè)計器”。

2.        在工具箱中,向下滾動直到到達標(biāo)記為 LinearGradientButton 的圖標(biāo)。雙擊該圖標(biāo)。

窗體上顯示一個“LinearGradientButton”。

3.        右擊“LinearGradientButton”并從快捷菜單中選擇“屬性”。

4.        在“屬性”窗口中檢查該控件的屬性。注意,它們與標(biāo)準(zhǔn)按鈕公開的屬性相同,不同的是多了我們自己加入的一些屬性

5.        設(shè)定本控件的前景色及背景色,然后可以選擇是否填充文字,是使用角度還是使用系統(tǒng)設(shè)定值進行漸變角度的變化。

6.        從“調(diào)試”菜單中選擇“啟動”。 出現(xiàn) Form1。

關(guān)鍵詞:C#

贊助商鏈接: