(十七)c#Winform自定义控件-基类窗体
程序员文章站
2022-06-05 20:32:23
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 如果觉得写的还行,请点个 star 支持一下吧 欢迎前来交流探讨: 企鹅群568015492 目录 ......
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
开源地址:
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
目录
准备工作
这个窗体继承子基类窗体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 }
1 namespace hzh_controls.forms 2 { 3 partial class frmwithtitle 4 { 5 /// <summary> 6 /// required designer variable. 7 /// </summary> 8 private system.componentmodel.icontainer components = null; 9 10 /// <summary> 11 /// clean up any resources being used. 12 /// </summary> 13 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 14 protected override void dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.dispose(); 19 } 20 base.dispose(disposing); 21 } 22 23 #region windows form designer generated code 24 25 /// <summary> 26 /// required method for designer support - do not modify 27 /// the contents of this method with the code editor. 28 /// </summary> 29 private void initializecomponent() 30 { 31 system.componentmodel.componentresourcemanager resources = new system.componentmodel.componentresourcemanager(typeof(frmwithtitle)); 32 this.lbltitle = new system.windows.forms.label(); 33 this.ucsplitline_h1 = new hzh_controls.controls.ucsplitline_h(); 34 this.btnclose = new system.windows.forms.panel(); 35 this.suspendlayout(); 36 // 37 // lbltitle 38 // 39 this.lbltitle.backcolor = system.drawing.color.transparent; 40 this.lbltitle.dock = system.windows.forms.dockstyle.top; 41 this.lbltitle.font = new system.drawing.font("微软雅黑", 17f); 42 this.lbltitle.location = new system.drawing.point(0, 0); 43 this.lbltitle.name = "lbltitle"; 44 this.lbltitle.size = new system.drawing.size(427, 60); 45 this.lbltitle.tabindex = 5; 46 this.lbltitle.text = "标题"; 47 this.lbltitle.textalign = system.drawing.contentalignment.middlecenter; 48 // 49 // ucsplitline_h1 50 // 51 this.ucsplitline_h1.backcolor = system.drawing.color.fromargb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238))))); 52 this.ucsplitline_h1.dock = system.windows.forms.dockstyle.top; 53 this.ucsplitline_h1.location = new system.drawing.point(0, 60); 54 this.ucsplitline_h1.name = "ucsplitline_h1"; 55 this.ucsplitline_h1.size = new system.drawing.size(427, 1); 56 this.ucsplitline_h1.tabindex = 0; 57 this.ucsplitline_h1.tabstop = false; 58 // 59 // btnclose 60 // 61 this.btnclose.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.right))); 62 this.btnclose.backgroundimage = global::hzh_controls.properties.resources.dialog_close; 63 this.btnclose.backgroundimagelayout = system.windows.forms.imagelayout.zoom; 64 this.btnclose.location = new system.drawing.point(399, 0); 65 this.btnclose.maximumsize = new system.drawing.size(0, 60); 66 this.btnclose.name = "btnclose"; 67 this.btnclose.size = new system.drawing.size(28, 60); 68 this.btnclose.tabindex = 6; 69 this.btnclose.visible = false; 70 this.btnclose.mousedown += new system.windows.forms.mouseeventhandler(this.btnclose_mousedown_1); 71 // 72 // frmwithtitle 73 // 74 this.autoscalemode = system.windows.forms.autoscalemode.none; 75 this.backcolor = system.drawing.color.white; 76 this.clientsize = new system.drawing.size(427, 310); 77 this.controls.add(this.btnclose); 78 this.controls.add(this.ucsplitline_h1); 79 this.controls.add(this.lbltitle); 80 this.icon = ((system.drawing.icon)(resources.getobject("$this.icon"))); 81 this.isfullsize = false; 82 this.isshowmaskdialog = true; 83 this.isshowregion = true; 84 this.name = "frmwithtitle"; 85 this.redraw = true; 86 this.showicon = false; 87 this.showintaskbar = false; 88 this.text = "frmwithtitle"; 89 this.shown += new system.eventhandler(this.frmwithtitle_shown); 90 this.visiblechanged += new system.eventhandler(this.frmwithtitle_visiblechanged); 91 this.resumelayout(false); 92 93 } 94 95 #endregion 96 97 private system.windows.forms.label lbltitle; 98 private controls.ucsplitline_h ucsplitline_h1; 99 private system.windows.forms.panel btnclose; 100 101 } 102 }
用处及效果
最后的话
如果你喜欢的话,请到 点个星 星吧