三步绘制控件:
- 定义属性;
- 画布重绘;
- 添加事件;
如何自定义一个转换键(Toggle)?
1.主要技能:
- 如何自定义属性;
- 画布重绘的一般格式;
- 控件的事件触发过程;
- 技能扩展
- 转换按钮使能时添加二次确认弹框?
- 在From窗体中应用控件时,点击事件没有触发?
- 属性名称在控件属性树中的排列如何定义?
- 添加一个字体更改属性?
2.定义属性
- 字体(Font)
- 颜色(Color)
- 字符串(String)
- 枚举(Enum)
- 属性说明 [Browsable(true)]
#region 属性——选中、背景颜色、文本、样式、滑块颜色
private Font displayFont = new Font("Segoe UI", 12);
[Browsable(true)]
[Category("布局_G")]
[Description("字体格式")]
public Font DisplayFont
{
get {
return displayFont; }
set
{
if (value != null)
{
displayFont = value;
this.Invalidate(); // Trigger redraw
}
}
}
private bool _checked = false;
[Browsable(true)] //说明(需放在属性前边):是否选中
[Category("布局_G")]
[Description("是否选中")]
public bool Checked
{
get {
return _checked; }
set
{
_checked = value;
this.Invalidate();
//激活触发事件
this.MouseDown_G?.Invoke(this, null);
}
}
private string falseText = "关闭";
[Browsable(true)] //说明:文本关闭
[Category("布局_G")]
[Description("文本关闭")]
public string FalseText
{
get {
return falseText; }
set {
falseText = value; this.Invalidate(); }
}
//样式切换
public enum SwType
{
Ellipse, //椭圆
Rectangle, //矩形
}
private SwType switchType = SwType.Ellipse;
[Browsable(true)] //说明:切换样式
[Category("布局_G")]
[Description("切换样式")]
public SwType SwitchType
{
get {
return switchType; }
set {
switchType = value; this.Invalidate(); }