前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
目录
https://www.cnblogs.com/bfyx/p/11364884.html
准备工作
这个窗体继承子基类窗体FrmBase,如果你对FrmBase还不了解,请移步 (十七)c#Winform自定义控件-基类窗体 查看
开始
添加Form,命名FrmWithTitle,继承自FrmBase
代码较少,直接全部代码
1 // 版权所有 黄正辉 交流群:568015492 QQ:623128629 2 // 文件名称:FrmWithTitle.cs 3 // 创建日期:2019-08-15 16:05:30 4 // 功能描述:FrmWithTitle 5 // 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 6 using System; 7 using System.Collections.Generic; 8 using System.ComponentModel; 9 using System.Data; 10 using System.Drawing; 11 using System.Linq; 12 using System.Text; 13 using System.Windows.Forms; 14 15 namespace HZH_Controls.Forms 16 { 17 [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(System.ComponentModel.Design.IDesigner))] 18 public partial class FrmWithTitle : FrmBase 19 { 20 [Description("窗体标题"), Category("自定义")] 21 public string Title 22 { 23 get 24 { 25 return lblTitle.Text; 26 } 27 set 28 { 29 lblTitle.Text = value; 30 } 31 } 32 private bool _isShowCloseBtn = false; 33 [Description("是否显示右上角关闭按钮"), Category("自定义")] 34 public bool IsShowCloseBtn 35 { 36 get 37 { 38 return _isShowCloseBtn; 39 } 40 set 41 { 42 _isShowCloseBtn = value; 43 btnClose.Visible = value; 44 if (value) 45 { 46 btnClose.Location = new Point(this.Width - btnClose.Width - 10, 0); 47 btnClose.BringToFront(); 48 } 49 } 50 } 51 52 public FrmWithTitle() 53 { 54 InitializeComponent(); 55 } 56 57 private void btnClose_MouseDown(object sender, MouseEventArgs e) 58 { 59 this.Close(); 60 } 61 62 private void FrmWithTitle_Shown(object sender, EventArgs e) 63 { 64 if (IsShowCloseBtn) 65 { 66 btnClose.Location = new Point(this.Width - btnClose.Width - 10, 0); 67 btnClose.BringToFront(); 68 } 69 } 70 71 private void btnClose_MouseDown_1(object sender, MouseEventArgs e) 72 { 73 this.Close(); 74 } 75 76 private void FrmWithTitle_VisibleChanged(object sender, EventArgs e) 77 { 78 } 79 } 80 }