前提 入行已經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命名為FrmDialog ,繼承FrmBase
私有的構造函數
1 private FrmDialog( 2 string strMessage, 3 string strTitle, 4 bool blnShowCancel = false, 5 bool blnShowClose = false, 6 bool blnisEnterClose = true) 7 { 8 InitializeComponent(); 9 if (!string.IsNullOrWhiteSpace(strTitle)) 10 lblTitle.Text = strTitle; 11 lblMsg.Text = strMessage; 12 if (blnShowCancel) 13 { 14 this.tableLayoutPanel1.ColumnStyles[1].Width = 1; 15 this.tableLayoutPanel1.ColumnStyles[2].Width = 50; 16 } 17 else 18 { 19 this.tableLayoutPanel1.ColumnStyles[1].Width = 0; 20 this.tableLayoutPanel1.ColumnStyles[2].Width = 0; 21 } 22 btnClose.Visible = blnShowClose; 23 blnEnterClose = blnisEnterClose; 24 }
搭配一個靜態的公共函數
1 #region 顯示一個模式信息框 2 /// <summary> 3 /// 功能描述:顯示一個模式信息框 4 /// 作 者:HZH 5 /// 創建日期:2019-03-04 15:49:48 6 /// 任務編號:POS 7 /// </summary> 8 /// <param name="owner">owner</param> 9 /// <param name="strMessage">strMessage</param> 10 /// <param name="strTitle">strTitle</param> 11 /// <param name="blnShowCancel">blnShowCancel</param> 12 /// <param name="isShowMaskDialog">isShowMaskDialog</param> 13 /// <param name="blnShowClose">blnShowClose</param> 14 /// <param name="isEnterClose">isEnterClose</param> 15 /// <returns>返回值</returns> 16 public static DialogResult ShowDialog( 17 IWin32Window owner, 18 string strMessage, 19 string strTitle = "提示", 20 bool blnShowCancel = false, 21 bool isShowMaskDialog = true, 22 bool blnShowClose = false, 23 bool blnIsEnterClose = true) 24 { 25 DialogResult result = DialogResult.Cancel; 26 if (owner == null || (owner is Control && (owner as Control).IsDisposed)) 27 { 28 result = new FrmDialog(strMessage, strTitle, blnShowCancel, blnShowClose, blnIsEnterClose) 29 { 30 StartPosition = FormStartPosition.CenterScreen, 31 IsShowMaskDialog = isShowMaskDialog, 32 TopMost = true 33 }.ShowDialog(); 34 } 35 else 36 { 37 if (owner is Control) 38 { 39 owner = (owner as Control).FindForm(); 40 } 41 result = new FrmDialog(strMessage, strTitle, blnShowCancel, blnShowClose, blnIsEnterClose) 42 { 43 StartPosition = (owner != null) ? FormStartPosition.CenterParent : FormStartPosition.CenterScreen, 44 IsShowMaskDialog = isShowMaskDialog, 45 TopMost = true 46 }.ShowDialog(owner); 47 } 48 return result; 49 } 50 #endregion
一些小事件
1 private void btnOK_BtnClick(object sender, EventArgs e) 2 { 3 this.DialogResult = System.Windows.Forms.DialogResult.OK; 4 } 5 6 private void btnCancel_BtnClick(object sender, EventArgs e) 7 { 8 this.DialogResult = System.Windows.Forms.DialogResult.Cancel; 9 } 10 11 private void btnClose_MouseDown(object sender, MouseEventArgs e) 12 { 13 this.DialogResult = System.Windows.Forms.DialogResult.Cancel; 14 } 15 16 protected override void DoEnter() 17 { 18 if (blnEnterClose) 19 this.DialogResult = System.Windows.Forms.DialogResult.OK; 20 }
代碼就這麼多,看下完整代碼
1 // 版權所有 黃正輝 交流群:568015492 QQ:623128629 2 // 文件名稱:FrmDialog.cs 3 // 創建日期:2019-08-15 16:04:36 4 // 功能描述:FrmDialog 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 public partial class FrmDialog : FrmBase 18 { 19 bool blnEnterClose = true; 20 private FrmDialog( 21 string strMessage, 22 string strTitle, 23 bool blnShowCancel = false, 24 bool blnShowClose = false, 25 bool blnisEnterClose = true) 26 { 27 InitializeComponent(); 28 if (!string.IsNullOrWhiteSpace(strTitle)) 29 lblTitle.Text = strTitle; 30 lblMsg.Text = strMessage; 31 if (blnShowCancel) 32 { 33 this.tableLayoutPanel1.ColumnStyles[1].Width = 1; 34 this.tableLayoutPanel1.ColumnStyles[2].Width = 50; 35 } 36 else 37 { 38 this.tableLayoutPanel1.ColumnStyles[1].Width = 0; 39 this.tableLayoutPanel1.ColumnStyles[2].Width = 0; 40 } 41 //btnCancel.Visible = blnShowCancel; 42 //ucSplitLine_V1.Visible = blnShowCancel; 43 btnClose.Visible = blnShowClose; 44 blnEnterClose = blnisEnterClose; 45 //if (blnShowCancel) 46 //{ 47 // btnOK.BtnForeColor = Color.FromArgb(255, 85, 51); 48 //} 49 } 50 51 #region 顯示一個模式信息框 52 /// <summary> 53 /// 功能描述:顯示一個模式信息框 54 /// 作 者:HZH 55 /// 創建日期:2019-03-04 15:49:48 56 /// 任務編號:POS 57 /// </summary> 58 /// <param name="owner">owner</param> 59 /// <param name="strMessage">strMessage</param> 60 /// <param name="strTitle">strTitle</param> 61 /// <param name="blnShowCancel">blnShowCancel</param> 62 /// <param name="isShowMaskDialog">isShowMaskDialog</param> 63 /// <param name="blnShowClose">blnShowClose</param> 64 /// <param name="isEnterClose">isEnterClose</param> 65 /// <returns>返回值</returns> 66 public static DialogResult ShowDialog( 67 IWin32Window owner, 68 string strMessage, 69 string strTitle = "提示", 70 bool blnShowCancel = false, 71 bool isShowMaskDialog = true, 72 bool blnShowClose = false, 73 bool blnIsEnterClose = true) 74 { 75 DialogResult result = DialogResult.Cancel; 76 if (owner == null || (owner is Control && (owner as Control).IsDisposed)) 77 { 78 result = new FrmDialog(strMessage, strTitle, blnShowCancel, blnShowClose, blnIsEnterClose) 79 { 80 StartPosition = FormStartPosition.CenterScreen, 81 IsShowMaskDialog = isShowMaskDialog, 82 TopMost = true 83 }.ShowDialog(); 84 } 85 else 86 { 87 if (owner is Control) 88 { 89 owner = (owner as Control).FindForm(); 90 } 91 result = new FrmDialog(strMessage, strTitle, blnShowCancel, blnShowClose, blnIsEnterClose) 92 { 93 StartPosition = (owner != null) ? FormStartPosition.CenterParent : FormStartPosition.CenterScreen, 94 IsShowMaskDialog = isShowMaskDialog, 95 TopMost = true 96 }.ShowDialog(owner); 97 } 98 return result; 99 } 100 #endregion 101 102 private void btnOK_BtnClick(object sender, EventArgs e) 103 { 104 this.DialogResult = System.Windows.Forms.DialogResult.OK; 105 } 106 107 private void btnCancel_BtnClick(object sender, EventArgs e) 108 { 109 this.DialogResult = System.Windows.Forms.DialogResult.Cancel; 110 } 111 112 private void btnClose_MouseDown(object sender, MouseEventArgs e) 113 { 114 this.DialogResult = System.Windows.Forms.DialogResult.Cancel; 115 } 116 117 protected override void DoEnter() 118 { 119 if (blnEnterClose) 120 this.DialogResult = System.Windows.Forms.DialogResult.OK; 121 } 122 123 private void FrmDialog_VisibleChanged(object sender, EventArgs e) 124 { 125 126 } 127 } 128 }View Code
1 namespace HZH_Controls.Forms 2 { 3 partial class FrmDialog 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(FrmDialog)); 32 this.btnClose = new System.Windows.Forms.Panel(); 33 this.panel1 = new System.Windows.Forms.Panel(); 34 this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); 35 this.btnCancel = new HZH_Controls.Controls.UCBtnExt(); 36 this.ucSplitLine_V1 = new HZH_Controls.Controls.UCSplitLine_V(); 37 this.btnOK = new HZH_Controls.Controls.UCBtnExt(); 38 this.lblMsg = new System.Windows.Forms.Label(); 39 this.lblTitle = new System.Windows.Forms.Label(); 40 this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H(); 41 this.ucSplitLine_H2 = new HZH_Controls.Controls.UCSplitLine_H(); 42 this.panel1.SuspendLayout(); 43 this.tableLayoutPanel1.SuspendLayout(); 44 this.SuspendLayout(); 45 // 46 // btnClose 47 // 48 this.btnClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); 49 this.btnClose.BackgroundImage = global::HZH_Controls.Properties.Resources.dialog_close; 50 this.btnClose.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; 51 this.btnClose.Location = new System.Drawing.Point(383, 0); 52 this.btnClose.Name = "btnClose"; 53 this.btnClose.Size = new System.Drawing.Size(28, 60); 54 this.btnClose.TabIndex = 4; 55 this.btnClose.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnClose_MouseDown); 56 // 57 // panel1 58 // 59 this.panel1.Controls.Add(this.tableLayoutPanel1); 60 this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom; 61 this.panel1.Location = new System.Drawing.Point(0, 214); 62 this.panel1.Name = "panel1"; 63 this.panel1.Size = new System.Drawing.Size(427, 64); 64 this.panel1.TabIndex = 3; 65 // 66 // tableLayoutPanel1 67 // 68 this.tableLayoutPanel1.ColumnCount = 3; 69 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); 70 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F)); 71 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); 72 this.tableLayoutPanel1.Controls.Add(this.btnCancel, 2, 0); 73 this.tableLayoutPanel1.Controls.Add(this.ucSplitLine_V1, 1, 0); 74 this.tableLayoutPanel1.Controls.Add(this.btnOK, 0, 0); 75 this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; 76 this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0); 77 this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(0); 78 this.tableLayoutPanel1.Name = "tableLayoutPanel1"; 79 this.tableLayoutPanel1.RowCount = 1; 80 this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); 81 this.tableLayoutPanel1.Size = new System.Drawing.Size(427, 64); 82 this.tableLayoutPanel1.TabIndex = 5; 83 // 84 // btnCancel 85 // 86 this.btnCancel.BackColor = System.Drawing.Color.Transparent; 87 this.btnCancel.BtnBackColor = System.Drawing.Color.Transparent; 88 this.btnCancel.BtnFont = new System.Drawing.Font("微軟雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 89 this.btnCancel.BtnForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(73)))), ((int)(((byte)(119)))), ((int)(((byte)(232))))); 90 this.btnCancel.BtnText = "取消"; 91 this.btnCancel.ConerRadius = 5; 92 this.btnCancel.Cursor = System.Windows.Forms.Cursors.Hand; 93 this.btnCancel.Dock = System.Windows.Forms.DockStyle.Fill; 94 this.btnCancel.FillColor = System.Drawing.Color.White; 95 this.btnCancel.Font = new System.Drawing.Font("微軟雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel); 96 this.btnCancel.IsRadius = false; 97 this.btnCancel.IsShowRect = false; 98 this.btnCancel.IsShowTips = false; 99 this.btnCancel.Location = new System.Drawing.Point(214, 0); 100 this.btnCancel.Margin = new System.Windows.Forms.Padding(0); 101 this.btnCancel.Name = "btnCancel"; 102 this.btnCancel.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247))))); 103 this.btnCancel.RectWidth = 1; 104 this.btnCancel.Size = new System.Drawing.Size(213, 64); 105 this.btnCancel.TabIndex = 1; 106 this.btnCancel.TabStop = false; 107 this.btnCancel.TipsText = ""; 108 this.btnCancel.BtnClick += new System.EventHandler(this.btnCancel_BtnClick); 109 // 110 // ucSplitLine_V1 111 // 112 this.ucSplitLine_V1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238))))); 113 this.ucSplitLine_V1.Dock = System.Windows.Forms.DockStyle.Fill; 114 this.ucSplitLine_V1.Location = new System.Drawing.Point(213, 15); 115 this.ucSplitLine_V1.Margin = new System.Windows.Forms.Padding(0, 15, 0, 15); 116 this.ucSplitLine_V1.Name = "ucSplitLine_V1"; 117 this.ucSplitLine_V1.Size = new System.Drawing.Size(1, 34); 118 this.ucSplitLine_V1.TabIndex = 2; 119 this.ucSplitLine_V1.TabStop = false; 120 // 121 // btnOK 122 // 123 this.btnOK.BackColor = System.Drawing.Color.Transparent; 124 this.btnOK.BtnBackColor = System.Drawing.Color.Transparent; 125 this.btnOK.BtnFont = new System.Drawing.Font("微軟雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 126 this.btnOK.BtnForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59))))); 127 this.btnOK.BtnText = "確定"; 128 this.btnOK.ConerRadius = 5; 129 this.btnOK.Cursor = System.Windows.Forms.Cursors.Hand; 130 this.btnOK.Dock = System.Windows.Forms.DockStyle.Fill; 131 this.btnOK.FillColor = System.Drawing.Color.White; 132 this.btnOK.Font = new System.Drawing.Font("微軟雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel); 133 this.btnOK.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59))))); 134 this.btnOK.IsRad