自己動手用c#寫控件
willsound([email protected])
關鍵詞
c#,.net,控件,gdi+
我平時比較喜歡使用delphi,小生不才,我隨然喜歡delphi,平時開發(至少現在)多用delphi,但是不怕各位高手笑話,我沒有用delphi寫過控件,雖然原理上知道,但總感覺不知無從下手:l
但是自從接觸了c#,她哪優美的身姿(代碼風格),風騷而不放縱的性格(對面向對象的體現比較好,要比delphi強),深深打動了我。經過一段時間的操練,我發現在開發控件及組件上(別的方面,小生我不敢妄斷),其簡便性真令我耳目一新。怎么樣,試一把吧.j
對了,我的開發平臺是windows 2000 server+.vs.net 正式版
我所實現的這個控件,是從窗體控件button繼乘的,能夠實現漸變背景,實現圖案及紋理填充文字.
好了,我們開在開始吧
1 首先打個vs.net
2在“文件”菜單中,指向“新建”,然后選擇“項目”以打開“新建項目”對話框。從“c# 項目”列表中選擇“windows 控件庫”項目模板,然后在“名稱”框中鍵入lineargradientbuttonlib,然后點確定。
3 在解決方案資源管理器中,右擊 usercontrol1.cs,并從快捷菜單中選擇“查看代碼”。
4 找到 class 語句 public class usercontrol1,將 usercontrol1 更改為 lineargradientbutton以更改組件的名稱。找到構造函數 public usercontrol1(),將它更改為 public lineargradientbutton ()。
5 在 class 語句,將該控件從 system.windows.forms.usercontrol 繼承的類型更改為 system.windows.forms.button。這允許繼承的控件繼承 button 控件的所有功能。
6 在解決方案資源管理器中,單擊 usercontrol1.cs,并在“屬性”窗口中,將 filename 屬性更改為lineargradientbutton.cs.
好了,到現在工作就告一段落了,下面的工作,是向咱們的控件添加屬性了。喝口水,繼續!
先加上名字空間using system.drawing.drawing2d;
1找到 class 語句。在緊靠 { 的后面鍵入下面的代碼:
private color frocolor; //漸變前景色
private color backcolor;//漸變背景色
private bool isusefloat;//是否使用角度轉變
private float angle; //放置角度
private lineargradientmode mode;//設定漸變的角度
private hatchstyle hatchstyle; //設定文本的填充圖案
private bool isusestyle;//設定是否用圖案填充圖案
上面這些是我們控件需要的私有域,下面開始為每個私有域做它們對應的屬性.在以上代碼的下面,寫入以下代碼:
[description("設定按鈕漸變的前景色"),category("appearance")]
public color frontcolor
{
get
{
return frocolor;
}
set
{
frocolor=value;
}
}
[description("設定按鈕漸變的背景色"),category("appearance")]
public color backgroundcolor
{
get
{
return backcolor;
}
set
{
backcolor=value;
}
}
[defaultvalue(false),description("設定是否人工設定角度")]
public bool usefloat
{
get
{
return isusefloat;
}
set
{
isusefloat=value;
}
}
[defaultvalue(false),description("設定是否使用圖案填充文本")]
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("當usefloat設為false時,設定漸變方向。 "),category("appearance")]
public lineargradientmode mode
{
get
{
return mode;
}
set
{
mode=value;
}
}
[defaultvalue(false),description("設定文本要填充的圖案"),category("appearance")]
public hatchstyle fillstyle
{
get
{
return hatchstyle;
}
set
{
hatchstyle=value;
}
}
好了,我們將控件的屬性設計好了,下面就要我們寫事件了.
因為我們這個控件是實現背景漸變及文字填充,所以override paint事件以完成自畫。
為了完成override,現在以下的準備工作(寫幾個在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函數,使之可以用圖案填充文本
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);
}
好了,現在開始重寫paint事件了.
protected override void onpaint(painteventargs pe)
{
graphics g=pe.graphics;
base.onpaint(pe); //調用父控件的方法
if(isusefloat==true) //假如使用角度控制漸變的角度
drawbuttonwithangle(g);
if(isusefloat==false)
drawbuttonwithmode(g,mode);
if(isusestyle==true)//假如使用圖案填充文字
drawbuttontext(g,hatchstyle);
else
drawbuttontext(g);
}
好了,現在大功告成了,進行保存,生成。
創建測試項目
1. 在“文件”菜單上,指向“添加項目”,然后單擊“新建項目”以打開“添加新項目”對話框。
2. 選擇“visual c# 項目”節點,然后單擊“windows 應用程序”。
3. 在“名稱”框中鍵入 test。
4. 在解決方案資源管理器中,右擊測試項目的“引用”節點,然后從快捷菜單中選擇“添加引用”以顯示“添加引用”對話框。
5. 單擊標記為“項目”的選項卡。
6. 雙擊 lineargradientbuttonlib 項目,并注意該項目此時出現在“選定的組件”窗格中。
添加引用后,應將新控件添加到工具箱。如果您的控件已經出現在工具箱中,則應該跳過下一節。
將控件添加到工具箱
1. 右擊工具箱,然后從快捷菜單中選擇“自定義工具箱”。
“自定義工具箱”對話框打開。
2. 選擇“.net 框架組件”選項卡并單擊“瀏覽”。瀏覽到 lineargradientbuttonlib/bin/debug 文件夾并選擇 lineargradientbuttonlib.dll。
lineargradientbutton 出現在“自定義工具箱”對話框的組件列表中。
3. 在“自定義工具箱”對話框中,單擊 lineargradientbutton 旁的框并關閉窗口。
lineargradientbutton 被添加到選定的工具箱的選項卡上。
將控件添加到窗體
1. 在解決方案資源管理器中,右擊“form1.cs”,然后從快捷菜單中選擇“視圖設計器”。
2. 在工具箱中,向下滾動直到到達標記為 lineargradientbutton 的圖標。雙擊該圖標。
窗體上顯示一個“lineargradientbutton”。
3. 右擊“lineargradientbutton”并從快捷菜單中選擇“屬性”。
4. 在“屬性”窗口中檢查該控件的屬性。注意,它們與標準按鈕公開的屬性相同,不同的是多了我們自己加入的一些屬性
5. 設定本控件的前景色及背景色,然后可以選擇是否填充文字,是使用角度還是使用系統設定值進行漸變角度的變化。
6. 從“調試”菜單中選擇“啟動”。 出現 form1。
誰如果需要源碼的話,請給我發信.