前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
官网:https://www.hzhcontrols.cn
GitHub:https://github.com/kwwwvagaa/NetWinformControl
如果觉得写的还行,请点个 star 支持一下吧
目录
c#Winform自定义控件-目录_c#winform自定义控件-有图标的按钮-CSDN博客
准备工作
用到了控件基类 UCControlBase,如果你还不了解UCControlBase,请移步 (一)c#Winform自定义控件-基类控件 查看
开始
添加一个用户控件,命名UCPanelTitle,继承自UCControlBase
2个属性
1 [Description("边框颜色"), Category("自定义")]
2 public Color BorderColor
3 {
4 get { return this.RectColor; }
5 set
6 {
7 this.RectColor = value;
8 this.lblTitle.BackColor = value;
9 }
10 }
11
12 [Description("面板标题"), Category("自定义")]
13 public string Title
14 {
15 get { return lblTitle.Text; }
16 set { lblTitle.Text = value; }
17 }
全部代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HZH_Controls.Controls
{
public partial class UCPanelTitle : UCControlBase
{
[Description("边框颜色"), Category("自定义")]
public Color BorderColor
{
get { return this.RectColor; }
set
{
this.RectColor = value;
this.lblTitle.BackColor = value;
}
}
[Description("面板标题"), Category("自定义")]
public string Title
{
get { return lblTitle.Text; }
set { lblTitle.Text = value; }
}
public UCPanelTitle()
{
InitializeComponent();
}
}
}
namespace HZH_Controls.Controls
{
partial class UCPanelTitle
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lblTitle = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// lblTitle
//
this.lblTitle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(22)))), ((int)(((byte)(160)))), ((int)(((byte)(133)))));
this.lblTitle.Dock = System.Windows.Forms.DockStyle.Top;
this.lblTitle.ForeColor = System.Drawing.Color.White;
this.lblTitle.Location = new System.Drawing.Point(0, 0);
this.lblTitle.Name = "lblTitle";
this.lblTitle.Size = new System.Drawing.Size(432, 34);
this.lblTitle.TabIndex = 0;
this.lblTitle.Text = "面板";
this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// UCPanelTitle
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.Transparent;
this.ConerRadius = 10;
this.Controls.Add(this.lblTitle);
this.FillColor = System.Drawing.Color.White;
this.IsRadius = true;
this.IsShowRect = true;
this.Name = "UCPanelTitle";
this.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(22)))), ((int)(((byte)(160)))), ((int)(((byte)(133)))));
this.Size = new System.Drawing.Size(432, 301);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label lblTitle;
}
}