對了 , 我的開發平臺是 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. 雙擊 LinearGradientButton Lib 項目,並注意該項目此時出現在 “ 選定的組件 ” 窗格中。
添加引用後,應將新控件添加到工具箱。如果您的控件已經出現在工具箱中,則應該跳過下一節。
將控件添加到工具箱
1. 右擊工具箱,然後從快捷菜單中選擇 “ 自定義工具箱 ” 。
“自定義工具箱”對話框打開。
2. 選擇 “.NET 框架組件 ” 選項卡並單擊 “ 瀏覽 ” 。瀏覽到 LinearGradientButton Lib/bin/debug 文件夾並選擇 LinearGradientButtonLib .dll 。
LinearGradientButton 出現在“自定義工具箱”對話框的組件列表中。
3. 在 “ 自定義工具箱 ” 對話框中,單擊 LinearGradientButton 旁的框並關閉窗口。
LinearGradientButton 被添加到選定的工具箱的選項卡上。
將控件添加到窗體
1. 在解決方案資源管理器中,右擊 “Form1.cs” ,然後從快捷菜單中選擇 “ 視圖設計器 ” 。
2. 在工具箱中,向下滾動直到到達標記爲 LinearGradientButton 的圖標。雙擊該圖標。
窗體上顯示一個 “ LinearGradientButton ”。
3. 右擊 “ LinearGradientButton ” 並從快捷菜單中選擇 “ 屬性 ” 。
4. 在 “ 屬性 ” 窗口中檢查該控件的屬性。注意,它們與標準按鈕公開的屬性相同,不同的是多了我們自己加入的一些屬性
5. 設定本控件的前景色及背景色,然後可以選擇是否填充文字,是使用角度還是使用系統設定值進行漸變角度的變化。
6. 從 “ 調試 ” 菜單中選擇 “ 啓動 ” 。 出現 Form1 。