C# Winform自定义控件(Button)

前言

Winform自带控件功能比较单一,针对于现在更丰富的UI设计,已经不适用于丰富多彩的界面设计,所以需要通过Visual Studio的自定义控件重新自定义按钮,保证满足设计过程中的正常使用,下面我们一步一步创建自定义按钮。

一、创建自定义控件项

1.打开Visual Studio(我这里使用的是2022版)--> 选择创建新项目-->选择类库(.NET Framework)-->填写项目名称和位置-->创建;

2.删除自动生成的class1.cs文件,在项目上右键(解决方案下带C#图表的行) -->添加-->新建项,选择C#项-->Windows Forms-->自定义控件(Windows 窗体),重新命名名称,点击添加;

3.根据现在自动生成的界面,点击"切换到代码视图",可以看到代码页;

二、添加代码

在代码页中自带一些基础代码,有一个构造函数和一个重写的受保护的绘制函数,我们把类的继承改成Button(把Control改成Button),并开始添加属性和功能(属性和方法需要结合使用);

一、添加属性

1.添加LED显示。

        添加LED属性代码(配合方法中LED块使用)

//是否启用
private bool LedEnable = false;
[Category("LED")]//(属性类)
[Description("是否启用LED")]//  (针对此属性的描述)
public bool LEDEnable
{
    get{return LedEnable;}
    set{
        LedEnable = value;
        Invalidate();
        }
}
//显示状态
private bool state = false;
[Category("LED")]
[Description("状态:true-LED ON  false-LED OFF")]
public bool State
{
    get {return state;}
    set {
        state = value;
        Invalidate();
        }
}
//开启颜色
private Color OnState = Color.Lime;
[Category("LED")]
[Description("LED 开启时颜色")]
public Color OnColor
{
    get{return OnState;}
    set{
        OnState = value;
        Invalidate();
        }
}
//关闭颜色
private Color OffState = Color.Silver;
[Category("LED")]
[Description("LED 关闭时颜色")]
public Color OffColor
{
    get{return OffState;}
    set{
        OffState = value;
         Invalidate();
        }
}

2.添加标题

 bool ShowTitle = false;
        [Category("Title")]
        [Description("显示标题")]
        public bool SHOWTitle
        {
            get { return ShowTitle; }
            set
            {
                ShowTitle = value;
                Invalidate();
            }
        }

        string TextTitle = string.Empty;
        [Category("Title")]
        [Description("内容")]
        public string TEXTTitle
        {
            get { return TextTitle; }
            set
            {
                TextTitle = value;
                Invalidate();
            }
        }

        Font fontTitle = new Font("宋体",9);
        [Category("Title")]
        [Description("字体")]
        public Font FontTitle
        {
            get { return fontTitle; }
            set
            {
                fontTitle = value;
                Invalidate();
            }
        }

        Color colorTitle = Color.Black;
        [Category("Title")]
        [Description("颜色")]
        public Color ColorTitle
        {
            get { return colorTitle; }
            set
            {
                colorTitle = value;
                Invalidate();
            }
        }

二、添加方法

        添加是否显示led和title功能

//在重写函数中调用
protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    
    //LED
    if(LedEnable)
    {
        Brush brush = new SolidBrush(state? OnState:OffState);

        e.Graphics.FillRectangle(brush,5,5,15,15);
        e.Graphics.FillRectangle(Brushes.White,8,8,6,2);
    }

    //title
    if(ShowTitle)
    {
         pe.Graphics.DrawString(TextTitle, fontTitle, new SolidBrush(colorTitle), 3, 3);
    }
}

三、日志记录

        在需要记录的地方添加记录

 bool savelog(string name,string info)
 {
      string path = $"./log/{DateTime.Now:yyyy}/{DateTime.Now:yyyy-MM-dd}";
      if(!Directory.Exists(Path.GetDirectoryName(path)))
      {
          Directory.CreateDirectory(Path.GetDirectoryName(path));
      }
      try
      {
          using (StreamWriter sw = new StreamWriter(path,true,Encoding.Default))
          {
              sw.WriteLine($"[{DateTime.Now:MM-dd HH:mm:ss}] [{name}]  [{info}]");
          }
          return true;
      }
      catch (Exception)
      {
          return false;
      }
 }

三、结尾/声明

此方法是基于本人现工作内容总结,包括网络查询学习内容,如有侵权请告知。

链接:https://download.csdn.net/download/qq_35355765/89211798

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用自定义 WinForm 控件来创建自己的 MessageBox,以下是一些实现思路: 1. 创建一个继承自 Form 的类,用于作为自定义 MessageBox 的主界面; 2. 在该界面中添加需要的控件,比如 Label 用于显示提示信息,Button 用于处理用户的选择等; 3. 在调用自定义 MessageBox 的代码中,实例化该 Form 类,并设置其属性和事件处理程序; 4. 在事件处理程序中,获取用户的选择并返回给调用方。 下面是一个简单的示例代码: ```csharp public partial class CustomMessageBox : Form { public CustomMessageBox(string message, string title, MessageBoxButtons buttons) { InitializeComponent(); this.lblMessage.Text = message; this.Text = title; switch (buttons) { case MessageBoxButtons.OK: this.btnOK.Visible = true; break; case MessageBoxButtons.OKCancel: this.btnOK.Visible = true; this.btnCancel.Visible = true; break; case MessageBoxButtons.YesNo: this.btnYes.Visible = true; this.btnNo.Visible = true; break; case MessageBoxButtons.YesNoCancel: this.btnYes.Visible = true; this.btnNo.Visible = true; this.btnCancel.Visible = true; break; } } public DialogResult Result { get; private set; } private void btnOK_Click(object sender, EventArgs e) { this.Result = DialogResult.OK; this.Close(); } private void btnCancel_Click(object sender, EventArgs e) { this.Result = DialogResult.Cancel; this.Close(); } private void btnYes_Click(object sender, EventArgs e) { this.Result = DialogResult.Yes; this.Close(); } private void btnNo_Click(object sender, EventArgs e) { this.Result = DialogResult.No; this.Close(); } } ``` 调用该自定义 MessageBox 的代码示例如下: ```csharp var result = new CustomMessageBox("Are you sure?", "Confirm", MessageBoxButtons.YesNo).ShowDialog(); if (result == DialogResult.Yes) { // Do something } else { // Do something else } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值