前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
本系列文章将讲解各种控件的开发及思路,欢迎各位批评指正。
此系列控件开发教程将全部在原生控件基础上进行重绘开发,目标的扁平化、漂亮、支持触屏。
如果有什么好的建议也可以评论留言来交流。
源码地址:
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
目录
http://toutiao.com/item/6824291838963220999/
准备工作
键盘控件目前分为4中,英文键盘,数字键盘,支付键盘,手写键盘
键盘一般用在到文本框弹出的键盘,那么为什么到现在还没有看到文本框的影子呢?因为文本框的某些功能牵扯到了自定义窗体,所以准备在自定义窗体介绍之后再来说文本框。
本篇文章介绍数字键盘和支付键盘,手写键盘将在后面文本框控件介绍是提及到,此处不单独介绍
开始
首先来说数字键盘
添加用户控件,命名UCKeyBorderNum
全部功能代码如下,没有太多东西
1 private bool useCustomEvent = false; 2 /// 3 /// 是否使用自定义的事件来接收按键,当为true时将不再向系统发送按键请求 4 /// 5 [Description("是否使用自定义的事件来接收按键,当为true时将不再向系统发送按键请求"), Category("自定义")] 6 public bool UseCustomEvent 7 { 8 get { return useCustomEvent; } 9 set { useCustomEvent = value; }10 }11 [Description("数字点击事件"), Category("自定义")]12 public event EventHandler NumClick;13 [Description("删除点击事件"), Category("自定义")]14 public event EventHandler BackspaceClick;15 [Description("回车点击事件"), Category("自定义")]16 public event EventHandler EnterClick;17 public UCKeyBorderNum()18 {19 InitializeComponent();20 }21 22 private void Num_MouseDown(object sender, MouseEventArgs e)23 {24 if (NumClick != null)25 {26 NumClick(sender, e);27 }28 if (useCustomEvent)29 return;30 Label lbl = sender as Label;31 SendKeys.Send(lbl.Tag.ToString());32 }33 34 private void Backspace_MouseDown(object sender, MouseEventArgs e)35 {36 if (BackspaceClick != null)37 {38 BackspaceClick(sender, e);39 }40 if (useCustomEvent)41 return;42 Label lbl = sender as Label;43 SendKeys.Send("{BACKSPACE}");44 }45 46 private void Enter_MouseDown(object sender, MouseEventArgs e)47 {48 if (EnterClick != null)49 {50 EnterClick(sender, e);51 }52 if (useCustomEvent)53 return;54 SendKeys.Send("{ENTER}");55 }
计效果
下面说支付键盘,这个可能就比较小众的键盘了,支持根据输入金额自动计算可能付款金额
添加用户控件,命名UCKeyBorderPay
同样的东西不多,主要的就一个计算预估付款金额
1 [Description("数字点击事件"), Category("自定义")] 2 public event EventHandler NumClick; 3 4 [Description("取消点击事件"), Category("自定义")] 5 public event EventHandler CancelClick; 6 7 [Description("确定点击事件"), Category("自定义")] 8 public event EventHandler OKClick; 9 10 [Description("删除点击事件"), Category("自定义")] 11 public event EventHandler BackspaceClick; 12 13 [Description("金额点击事件"), Category("自定义")] 14 public event EventHandler MoneyClick; 15 public UCKeyBorderPay() 16 { 17 InitializeComponent(); 18 } 19 20 #region 设置快速付款金额 21 /// 22 /// 功能描述:设置快速付款金额 23 /// 作 者:HZH 24 /// 创建日期:2019-03-07 11:41:04 25 /// 任务编号:POS 26 /// 27 /// SorceMoney 28 public void SetPayMoney(decimal SorceMoney) 29 { 30 List list = new List(); 31 decimal d = Math.Ceiling(SorceMoney); 32 if (SorceMoney > 0m) 33 { 34 if (SorceMoney < 5m) 35 { 36 list.Add(5m); 37 list.Add(10m); 38 list.Add(20m); 39 list.Add(50m); 40 } 41 else if (SorceMoney < 10m) 42 { 43 list.Add(10m); 44 list.Add(20m); 45 list.Add(50m); 46 list.Add(100m); 47 } 48 else 49 { 50 int num = Convert.ToInt32(d % 10m); 51 int num2 = Convert.ToInt32(Math.Floor(d / 10m) % 10m); 52 int num3 = Convert.ToInt32(Math.Floor(d / 100m)); 53 int num4; 54 if (num < 5) 55 { 56 num4 = num2 * 10 + 5; 57 list.Add(num4 + num3 * 100); 58 num4 = (num2 + 1) * 10; 59 list.Add(num4 + num3 * 100); 60 } 61 else 62 { 63 num4 = (num2 + 1) * 10; 64 list.Add(num4 + num3 * 100); 65 } 66 if (num4 >= 0 && num4 < 10) 67 { 68 num4 = 10; 69 if (list.Count < 4) 70 { 71 list.Add(num4 + num3 * 100); 72 } 73 num4 = 20; 74 if (list.Count < 4) 75 { 76 list.Add(num4 + num3 * 100); 77 } 78 num4 = 50; 79 if (list.Count < 4) 80 { 81 list.Add(num4 + num3 * 100); 82 } 83 num4 = 100; 84 if (list.Count < 4) 85 { 86 list.Add(num4 + num3 * 100); 87 } 88 } 89 else if (num4 >= 10 && num4 < 20) 90 { 91 num4 = 20; 92 if (list.Count < 4) 93 { 94 list.Add(num4 + num3 * 100); 95 } 96 num4 = 50; 97 if (list.Count < 4) 98 { 99 list.Add(num4 + num3 * 100);100 }101 num4 = 100;102 if (list.Count < 4)103 {104 list.Add(num4 + num3 * 100);105 }106 }107 else if (num4 >= 20 && num4 < 50)108 {109 num4 = 50;110 if (list.Count < 4)111 {112 list.Add(num4 + num3 * 100);113 }114 num4 = 100;115 if (list.Count < 4)116 {117 list.Add(num4 + num3 * 100);118 }119 }120 else if (num4 < 100)121 {122 num4 = 100;123 if (list.Count < 4)124 {125 list.Add(num4 + num3 * 100);126 }127 }128 }129 }130 SetFastMoneyToContrl(list);131 }132 #endregion133 134 private void SetFastMoneyToContrl(List values)135 {136 List lbl = new List() { lblFast1, lblFast2, lblFast3, lblFast4 };137 lblFast1.Tag = lblFast1.Text = "";138 lblFast2.Tag = lblFast2.Text = "";139 lblFast3.Tag = lblFast3.Text = "";140 lblFast4.Tag = lblFast4.Text = "";141 for (int i = 0; i < lbl.Count && i < values.Count; i++)142 {143 if (values[i].ToString("0.##").Length < 4)144 {145 lbl[i].Font = new System.Drawing.Font("Arial Unicode MS", 30F);146 }147 else148 {149 Graphics graphics = lbl[i].CreateGraphics();150 for (int j = 0; j < 5; j++)151 {152 SizeF sizeF = graphics.MeasureString(values[i].ToString("0.##"), new System.Drawing.Font("Arial Unicode MS", 30 - j * 5), 100, StringFormat.GenericTypographic);153 if (sizeF.Width <= lbl[i].Width - 20)154 {155 lbl[i].Font = new System.Drawing.Font("Arial Unicode MS", 30 - j * 5);156 break;157 }158 }159 graphics.Dispose();160 }161 lbl[i].Tag = lbl[i].Text = values[i].ToString("0.##");162 }163 }164 private void Num_MouseDown(object sender, MouseEventArgs e)165 {166 if (NumClick != null)167 NumClick((sender as Label).Tag, e);168 }169 170 private void Backspace_MouseDown(object sender, MouseEventArgs e)171 {172 if (BackspaceClick != null)173 BackspaceClick((sender as Label).Tag, e);174 }175 176 private void Cancel_MouseDown(object sender, MouseEventArgs e)177 {178 if (CancelClick != null)179 CancelClick((sender as Label).Tag, e);180 }181 182 private void OK_MouseDown(object sender, MouseEventArgs e)183 {184 if (OKClick != null)185 OKClick((sender as Label).Tag, e);186 }187 188 private void Money_MouseDown(object sender, MouseEventArgs e)189 {190 if (MoneyClick != null)191 MoneyClick((sender as Label).Tag, e);192 }193 194 public void Money1Click()195 {196 Money_MouseDown(lblFast1, null);197 }198 199 public void Money2Click()200 {201 Money_MouseDown(lblFast2, null);202 }203 204 public void Money3Click()205 {206 Money_MouseDown(lblFast3, null);207 }208 209 public void Money4Click()210 {211 Money_MouseDown(lblFast4, null);212 }
计效果
那4个空白的位置就是用来填充预估付款金额的
用处及效果
使用方法将在后面的文本框处详细介绍