前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 如果覺得寫的還行,請點個 star 支持一下吧 歡迎前來交流探討: 企鵝群568015492 目錄 ...
前提
入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。
開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
如果覺得寫的還行,請點個 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 }View Code
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 }View Code
用處及效果
最後的話
如果你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星 星吧