【C#】【Winform】自定义控件、自定义事件

https://blog.csdn.net/m0_62366581/article/details/139553373

在开发桌面的过程中,有时候自带的控件样式或者功能上可能不一定能够满足我们的所有要求。这时候,我们需要自定义控件。

  1. 创建类库项目
  2. 把图标拖放到资源文件中
  3. 添加用户控件
  4. 设置样式尺寸
  5. 修改代码视图
  6. 其他项目中复用自定义控件

1.创建类库项目

新建类库项目。注意选择“.NET Framework”。创建完成后,删除默认的Class1。

2.把全部项目拖放到资源文件中

在类库中添加图标文件(这里是png结尾的两个文件)。右击项目的【属性】→【资源】,打开资源文件。

 将图片拖拽到资源文件,并保存,即可添加到Resources资源文件中。

3.添加用户控件

右击【添加】→【新建项】,选择【用户窗体控件】。这里将命名BaseValue.cs。

4.设置样式尺寸

右击【属性】,设置大小尺寸等。

这里,添加了一个 PictureBox 和 Label 控件。并设置及基础样式。

5.修改代码

按F7进入代码修改页面。自定义属性和事件。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace MyControlLib
12 {
13     public partial class BaseValue : UserControl
14     {
15         public BaseValue()
16         {
17             InitializeComponent();
18         }
19 
20         private bool isRun;
21         [Category("自定义属性")]
22         [Description("阀门启动或关闭")]
23         public bool IsRun
24         {
25             get { return isRun; }
26             set {
27                 if (value != isRun){
28                     isRun = value;
29                     if (isRun)
30                     {
31                         this.lb.Text = "已打开";
32                         this.pictureBox1.Image = Properties.Resources.face;
33                     }
34                     else
35                     {
36                         this.lb.Text = "已关闭";
37                         this.pictureBox1.Image = Properties.Resources.proof;
38                     }
39                 }
40             }
41         }
42 
43         public event Action<bool> IsRunChanged;
44         [Category("自定义事件")]
45         [Description("阀门双击事件")]
46         private void picValue_DoubleClick(object sender, EventArgs e)
47         {
48             if(IsRunChanged == null)
49             {
50                 IsRunChanged(isRun);
51             }
52         }
53     }
54 }
BaseValue

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

1 this.SetStyle(ControlStyles.UserPaint, true);
2 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
3 this.SetStyle(ControlStyles.DoubleBuffer, true);
4 this.SetStyle(ControlStyles.ResizeRedraw, true);
5 this.SetStyle(ControlStyles.Selectable, true);
6 this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
AllPaintingInWmPaint  如果为 true,则控件忽略窗口消息 WM_ERASEBKGND 以减少闪烁。 仅当将 UserPaint 位设置为 true 时,才应用此样式。 
CacheText 如果为 true,则控件将保留文本的副本,而不是每次必需时从 Handle 中获取。 此样式默认为 false。 此行为可提高性能,但很难保持文本同步。
 ContainerControl  如果为 true,则控件是类容器控件。
DoubleBuffer 如果为 true,则在缓冲区中进行绘制,并且完成后将结果输出到屏幕。 双缓冲可以防止因重绘控件而引起的闪烁。 如果将 DoubleBuffer 设置为 true,则还应将 UserPaint 和 AllPaintingInWmPaint 设置为 true。
EnableNotifyMessage 如果为 true,则将对发送到控件的 WndProc(Message) 的每个消息调用 OnNotifyMessage(Message) 方法。 此样式默认为 false。 EnableNotifyMessage 在部分信任中不起作用。
FixedHeight 如果为 true,则控件在自动缩放时具有固定高度。 例如,如果布局操作尝试重新缩放控件以适应新的 Font,则控件的 Height 保持不变。
FixedWidth 如果为 true,则控件在自动缩放时具有固定宽度。 例如,如果布局操作尝试重新缩放控件以适应新的 Font,则控件的 Width 保持不变。
Opaque 如果为 true,则控件会绘制为不透明,且不绘制背景。
OptimizedDoubleBuffer 如果为 true,则控件将首先绘制到缓冲区而不是直接绘制到屏幕,这可以减少闪烁。 如果将此属性设置为 true,则还应将 AllPaintingInWmPaint 设置为 true。
ResizeRedraw 如果为 true,则控件会在调整大小时进行重绘。
Selectable 如果为 true,则控件可以接收焦点。
StandardClick 如果为 true,则控件实现标准 Click 行为。
StandardDoubleClick 如果为 true,则控件实现标准 DoubleClick 行为。 如果未将 StandardClick 位设置为 true,则忽略此样式。
SupportsTransparentBackColor 如果为 true,则控件接受 alpha 组件数小于 255 个的 BackColor 来模拟透明度。 仅当将 UserPaint 位设置为 true 且父控件从 Control 派生时,才会模拟透明度。
UserMouse 如果为 true,则将由控件而不是操作系统处理其自身的鼠标事件。
UserPaint 如果为 true,则会由控件而不是由操作系统来绘制控件自身。 如果 false,则不会引发 Paint 事件。 此样式仅适用于从 Control 派生的类。
UseTextForAccessibility 指定控件的 Text 属性的值,若设置,则确定控件的默认 Active Accessibility 名称和快捷键。

6.其他项目中复用自定义控件

其他项目,添加dll引用,并在工具箱中添加引用。即可在其他项目中复用自定义控件

 

posted @ 2024-08-28 10:06  陆陆无为而治者  阅读(3)  评论(0编辑  收藏  举报