前提 入行已經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,命名FrmBack,繼承自FrmBase
屬性
1 private string _frmTitle = "自定義窗體"; 2 /// <summary> 3 /// 窗體標題 4 /// </summary> 5 [Description("窗體標題"), Category("自定義")] 6 public string FrmTitle 7 { 8 get { return _frmTitle; } 9 set 10 { 11 _frmTitle = value; 12 btnBack1.BtnText = value; 13 } 14 } 15 [Description("幫助按鈕點擊事件"), Category("自定義")] 16 public event EventHandler BtnHelpClick;
一點小事件
1 private void btnBack1_btnClick(object sender, EventArgs e) 2 { 3 this.Close(); 4 } 5 6 private void label1_MouseDown(object sender, MouseEventArgs e) 7 { 8 if (BtnHelpClick != null) 9 BtnHelpClick(sender, e); 10 }
完整代碼
1 // 版權所有 黃正輝 交流群:568015492 QQ:623128629 2 // 文件名稱:FrmTemp1.cs 3 // 創建日期:2019-08-15 16:04:48 4 // 功能描述:FrmTemp1 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 FrmBack : FrmBase 19 { 20 private string _frmTitle = "自定義窗體"; 21 /// <summary> 22 /// 窗體標題 23 /// </summary> 24 [Description("窗體標題"), Category("自定義")] 25 public string FrmTitle 26 { 27 get { return _frmTitle; } 28 set 29 { 30 _frmTitle = value; 31 btnBack1.BtnText = value; 32 } 33 } 34 [Description("幫助按鈕點擊事件"), Category("自定義")] 35 public event EventHandler BtnHelpClick; 36 37 public FrmBack() 38 { 39 InitializeComponent(); 40 } 41 42 private void btnBack1_btnClick(object sender, EventArgs e) 43 { 44 this.Close(); 45 } 46 47 private void label1_MouseDown(object sender, MouseEventArgs e) 48 { 49 if (BtnHelpClick != null) 50 BtnHelpClick(sender, e); 51 } 52 } 53 }View Code
1 namespace HZH_Controls.Forms 2 { 3 partial class FrmBack 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 this.components = new System.ComponentModel.Container(); 32 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmBack)); 33 this.panTop = new System.Windows.Forms.Panel(); 34 this.label1 = new System.Windows.Forms.Label(); 35 this.btnBack1 = new HZH_Controls.Controls.UCBtnImg(); 36 this.imageList1 = new System.Windows.Forms.ImageList(this.components); 37 this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H(); 38 this.panTop.SuspendLayout(); 39 this.SuspendLayout(); 40 // 41 // panTop 42 // 43 this.panTop.Controls.Add(this.label1); 44 this.panTop.Controls.Add(this.btnBack1); 45 this.panTop.Dock = System.Windows.Forms.DockStyle.Top; 46 this.panTop.Location = new System.Drawing.Point(0, 0); 47 this.panTop.Name = "panTop"; 48 this.panTop.Size = new System.Drawing.Size(679, 60); 49 this.panTop.TabIndex = 2; 50 // 51 // label1 52 // 53 this.label1.BackColor = System.Drawing.Color.Transparent; 54 this.label1.Dock = System.Windows.Forms.DockStyle.Right; 55 this.label1.Font = new System.Drawing.Font("微軟雅黑", 12F, System.Drawing.FontStyle.Bold); 56 this.label1.Image = global::HZH_Controls.Properties.Resources.help; 57 this.label1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; 58 this.label1.Location = new System.Drawing.Point(612, 0); 59 this.label1.Name = "label1"; 60 this.label1.Size = new System.Drawing.Size(67, 60); 61 this.label1.TabIndex = 1; 62 this.label1.Text = "幫助"; 63 this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight; 64 this.label1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.label1_MouseDown); 65 // 66 // btnBack1 67 // 68 this.btnBack1.BackColor = System.Drawing.Color.Transparent; 69 this.btnBack1.BtnBackColor = System.Drawing.Color.Transparent; 70 this.btnBack1.BtnFont = new System.Drawing.Font("微軟雅黑", 17F); 71 this.btnBack1.BtnForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(102)))), ((int)(((byte)(102)))), ((int)(((byte)(102))))); 72 this.btnBack1.BtnText = "自定義按鈕"; 73 this.btnBack1.ConerRadius = 5; 74 this.btnBack1.Cursor = System.Windows.Forms.Cursors.Hand; 75 this.btnBack1.Dock = System.Windows.Forms.DockStyle.Left; 76 this.btnBack1.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247))))); 77 this.btnBack1.Font = new System.Drawing.Font("微軟雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel); 78 this.btnBack1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(102)))), ((int)(((byte)(102)))), ((int)(((byte)(102))))); 79 this.btnBack1.Image = ((System.Drawing.Image)(resources.GetObject("btnBack1.Image"))); 80 this.btnBack1.IsRadius = true; 81 this.btnBack1.IsShowRect = true; 82 this.btnBack1.IsShowTips = false; 83 this.btnBack1.Location = new System.Drawing.Point(0, 0); 84 this.btnBack1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); 85 this.btnBack1.Name = "btnBack1"; 86 this.btnBack1.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247))))); 87 this.btnBack1.RectWidth = 1; 88 this.btnBack1.Size = new System.Drawing.Size(200, 60); 89 this.btnBack1.TabIndex = 0; 90 this.btnBack1.TabStop = false; 91 this.btnBack1.TipsText = ""; 92 this.btnBack1.BtnClick += new System.EventHandler(this.btnBack1_btnClick); 93 // 94 // imageList1 95 // 96 this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream"))); 97 this.imageList1.TransparentColor = System.Drawing.Color.Transparent; 98 this.imageList1.Images.SetKeyName(0, "help.png"); 99 // 100 // ucSplitLine_H1 101 // 102 this.ucSplitLine_H1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238))))); 103 this.ucSplitLine_H1.Dock = System.Windows.Forms.DockStyle.Top; 104 this.ucSplitLine_H1.Location = new System.Drawing.Point(0, 60); 105 this.ucSplitLine_H1.MaximumSize = new System.Drawing.Size(0, 1); 106 this.ucSplitLine_H1.Name = "ucSplitLine_H1"; 107 this.ucSplitLine_H1.Size = new System.Drawing.Size(679, 1); 108 this.ucSplitLine_H1.TabIndex = 0; 109 this.ucSplitLine_H1.TabStop = false; 110 // 111 // FrmTemp1 112 // 113 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; 114 this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247))))); 115 this.ClientSize = new System.Drawing.Size(679, 477); 116 this.Controls.Add(this.ucSplitLine_H1); 117 this.Controls.Add(this.panTop); 118 this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); 119 this.Name = "FrmTemp1"; 120 this.ShowIcon = false; 121 this.ShowInTaskbar = false; 122 this.Text = "FrmTemp1"; 123 this.panTop.ResumeLayout(false); 124 this.ResumeLayout(false); 125 126 } 127 128 #endregion 129 130 private Controls.UCBtnImg btnBack1; 131 private System.Windows.Forms.Label label1; 132 private System.Windows.Forms.ImageList imageList1; 133 private Controls.UCSplitLine_H ucSplitLine_H1; 134 private System.Windows.Forms.Panel panTop; 135 } 136 }View Code
設計效果
用處及效果
用處:這個看個人使用情況吧,你高興的話就用這個窗體就可以了
最後的話
如果你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星 星吧