Bootstrap

C#自定义控件(自定义属性、自定义事件)

1、创建类库项目

在当前解决方案中添加新项目类库。

注意选择“.NET Framework

创建完成后,删除默认的Class1类。

2、添加项目图片

在类库的debug中添加一个文件夹image,并复制项目需要的图片。

3、把全部项目拖放到资源文件中

选中所有图片,然后拖放到[资源],方法是右击类库项目,选择[属性]—>[资源]—>单击创建链接。

选中images里面所有图片,拖放到右边,即可添加到Resources资源文件中:

4、添加用户控件

右击类库项目名称,选择[添加]—>[新建项]然后再窗口中选择“用户控件(Windows窗体)”。


然后单击添加按钮,可以看到用户控件的模板:

5、设置尺寸样式

6、按F7进入代码视图

编写如下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace thing.HeatingCustomControls
{
    public partial class ThingerHCValve : UserControl
    {
        public ThingerHCValve()
        {
            InitializeComponent();
        }

        private bool isRun;
        [Category("自定义属性")]
        [Description("阀门启动或关闭")]
        public bool IsRun
        {
            get 
            { 
                return isRun; 
            }
            set 
            {
                if(value != isRun)
                {
                    isRun = value;
                    if (isRun)
                    {
                        this.lblValve.Text = "已打开";
                        this.picValve.BackgroundImage = Properties.Resources.阀门Open;
                    }
                    else
                    {
                        this.lblValve.Text = "已关闭";
                        this.picValve.BackgroundImage = Properties.Resources.阀门Close;
                    }
                } 
            }
        }

        public event Action<bool> IsRunChanged;
        [Category("自定义事件")]
        [Description("阀门双击事件")]
        private void picValve_DoubleClick(object sender, EventArgs e)
        {
            if(IsRunChanged == null)
            {
                IsRunChanged(IsRun);
            }
        }
    }
}

设置样式属性减少控件使用时的闪烁

this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

 

AllPaintingInWmPaint8192
CacheText16384

如果为 true,则控件将保留文本的副本,而不是每次必需时从 Handle 中获取。 此样式默认为 false。 此行为可提高性能,但很难保持文本同步。

ContainerControl1

如果为 true,则控件是类容器控件。

DoubleBuffer65536

如果为 true,则在缓冲区中进行绘制,并且完成后将结果输出到屏幕。 双缓冲可以防止因重绘控件而引起的闪烁。 如果将 DoubleBuffer 设置为 true,则还应将 UserPaint 和 AllPaintingInWmPaint 设置为 true

EnableNotifyMessage32768

如果为 true,则将对发送到控件的 WndProc(Message) 的每个消息调用 OnNotifyMessage(Message) 方法。 此样式默认为 false。 EnableNotifyMessage 在部分信任中不起作用。

FixedHeight64

如果为 true,则控件在自动缩放时具有固定高度。 例如,如果布局操作尝试重新缩放控件以适应新的 Font,则控件的 Height 保持不变。

FixedWidth32

如果为 true,则控件在自动缩放时具有固定宽度。 例如,如果布局操作尝试重新缩放控件以适应新的 Font,则控件的 Width 保持不变。

Opaque4

如果为 true,则控件会绘制为不透明,且不绘制背景。

OptimizedDoubleBuffer131072

如果为 true,则控件将首先绘制到缓冲区而不是直接绘制到屏幕,这可以减少闪烁。 如果将此属性设置为 true,则还应将 AllPaintingInWmPaint 设置为 true

ResizeRedraw16

如果为 true,则控件会在调整大小时进行重绘。

Selectable512

如果为 true,则控件可以接收焦点。

StandardClick256

如果为 true,则控件实现标准 Click 行为。

StandardDoubleClick4096

如果为 true,则控件实现标准 DoubleClick 行为。 如果未将 StandardClick 位设置为 true,则忽略此样式。

SupportsTransparentBackColor2048

如果为 true,则控件接受 alpha 组件数小于 255 个的 BackColor 来模拟透明度。 仅当将 UserPaint 位设置为 true 且父控件从 Control 派生时,才会模拟透明度。

UserMouse1024

如果为 true,则将由控件而不是操作系统处理其自身的鼠标事件。

UserPaint2

如果为 true,则会由控件而不是由操作系统来绘制控件自身。 如果 false,则不会引发 Paint 事件。 此样式仅适用于从 Control 派生的类。

UseTextForAccessibility262144

指定控件的 Text 属性的值,若设置,则确定控件的默认 Active Accessibility 名称和快捷键。

;