WinForm控件开发【转载】
http://www.cnblogs.com/luqingfei/archive/2007/03/1...
用VS建两个项目(CustomControlSample, TestControl), 项目类型分别是类库(不是内裤!!!), Windows应用程序.
using System.Windows.Forms; using System.Drawing; namespace CustomControlSample {public class FirstControl : Control
{private int simpleField;
public int SimpleProperty
{ get { return simpleField; } set { simpleField = value; }}
protected override void OnPaint(PaintEventArgs e)
{ base.OnPaint(e);e.Graphics.DrawRectangle(Pens.Red, new Rectangle(Point.Empty, new Size(Width - 1, Height - 1)));
}
}
}
写完后 生成 DLL 文件
然后在 WINFORM 窗体 引用这个DLL 文件
只有一个属性的控件!!!
拖到windows 窗体上:
在属性浏览器中可以看到该控件的唯一属性:
一个最最简单的Dot net winform控件做好了.
在这里用到 属性(Attribute), 属性(Attribute)与属性(Property)不同, 前者是用来描述编程元素的,后都是用来描述对象的. 简单地说, 错了不要骂我!!!
将上代码稍稍改动了一点:
using System.ComponentModel; using System.Windows.Forms; using System.Drawing; namespace CustomControlSample {public class FirstControl : Control
{private int simpleField;
[Category("我是属性,我怕谁!")] [Description("我是属性,故我在(属性浏览器中)!")]public int SimpleProperty
{ get { return simpleField; } set { simpleField = value; }}
protected override void OnPaint(PaintEventArgs e)
{ base.OnPaint(e);e.Graphics.DrawRectangle(Pens.Red, new Rectangle(Point.Empty, new Size(Width - 1, Height - 1)));
}
}
}
