对了 , 我的开发平台是 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 。