C#/ASP.NET/HTML/CSS/JS/JQUERY//PS/CDR/AE/EDIUS
在WinFrom中用户自定义控件大概有三种形式:
(1)、 自定义控件 :通过继承Control类创建一个新的用户控件。Control 类提供控件所需的所有基本功能(包括鼠标和键盘处理事件),但不提供控件特定的功能或图形界面。 在开发过程中,需要完成Control类的OnPaint事件代码,或者重写WndProc方法,需要使用到GDI+和Win32 API编程。
(2)、扩展控件 :在现有的控件基础上派生出新的控件,通过添加自定义属性、方法或其他功能来扩展原有控件。可以重写基类的OnPaint方法,或者WndProc方法。
(3)、复合控件 :将目前现有的控件组合到一起形成一个新的控件。复合控件从UserControl 类派生,基类 UserControl 为子控件提供了键盘路由并使子控件可以作为一个组进行相关工作。
本节示例,在WinForm窗体控件Panel的基础上扩展一个新的控件。新控件需要实现:
1、Panel边框颜色可以自定义设置;
2、Panel可以设置一个圆角弧度。
我采用扩展控件的方式,来实现我所需要的控件,具体编码操作步骤如下:
I、在VS中新建一个项目,添加一个类文件取名为PanelEx继承Panel控件,如:public classPanelEx:Panel
II、添加所需要的扩展控件属性,边框颜色-BorderColor、边框圆角-Radius
III、重写WndProc方法
IV、设置控件在Visual Studio IDE工具栏图标
[ToolboxBitmap(typeof(Panel))]
public class PanelEx:Panel
{
private Color _borderColor = Color.FromArgb(23, 169, 254);
private int _radius = 10;
private RoundStyle _roundeStyle;
private const int WM_ERASEBKGND = 0x0014;
private const int WM_PAINT = 0xF;
public PanelEx()
: base()
{
}
[DefaultValue(typeof(Color), "23, 169, 254"), Description("控件边框颜色")]
public Color BorderColor
{
get { return _borderColor; }
set
{
_borderColor = value;
base.Invalidate();
}
}
[DefaultValue(typeof(int), "10"), Description("圆角弧度大小")]
public int Radius
{
get { return _radius; }
set
{
_radius = value;
base.Invalidate();
}
}
protected override void WndProc(ref Message m)
{
try
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
if (this.Radius > 0)
{
using (Graphics g = Graphics.FromHwnd(this.Handle))
{
Rectangle r = new Rectangle();
r.Width = this.Width;
r.Height = this.Height;
DrawBorder(g,r,this.Radius);
}
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void DrawBorder(Graphics g, Rectangle rect, int radius)
{
rect.Width -= 1;
rect.Height -= 1;
GraphicsPath path = new GraphicsPath();
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddArc(rect.Right - radius , rect.Y, radius, radius, 270, 90);
path.AddArc(rect.Right - radius , rect.Bottom - radius , radius, radius, 0, 90);
path.AddArc(rect.X, rect.Bottom - radius , radius, radius, 90, 90);
using (Pen pen = new Pen(this.BorderColor))
{
g.DrawPath(pen, path);
}
}
}
编码完成后,控件效果如下:
© Yue's Blog | Powered by LOFTER