主题:[转帖]过节7天,大家玩得开心吧~
为避免无聊灌水之嫌,还是转贴一篇《如何用C#做控件》的文章咯~
其实还是想和大家寒暄一下,哈哈~ ^_^
以创建一个Windows窗体控件为例,介绍C#语言中的继承、属性、重载、文档化、设计器、签名等概念和应用。
步骤:
1. 建立一个空解决方案;
Windows Forms Divider Panel,
2. 然后建立一个Windows控件库项目;
DividerPanel
3. 添加一个类;
DividerPanel.cs
4. 将该类从已有的控件类中继承;
public class DividerPanel : System.Windows.Forms.Panel
{
}
5. 增加属性和访问器;
新增两个属性:
private System.Windows.Forms.Border3DSide borderSide;
private System.Windows.Forms.Border3DStyle border3DStyle;
对于私有变量,使用访问器将其暴露出来:
public System.Windows.Forms.Border3DSide BorderSide
{
get { return this.borderSide; }
set
{
if( this.borderSide != value )
{
this.borderSide = value;
this.Invalidate();
}
}
}
// good
private System.Windows.Forms.Border3DSide borderSide;
// bad
private Border3DSide borderSide;
6. 在构造函数中设置初始值:
public DividerPanel()
{
// Set default values for our control's properties
this.borderSide = System.Windows.Forms.Border3DSide.All;
this.border3DStyle = System.Windows.Forms.Border3DStyle.Etched;
}
7. 法重载继承的方法
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
// allow normal control painting to occur first
base.OnPaint(e);
// add our custom border
System.Windows.Forms.ControlPaint.DrawBorder3D (
e.Graphics,
this.ClientRectangle,
this.border3DStyle,
this.borderSide );
}
8. 增加属性描述和文档化支持
/// <summary>
/// Specifies the sides of the panel to apply a three-dimensional border to.
/// </summary>
[Bindable(true), Category("Border Options"),
DefaultValue(System.Windows.Forms.Border3DSide.All),
Description("Specifies the sides of the panel to apply a
three-dimensional border to.")]
public System.Windows.Forms.Border3DSide BorderSide
{
get { return this.borderSide; }
set
{
if( this.borderSide != value )
{
this.borderSide = value;
this.Invalidate();
}
}
}
9. 增加工具箱支持
增加bitmap图(DividerPanel.bmp)作为显示在工具箱中的图标,存为嵌入资源;
[ToolboxItem(true)]
[ToolboxBitmap(typeof(DividerPanel))]
public class DividerPanel : System.Windows.Forms.Panel
{
}
10. 增加设计器类
从该控件中去除BorderStyle属性:
添加System.Designer.dll引用;
添加类:DividerPanelDesigner.cs;
将新类从设计器继承:
public class DividerPanelDesigner :
System.Windows.Forms.Design.ScrollableControlDesigner
{
}
重载:
protected override void PreFilterProperties(
System.Collections.IDictionary properties)
{
properties.Remove("BorderStyle");
}
增加设计器属性:
[ToolboxItem(true)]
[ToolboxBitmap(typeof(DividerPanel))]
[DesignerAttribute(typeof(DividerPanelDesigner))]
public class DividerPanel : System.Windows.Forms.Panel
{
}
11. Assembly属性和签名
[assembly: AssemblyTitle("Divider Panel")]
[assembly: AssemblyDescription(
"A Panel control with selectable border appearance")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("CodeShack")]
[assembly: AssemblyProduct("Divider Panel Tutorial")]
[assembly: AssemblyCopyright("Copyright (c) 2003-2004 CodeShack")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyKeyFile("..\\..\\..\\DividerPanel.snk")]
sn -k [outputfile].snk
12. 使用该控件
添加引用;
拖拽控件到窗体
其实还是想和大家寒暄一下,哈哈~ ^_^
以创建一个Windows窗体控件为例,介绍C#语言中的继承、属性、重载、文档化、设计器、签名等概念和应用。
步骤:
1. 建立一个空解决方案;
Windows Forms Divider Panel,
2. 然后建立一个Windows控件库项目;
DividerPanel
3. 添加一个类;
DividerPanel.cs
4. 将该类从已有的控件类中继承;
public class DividerPanel : System.Windows.Forms.Panel
{
}
5. 增加属性和访问器;
新增两个属性:
private System.Windows.Forms.Border3DSide borderSide;
private System.Windows.Forms.Border3DStyle border3DStyle;
对于私有变量,使用访问器将其暴露出来:
public System.Windows.Forms.Border3DSide BorderSide
{
get { return this.borderSide; }
set
{
if( this.borderSide != value )
{
this.borderSide = value;
this.Invalidate();
}
}
}
// good
private System.Windows.Forms.Border3DSide borderSide;
// bad
private Border3DSide borderSide;
6. 在构造函数中设置初始值:
public DividerPanel()
{
// Set default values for our control's properties
this.borderSide = System.Windows.Forms.Border3DSide.All;
this.border3DStyle = System.Windows.Forms.Border3DStyle.Etched;
}
7. 法重载继承的方法
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
// allow normal control painting to occur first
base.OnPaint(e);
// add our custom border
System.Windows.Forms.ControlPaint.DrawBorder3D (
e.Graphics,
this.ClientRectangle,
this.border3DStyle,
this.borderSide );
}
8. 增加属性描述和文档化支持
/// <summary>
/// Specifies the sides of the panel to apply a three-dimensional border to.
/// </summary>
[Bindable(true), Category("Border Options"),
DefaultValue(System.Windows.Forms.Border3DSide.All),
Description("Specifies the sides of the panel to apply a
three-dimensional border to.")]
public System.Windows.Forms.Border3DSide BorderSide
{
get { return this.borderSide; }
set
{
if( this.borderSide != value )
{
this.borderSide = value;
this.Invalidate();
}
}
}
9. 增加工具箱支持
增加bitmap图(DividerPanel.bmp)作为显示在工具箱中的图标,存为嵌入资源;
[ToolboxItem(true)]
[ToolboxBitmap(typeof(DividerPanel))]
public class DividerPanel : System.Windows.Forms.Panel
{
}
10. 增加设计器类
从该控件中去除BorderStyle属性:
添加System.Designer.dll引用;
添加类:DividerPanelDesigner.cs;
将新类从设计器继承:
public class DividerPanelDesigner :
System.Windows.Forms.Design.ScrollableControlDesigner
{
}
重载:
protected override void PreFilterProperties(
System.Collections.IDictionary properties)
{
properties.Remove("BorderStyle");
}
增加设计器属性:
[ToolboxItem(true)]
[ToolboxBitmap(typeof(DividerPanel))]
[DesignerAttribute(typeof(DividerPanelDesigner))]
public class DividerPanel : System.Windows.Forms.Panel
{
}
11. Assembly属性和签名
[assembly: AssemblyTitle("Divider Panel")]
[assembly: AssemblyDescription(
"A Panel control with selectable border appearance")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("CodeShack")]
[assembly: AssemblyProduct("Divider Panel Tutorial")]
[assembly: AssemblyCopyright("Copyright (c) 2003-2004 CodeShack")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyKeyFile("..\\..\\..\\DividerPanel.snk")]
sn -k [outputfile].snk
12. 使用该控件
添加引用;
拖拽控件到窗体