前提
入行已经 7,8 年了, 一直想做一套漂亮点的自定义控件, 于是就有了本系列文章.
GitHub: https://github.com/kwwwvagaa/NetWinformControl
码云:
如果觉得写的还行, 请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群 568015492
麻烦博客下方点个 [推荐] , 谢谢
- NuGet
- Install-Package HZH_Controls
目录
https://www.cnblogs.com/bfyx/p/11364884.html
用处及效果
准备工作
这个控件将继承自 (三)c#Winform 自定义控件 - 有图标的按钮, 如不了解, 请移步查看
开始
添加一个用户控件 UCDropDownBtn, 继承自 UCBtnImg
处理一些属性
- Forms.FrmAnchor _frmAnchor;
- private int _dropPanelHeight = -1;
- public new event EventHandler BtnClick;
- [Description("下拉框高度"), Category("自定义")]
- public int DropPanelHeight
- {
- get { return _dropPanelHeight; }
- set { _dropPanelHeight = value; }
- }
- private string[] btns ;
- [Description("按钮"), Category("自定义")]
- public string[] Btns
- {
- get { return btns; }
- set { btns = value; }
- }
- [Obsolete("不再可用的属性")]
- [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
- public override Image Image
- {
- get;
- set;
- }
- [Obsolete("不再可用的属性")]
- [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
- public override ContentAlignment ImageAlign
- {
- get;
- set;
- }
点击时候显示下拉框
- void UCDropDownBtn_BtnClick(object sender, EventArgs e)
- {
- if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false)
- {
- if (Btns != null && Btns.Length> 0)
- {
- int intRow = 0;
- int intCom = 1;
- var p = this.PointToScreen(this.Location);
- while (true)
- {
- int intScreenHeight = Screen.PrimaryScreen.Bounds.Height;
- if ((p.Y + this.Height + Btns.Length / intCom * 50 <intScreenHeight || p.Y - Btns.Length / intCom * 50> 0)
- && (_dropPanelHeight <= 0 ? true : (Btns.Length / intCom * 50 <= _dropPanelHeight)))
- {
- intRow = Btns.Length / intCom + (Btns.Length % intCom != 0 ? 1 : 0);
- break;
- }
- intCom++;
- }
- UCTimePanel ucTime = new UCTimePanel();
- ucTime.IsShowBorder = true;
- int intWidth = this.Width / intCom;
- Size size = new Size(intCom * intWidth, intRow * 50);
- ucTime.Size = size;
- ucTime.FirstEvent = true;
- ucTime.SelectSourceEvent += ucTime_SelectSourceEvent;
- ucTime.Row = intRow;
- ucTime.Column = intCom;
- List<KeyValuePair<string, string>> lst = new List<KeyValuePair<string, string>>();
- foreach (var item in Btns)
- {
- lst.Add(new KeyValuePair<string, string>(item, item));
- }
- ucTime.Source = lst;
- _frmAnchor = new Forms.FrmAnchor(this, ucTime);
- _frmAnchor.Load += (a, b) => { (a as Form).Size = size; };
- _frmAnchor.Show(this.FindForm());
- }
- }
- else
- {
- _frmAnchor.Close();
- }
- }
处理一下按钮事件
- void ucTime_SelectSourceEvent(object sender, EventArgs e)
- {
- if (_frmAnchor != null && !_frmAnchor.IsDisposed && _frmAnchor.Visible)
- {
- _frmAnchor.Close();
- if (BtnClick != null)
- {
- BtnClick(sender.ToString(), e);
- }
- }
- }
完整代码
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace HZH_Controls.Controls.Btn
- {
- [DefaultEvent("BtnClick")]
- public partial class UCDropDownBtn : UCBtnImg
- {
- Forms.FrmAnchor _frmAnchor;
- private int _dropPanelHeight = -1;
- public new event EventHandler BtnClick;
- [Description("下拉框高度"), Category("自定义")]
- public int DropPanelHeight
- {
- get { return _dropPanelHeight; }
- set { _dropPanelHeight = value; }
- }
- private string[] btns ;
- [Description("按钮"), Category("自定义")]
- public string[] Btns
- {
- get { return btns; }
- set { btns = value; }
- }
- [Obsolete("不再可用的属性")]
- [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
- public override Image Image
- {
- get;
- set;
- }
- [Obsolete("不再可用的属性")]
- [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
- public override ContentAlignment ImageAlign
- {
- get;
- set;
- }
- public UCDropDownBtn()
- {
- InitializeComponent();
- IsShowTips = false;
- this.lbl.Image=Properties.Resources.ComboBox;
- this.lbl.ImageAlign = ContentAlignment.MiddleRight;
- base.BtnClick += UCDropDownBtn_BtnClick;
- }
- void UCDropDownBtn_BtnClick(object sender, EventArgs e)
- {
- if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false)
- {
- if (Btns != null && Btns.Length> 0)
- {
- int intRow = 0;
- int intCom = 1;
- var p = this.PointToScreen(this.Location);
- while (true)
- {
- int intScreenHeight = Screen.PrimaryScreen.Bounds.Height;
- if ((p.Y + this.Height + Btns.Length / intCom * 50 <intScreenHeight || p.Y - Btns.Length / intCom * 50> 0)
- && (_dropPanelHeight <= 0 ? true : (Btns.Length / intCom * 50 <= _dropPanelHeight)))
- {
- intRow = Btns.Length / intCom + (Btns.Length % intCom != 0 ? 1 : 0);
- break;
- }
- intCom++;
- }
- UCTimePanel ucTime = new UCTimePanel();
- ucTime.IsShowBorder = true;
- int intWidth = this.Width / intCom;
- Size size = new Size(intCom * intWidth, intRow * 50);
- ucTime.Size = size;
- ucTime.FirstEvent = true;
- ucTime.SelectSourceEvent += ucTime_SelectSourceEvent;
- ucTime.Row = intRow;
- ucTime.Column = intCom;
- List<KeyValuePair<string, string>> lst = new List<KeyValuePair<string, string>>();
- foreach (var item in Btns)
- {
- lst.Add(new KeyValuePair<string, string>(item, item));
- }
- ucTime.Source = lst;
- _frmAnchor = new Forms.FrmAnchor(this, ucTime);
- _frmAnchor.Load += (a, b) => { (a as Form).Size = size; };
- _frmAnchor.Show(this.FindForm());
- }
- }
- else
- {
- _frmAnchor.Close();
- }
- }
- void ucTime_SelectSourceEvent(object sender, EventArgs e)
- {
- if (_frmAnchor != null && !_frmAnchor.IsDisposed && _frmAnchor.Visible)
- {
- _frmAnchor.Close();
- if (BtnClick != null)
- {
- BtnClick(sender.ToString(), e);
- }
- }
- }
- }
- }
- View Code
- namespace HZH_Controls.Controls.Btn
- {
- partial class UCDropDownBtn
- {
- /// <summary>
- /// 必需的设计器变量.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// 清理所有正在使用的资源.
- /// </summary>
- /// <param name="disposing"> 如果应释放托管资源, 为 true; 否则为 false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region 组件设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要
- /// 使用代码编辑器修改此方法的内容.
- /// </summary>
- private void InitializeComponent()
- {
- System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UCDropDownBtn));
- this.SuspendLayout();
- //
- // lbl
- //
- this.lbl.Font = new System.Drawing.Font("微软雅黑", 14F);
- this.lbl.ForeColor = System.Drawing.Color.White;
- this.lbl.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
- this.lbl.ImageList = null;
- this.lbl.Text = "自定义按钮";
- this.lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
- //
- // UCDropDownBtn
- //
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
- this.BtnFont = new System.Drawing.Font("微软雅黑", 14F);
- this.BtnForeColor = System.Drawing.Color.White;
- this.ForeColor = System.Drawing.Color.White;
- this.Image = ((System.Drawing.Image)(resources.GetObject("$this.Image")));
- this.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
- this.Margin = new System.Windows.Forms.Padding(2);
- this.Name = "UCDropDownBtn";
- this.ResumeLayout(false);
- }
- #endregion
- }
- }
- View Code
最后的话
如果你喜欢的话, 请到 点个星星吧
来源: https://www.cnblogs.com/bfyx/p/11417291.html