- 编译 dll 文件。
- 把 dll 文件拷贝到工作文件夹下面。
- 在右边文件夹的【引用】上右击,选择【添加引用】,在【com】中浏览 选择,即可添加。也可以在菜单【项目】/【添加引用】上添加。
- 如果需要修改,需要把管理员目录下的 dll 文件删除,再次生成。
创建自定义控件 定义控件类
定义从 System.Windows.Forms.Control 派生的类。public class FirstControl:Control{...}
定义属性
以下代码片段定义名为 TextAlignment 的属性,FirstControl 使用该属性来
定义从 Control 继承的 Text 属性的显示格式。
// ContentAlignment is an enumeration defined in the System.Drawing
// namespace that specifies the alignment of content on a drawing
// surface.
private ContentAlignment alignment = ContentAlignment.MiddleLeft;
public ContentAlignment TextAlignment {
get {
return alignment;
}
set {
alignment = value;
// The Invalidate method invokes the OnPaint method described
// in step 3.
Invalidate();
}
}
在设置更改控件外观显示的属性时,必须调用 Invalidate 方法来重新绘制该控件。Invalidate 是在基类 Control 中定义的。