<ul id="qxxfc"><fieldset id="qxxfc"><tr id="qxxfc"></tr></fieldset></ul>


      前提

      入行已經(jīng)7,8年了,一直想做一套漂亮點(diǎn)的自定義控件,于是就有了本系列文章。

      開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
      <https://gitee.com/kwwwvagaa/net_winform_custom_control>

      如果覺得寫的還行,請點(diǎn)個(gè) star 支持一下吧

      歡迎前來交流探討: 企鵝群568015492?
      <https://shang.qq.com/wpa/qunwpa?idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d>

      目錄

      https://www.cnblogs.com/bfyx/p/11364884.html
      <https://www.cnblogs.com/bfyx/p/11364884.html>

      準(zhǔn)備工作

      該控件將繼承基類控件UCControlBase,如果你還對UCControlBase不了解的下,

      請移步?(一)c#Winform自定義控件-基類控件 <https://www.cnblogs.com/bfyx/p/11361809.html>? 查看

      首先我們了解下要做的是什么,我們需要做一個(gè)可以自定義填充顏色,有圓角邊框,有角標(biāo)的按鈕

      開始

      添加一個(gè)用戶控件,命名為UCBtnExt ,繼承 UCControlBase

      先來看看我們按鈕需要支持的屬性吧
      1 #region 字段屬性 2 [Description("是否顯示角標(biāo)"), Category("自定義")] 3 public bool
      IsShowTips 4 { 5 get 6 { 7 return this.lblTips.Visible; 8 } 9 set 10 {
      11 this.lblTips.Visible = value; 12 } 13 } 14 15 [Description("角標(biāo)文字"),
      Category("自定義")] 16 public string TipsText 17 { 18 get 19 { 20 return this
      .lblTips.Text;21 } 22 set 23 { 24 this.lblTips.Text = value; 25 } 26 } 27 28
      private Color _btnBackColor = Color.White; 29 [Description("按鈕背景色"), Category("
      自定義")] 30 public Color BtnBackColor 31 { 32 get { return _btnBackColor; } 33
      set 34 { 35 _btnBackColor = value; 36 this.BackColor = value; 37 } 38 } 39 40
      private Color _btnForeColor = Color.Black; 41 /// <summary> 42 /// 按鈕字體顏色 43 ///
      </summary> 44 [Description("按鈕字體顏色"), Category("自定義")] 45 public Color
      BtnForeColor46 { 47 get { return _btnForeColor; } 48 set 49 { 50
      _btnForeColor = value; 51 this.lbl.ForeColor = value; 52 } 53 } 54 55 private
      Font _btnFont =new System.Drawing.Font("微軟雅黑", 12F,
      System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134
      )));56 /// <summary> 57 /// 按鈕字體 58 /// </summary> 59 [Description("按鈕字體"),
      Category("自定義")] 60 public Font BtnFont 61 { 62 get { return _btnFont; } 63 set
      64 { 65 _btnFont = value; 66 this.lbl.Font = value; 67 } 68 } 69 70 ///
      <summary> 71 /// 按鈕點(diǎn)擊事件 72 /// </summary> 73 [Description("按鈕點(diǎn)擊事件"), Category("
      自定義")] 74 public event EventHandler BtnClick; 75 76 private string _btnText; 77
      /// <summary> 78 /// 按鈕文字 79 /// </summary> 80 [Description("按鈕文字"), Category("
      自定義")] 81 public string BtnText 82 { 83 get { return _btnText; } 84 set 85 {
      86 _btnText = value; 87 lbl.Text = value; 88 } 89 } 90 #endregion
      有了屬性是不是就更明了呢

      還有最后關(guān)鍵的一點(diǎn)東西,就是按鈕的點(diǎn)擊事件
      1 private void lbl_MouseDown(object sender, MouseEventArgs e) 2 { 3 if (this
      .BtnClick !=null) 4 BtnClick(this, e); 5 }
      至此基本上就完工了,下面列出了完整的代碼
      1 // 版權(quán)所有 黃正輝 交流群:568015492 QQ:623128629 2 // 文件名稱:UCBtnExt.cs 3 //
      創(chuàng)建日期:2019-08-15 15:57:36 4 // 功能描述:按鈕 5 // 項(xiàng)目地址:
      https://gitee.com/kwwwvagaa/net_winform_custom_control 6 7 using System; 8
      using System.Collections.Generic; 9 using System.ComponentModel; 10 using
      System.Drawing; 11 using System.Data; 12 using System.Linq; 13 using
      System.Text; 14 using System.Windows.Forms; 15 16 namespace
      HZH_Controls.Controls 17 { 18 [DefaultEvent("BtnClick")] 19 public partial
      class UCBtnExt : UCControlBase 20 { 21 #region 字段屬性 22 [Description("是否顯示角標(biāo)"
      ), Category("自定義")] 23 public bool IsShowTips 24 { 25 get 26 { 27 return
      this.lblTips.Visible; 28 } 29 set 30 { 31 this.lblTips.Visible = value;
      32 } 33 } 34 35 [Description("角標(biāo)文字"), Category("自定義")] 36 public string
      TipsText 37 { 38 get 39 { 40 return this.lblTips.Text; 41 } 42 set 43
      { 44 this.lblTips.Text = value; 45 } 46 } 47 48 private Color
      _btnBackColor = Color.White; 49 [Description("按鈕背景色"), Category("自定義")] 50
      public Color BtnBackColor 51 { 52 get { return _btnBackColor; } 53 set 54
      { 55 _btnBackColor = value; 56 this.BackColor = value; 57 } 58 } 59 60
      private Color _btnForeColor = Color.Black; 61 /// <summary> 62 /// 按鈕字體顏色 63
      /// </summary> 64 [Description("按鈕字體顏色"), Category("自定義")] 65 public Color
      BtnForeColor 66 { 67 get { return _btnForeColor; } 68 set 69 { 70
      _btnForeColor = value; 71 this.lbl.ForeColor = value; 72 } 73 } 74 75
      private Font _btnFont = new System.Drawing.Font("微軟雅黑", 12F,
      System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134
      ))); 76 /// <summary> 77 /// 按鈕字體 78 /// </summary> 79 [Description("按鈕字體"),
      Category("自定義")] 80 public Font BtnFont 81 { 82 get { return _btnFont; } 83
      set 84 { 85 _btnFont = value; 86 this.lbl.Font = value; 87 } 88 } 89
      90 /// <summary> 91 /// 按鈕點(diǎn)擊事件 92 /// </summary> 93 [Description("按鈕點(diǎn)擊事件"),
      Category("自定義")] 94 public event EventHandler BtnClick; 95 96 private string
      _btnText; 97 /// <summary> 98 /// 按鈕文字 99 /// </summary> 100 [Description("
      按鈕文字"), Category("自定義")] 101 public string BtnText 102 { 103 get { return
      _btnText; }104 set 105 { 106 _btnText = value; 107 lbl.Text = value; 108 } 109
      }110 #endregion 111 public UCBtnExt() 112 { 113 InitializeComponent(); 114
      this.TabStop = false; 115 } 116 117 private void lbl_MouseDown(object sender,
      MouseEventArgs e)118 { 119 if (this.BtnClick != null) 120 BtnClick(this, e);
      121 } 122 } 123 } View Code 1 namespace HZH_Controls.Controls 2 { 3 public
      partial class UCBtnExt 4 { 5 /// <summary> 6 /// 必需的設(shè)計(jì)器變量。 7 /// </summary>
      8 private System.ComponentModel.IContainer components = null; 9 10 ///
      <summary> 11 /// 清理所有正在使用的資源。 12 /// </summary> 13 /// <param
      name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 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 組件設(shè)計(jì)器生成的代碼 24 25 /// <summary> 26 /// 設(shè)計(jì)器支持所需的方法 - 不要 27 ///
      使用代碼編輯器修改此方法的內(nèi)容。 28 /// </summary> 29 private void InitializeComponent() 30 {
      31 this.components = new System.ComponentModel.Container(); 32
      System.ComponentModel.ComponentResourceManager resources =new
      System.ComponentModel.ComponentResourceManager(typeof(UCBtnExt)); 33 this.lbl =
      new System.Windows.Forms.Label(); 34 this.lblTips = new
      System.Windows.Forms.Label(); 35 this.imageList1 = new
      System.Windows.Forms.ImageList(this.components); 36 this.SuspendLayout(); 37
      // 38 // lbl 39 // 40 this.lbl.BackColor = System.Drawing.Color.Transparent;
      41 this.lbl.Dock = System.Windows.Forms.DockStyle.Fill; 42 this.lbl.Font = new
      System.Drawing.Font("微軟雅黑", 12F, System.Drawing.FontStyle.Regular,
      System.Drawing.GraphicsUnit.Point, ((byte)(134))); 43 this.lbl.ImageAlign =
      System.Drawing.ContentAlignment.MiddleLeft; 44 this.lbl.Location = new
      System.Drawing.Point(0, 0); 45 this.lbl.Name = "lbl"; 46 this.lbl.Size = new
      System.Drawing.Size(184, 60); 47 this.lbl.TabIndex = 0; 48 this.lbl.Text = "
      自定義按鈕"; 49 this.lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
      50 this.lbl.MouseDown += new System.Windows.Forms.MouseEventHandler(this
      .lbl_MouseDown); 51 // 52 // lblTips 53 // 54 this.lblTips.Anchor =
      ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top |
      System.Windows.Forms.AnchorStyles.Right))); 55 this.lblTips.BackColor =
      System.Drawing.Color.Transparent; 56 this.lblTips.Font = new
      System.Drawing.Font("Arial Unicode MS", 12F, System.Drawing.FontStyle.Regular,
      System.Drawing.GraphicsUnit.Point, ((byte)(134))); 57 this.lblTips.ForeColor =
      System.Drawing.Color.White; 58 this.lblTips.ImageIndex = 0; 59 this
      .lblTips.ImageList =this.imageList1; 60 this.lblTips.Location = new
      System.Drawing.Point(158, 0); 61 this.lblTips.Name = "lblTips"; 62 this
      .lblTips.Size =new System.Drawing.Size(24, 24); 63 this.lblTips.TabIndex = 1;
      64 this.lblTips.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; 65
      this.lblTips.Visible = false; 66 // 67 // imageList1 68 // 69 this
      .imageList1.ImageStream =
      ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("
      imageList1.ImageStream"))); 70 this.imageList1.TransparentColor =
      System.Drawing.Color.Transparent; 71 this.imageList1.Images.SetKeyName(0, "
      tips.png"); 72 // 73 // UCBtnExt 74 // 75 this.AutoScaleMode =
      System.Windows.Forms.AutoScaleMode.None; 76 this.BackColor =
      System.Drawing.Color.Transparent; 77 this.ConerRadius = 5; 78 this
      .Controls.Add(this.lblTips); 79 this.Controls.Add(this.lbl); 80 this.Cursor =
      System.Windows.Forms.Cursors.Hand; 81 this.FillColor =
      System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((
      int)(((byte)(247))))); 82 this.IsShowRect = true; 83 this.IsRadius = true; 84
      this.Margin = new System.Windows.Forms.Padding(0); 85 this.Name = "UCBtnExt";
      86 this.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int
      )(((byte)(247)))), ((int)(((byte)(247))))); 87 this.Size = new
      System.Drawing.Size(184, 60); 88 this.ResumeLayout(false); 89 90 } 91 92
      #endregion 93 94 public System.Windows.Forms.Label lbl; 95 private
      System.Windows.Forms.Label lblTips; 96 private System.Windows.Forms.ImageList
      imageList1; 97 98 99 } 100 } View Code
      用處及效果

      用處:按鈕有什么用,我想我不用解釋了吧

      效果:



      ?

      最后的話

      如果你喜歡的話,請到?https://gitee.com/kwwwvagaa/net_winform_custom_control
      <https://gitee.com/kwwwvagaa/net_winform_custom_control>?點(diǎn)個(gè)星星吧

      友情鏈接
      ioDraw流程圖
      API參考文檔
      OK工具箱
      云服務(wù)器優(yōu)惠
      阿里云優(yōu)惠券
      騰訊云優(yōu)惠券
      京東云優(yōu)惠券
      站點(diǎn)信息
      問題反饋
      郵箱:[email protected]
      QQ群:637538335
      關(guān)注微信

        <ul id="qxxfc"><fieldset id="qxxfc"><tr id="qxxfc"></tr></fieldset></ul>
          影视一区 | 欧美日韩一区二区三区不卡视频 | 欧美怡红院院 | 偷拍与自拍视频 | 久久调教视频 | 公车上强行被灌满脓液h视频 | 国产福利视频在线播放 | 野外一级片 | 永久免费人成在线看不良视频 | 熟年交尾五十路视频在线播放 |