WinFrom自定义控件–Panel扩展

在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工具栏图标

  1. [ToolboxBitmap(typeof(Panel))]   

  2.     public class PanelEx:Panel   

  3.     {   

  4.         private Color _borderColor = Color.FromArgb(23, 169, 254);   

  5.         private int _radius = 10;   

  6.         private RoundStyle _roundeStyle;   

  7.   

  8.         private const int WM_ERASEBKGND = 0x0014;   

  9.         private const int WM_PAINT = 0xF;   

  10.   

  11.         public PanelEx()   

  12.             : base()   

  13.         {   

  14.         }   

  15.   

  16.         [DefaultValue(typeof(Color), "23, 169, 254"), Description("控件边框颜色")]   

  17.         public Color BorderColor   

  18.         {   

  19.             get { return _borderColor; }   

  20.             set    

  21.             {    

  22.                 _borderColor = value;   

  23.                 base.Invalidate();   

  24.             }   

  25.         }   

  26.   

  27.         [DefaultValue(typeof(int), "10"), Description("圆角弧度大小")]   

  28.         public int Radius   

  29.         {   

  30.             get { return _radius; }   

  31.             set  

  32.             {   

  33.                 _radius = value;   

  34.                 base.Invalidate();   

  35.             }   

  36.         }   

  37.   

  38.         protected override void WndProc(ref Message m)   

  39.         {   

  40.             try  

  41.             {   

  42.                 base.WndProc(ref m);   

  43.                 if (m.Msg == WM_PAINT)   

  44.                 {   

  45.                     if (this.Radius > 0)   

  46.                     {   

  47.                         using (Graphics g = Graphics.FromHwnd(this.Handle))   

  48.                         {   

  49.                             Rectangle r = new Rectangle();   

  50.                             r.Width = this.Width;   

  51.                             r.Height = this.Height;   

  52.                             DrawBorder(g,r,this.Radius);   

  53.                         }   

  54.                     }   

  55.                 }   

  56.             }   

  57.             catch(Exception ex)   

  58.             {   

  59.                 MessageBox.Show(ex.Message);   

  60.             }   

  61.         }   

  62.   

  63.         private void DrawBorder(Graphics g, Rectangle rect, int radius)   

  64.         {   

  65.             rect.Width -= 1;   

  66.             rect.Height -= 1;   

  67.   

  68.             GraphicsPath path = new GraphicsPath();   

  69.   

  70.             path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);   

  71.             path.AddArc(rect.Right - radius , rect.Y, radius, radius, 270, 90);   

  72.             path.AddArc(rect.Right - radius , rect.Bottom - radius , radius, radius, 0, 90);   

  73.             path.AddArc(rect.X, rect.Bottom - radius , radius, radius, 90, 90);   

  74.   

  75.             using (Pen pen = new Pen(this.BorderColor))   

  76.             {   

  77.                 g.DrawPath(pen, path);   

  78.             }   

  79.         }   

  80.     } 


编码完成后,控件效果如下:







评论

© Yue's Blog | Powered by LOFTER