(二十四)c#Winform自定义控件-单标题窗体-HZHControls
官网
前提
入行已经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 }
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 }
用处及效果
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧
作者:冰封一夏
出处:http://www.cnblogs.com/bfyx/
HZHControls官网:http://www.hzhcontrols.cn
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,
且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git