欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

(三十一)c#Winform自定义控件-文本框(四)

程序员文章站 2022-06-05 20:32:47
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 如果觉得写的还行,请点个 star 支持一下吧 欢迎前来交流探讨: 企鹅群568015492 目录 ......

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

开源地址:

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 

目录

准备工作

终于到文本框了,文本框将包含原文本框扩展,透明文本框,数字输入文本框,带边框文本框

本文将讲解带边框文本框,可选弹出键盘样式,继承自控件基类uccontrolbase

同时用到了无焦点窗体和键盘,如果你还没有了解,请前往查看

(一)c#winform自定义控件-基类控件

(十九)c#winform自定义控件-停靠窗体

(十五)c#winform自定义控件-键盘(二)

(十四)c#winform自定义控件-键盘(一)

开始

添加用户控件,命名uctextboxex,继承自uccontrolbase

属性

  1 private bool m_isshowclearbtn = true;
  2         int m_intselectionstart = 0;
  3         int m_intselectionlength = 0;
  4         /// <summary>
  5         /// 功能描述:是否显示清理按钮
  6         /// 作  者:hzh
  7         /// 创建日期:2019-02-28 16:13:52
  8         /// </summary>        
  9         [description("是否显示清理按钮"), category("自定义")]
 10         public bool isshowclearbtn
 11         {
 12             get { return m_isshowclearbtn; }
 13             set
 14             {
 15                 m_isshowclearbtn = value;
 16                 if (value)
 17                 {
 18                     btnclear.visible = !(txtinput.text == "\r\n") && !string.isnullorempty(txtinput.text);
 19                 }
 20                 else
 21                 {
 22                     btnclear.visible = false;
 23                 }
 24             }
 25         }
 26 
 27         private bool m_isshowsearchbtn = false;
 28         /// <summary>
 29         /// 是否显示查询按钮
 30         /// </summary>
 31 
 32         [description("是否显示查询按钮"), category("自定义")]
 33         public bool isshowsearchbtn
 34         {
 35             get { return m_isshowsearchbtn; }
 36             set
 37             {
 38                 m_isshowsearchbtn = value;
 39                 btnsearch.visible = value;
 40             }
 41         }
 42 
 43         [description("是否显示键盘"), category("自定义")]
 44         public bool isshowkeyboard
 45         {
 46             get
 47             {
 48                 return btnkeybord.visible;
 49             }
 50             set
 51             {
 52                 btnkeybord.visible = value;
 53             }
 54         }
 55         [description("字体"), category("自定义")]
 56         public new font font
 57         {
 58             get
 59             {
 60                 return this.txtinput.font;
 61             }
 62             set
 63             {
 64                 this.txtinput.font = value;
 65             }
 66         }
 67 
 68         [description("输入类型"), category("自定义")]
 69         public textinputtype inputtype
 70         {
 71             get { return txtinput.inputtype; }
 72             set { txtinput.inputtype = value; }
 73         }
 74 
 75         /// <summary>
 76         /// 水印文字
 77         /// </summary>
 78         [description("水印文字"), category("自定义")]
 79         public string prompttext
 80         {
 81             get
 82             {
 83                 return this.txtinput.prompttext;
 84             }
 85             set
 86             {
 87                 this.txtinput.prompttext = value;
 88             }
 89         }
 90 
 91         [description("水印字体"), category("自定义")]
 92         public font promptfont
 93         {
 94             get
 95             {
 96                 return this.txtinput.promptfont;
 97             }
 98             set
 99             {
100                 this.txtinput.promptfont = value;
101             }
102         }
103 
104         [description("水印颜色"), category("自定义")]
105         public color promptcolor
106         {
107             get
108             {
109                 return this.txtinput.promptcolor;
110             }
111             set
112             {
113                 this.txtinput.promptcolor = value;
114             }
115         }
116 
117         /// <summary>
118         /// 获取或设置一个值,该值指示当输入类型inputtype=regex时,使用的正则表达式。
119         /// </summary>
120         [description("获取或设置一个值,该值指示当输入类型inputtype=regex时,使用的正则表达式。")]
121         public string regexpattern
122         {
123             get
124             {
125                 return this.txtinput.regexpattern;
126             }
127             set
128             {
129                 this.txtinput.regexpattern = value;
130             }
131         }
132         /// <summary>
133         /// 当inputtype为数字类型时,能输入的最大值
134         /// </summary>
135         [description("当inputtype为数字类型时,能输入的最大值。")]
136         public decimal maxvalue
137         {
138             get
139             {
140                 return this.txtinput.maxvalue;
141             }
142             set
143             {
144                 this.txtinput.maxvalue = value;
145             }
146         }
147         /// <summary>
148         /// 当inputtype为数字类型时,能输入的最小值
149         /// </summary>
150         [description("当inputtype为数字类型时,能输入的最小值。")]
151         public decimal minvalue
152         {
153             get
154             {
155                 return this.txtinput.minvalue;
156             }
157             set
158             {
159                 this.txtinput.minvalue = value;
160             }
161         }
162         /// <summary>
163         /// 当inputtype为数字类型时,能输入的最小值
164         /// </summary>
165         [description("当inputtype为数字类型时,小数位数。")]
166         public int declength
167         {
168             get
169             {
170                 return this.txtinput.declength;
171             }
172             set
173             {
174                 this.txtinput.declength = value;
175             }
176         }
177 
178         private keyboardtype keyboardtype = keyboardtype.uckeyborderall_en;
179         [description("键盘打开样式"), category("自定义")]
180         public keyboardtype keyboardtype
181         {
182             get { return keyboardtype; }
183             set { keyboardtype = value; }
184         }
185         [description("查询按钮点击事件"), category("自定义")]
186         public event eventhandler searchclick;
187 
188         [description("文本改变事件"), category("自定义")]
189         public new event eventhandler textchanged;
190         [description("键盘按钮点击事件"), category("自定义")]
191         public event eventhandler keyboardclick;
192 
193         [description("文本"), category("自定义")]
194         public string inputtext
195         {
196             get
197             {
198                 return txtinput.text;
199             }
200             set
201             {
202                 txtinput.text = value;
203             }
204         }
205 
206         private bool isfocuscolor = true;
207         [description("获取焦点是否变色"), category("自定义")]
208         public bool isfocuscolor
209         {
210             get { return isfocuscolor; }
211             set { isfocuscolor = value; }
212         }
213         private color _fillcolor;
214         public new color fillcolor
215         {
216             get
217             {
218                 return _fillcolor;
219             }
220             set
221             {
222                 _fillcolor = value;
223                 base.fillcolor = value;
224                 this.txtinput.backcolor = value;
225             }
226         }

一些事件

  1 void uctextboxex_sizechanged(object sender, eventargs e)
  2         {
  3             this.txtinput.location = new point(this.txtinput.location.x, (this.height - txtinput.height) / 2);
  4         }
  5 
  6 
  7         private void txtinput_textchanged(object sender, eventargs e)
  8         {
  9             if (m_isshowclearbtn)
 10             {
 11                 btnclear.visible = !(txtinput.text == "\r\n") && !string.isnullorempty(txtinput.text);
 12             }
 13             if (textchanged != null)
 14             {
 15                 textchanged(sender, e);
 16             }
 17         }
 18 
 19         private void btnclear_mousedown(object sender, mouseeventargs e)
 20         {
 21             txtinput.clear();
 22             txtinput.focus();
 23         }
 24 
 25         private void btnsearch_mousedown(object sender, mouseeventargs e)
 26         {
 27             if (searchclick != null)
 28             {
 29                 searchclick(sender, e);
 30             }
 31         }
 32         forms.frmanchor m_frmanchor;
 33         private void btnkeybord_mousedown(object sender, mouseeventargs e)
 34         {
 35             m_intselectionstart = this.txtinput.selectionstart;
 36             m_intselectionlength = this.txtinput.selectionlength;
 37             this.findform().activecontrol = this;
 38             this.findform().activecontrol = this.txtinput;
 39             switch (keyboardtype)
 40             {
 41                 case keyboardtype.uckeyborderall_en:
 42                     if (m_frmanchor == null)
 43                     {
 44                         if (m_frmanchor == null)
 45                         {
 46                             uckeyborderall key = new uckeyborderall();
 47                             key.chartype = keyborderchartype.char;
 48                             key.retractclike += (a, b) =>
 49                             {
 50                                 m_frmanchor.hide();
 51                             };
 52                             m_frmanchor = new forms.frmanchor(this, key);
 53                             m_frmanchor.visiblechanged += (a, b) =>
 54                             {
 55                                 if (m_frmanchor.visible)
 56                                 {
 57                                     this.txtinput.selectionstart = m_intselectionstart;
 58                                     this.txtinput.selectionlength = m_intselectionlength;
 59                                 }
 60                             };
 61                         }
 62                     }
 63                     break;
 64                 case keyboardtype.uckeyborderall_num:
 65 
 66                     if (m_frmanchor == null)
 67                     {
 68                         uckeyborderall key = new uckeyborderall();
 69                         key.chartype = keyborderchartype.number;
 70                         key.retractclike += (a, b) =>
 71                         {
 72                             m_frmanchor.hide();
 73                         };
 74                         m_frmanchor = new forms.frmanchor(this, key);
 75                         m_frmanchor.visiblechanged += (a, b) =>
 76                         {
 77                             if (m_frmanchor.visible)
 78                             {
 79                                 this.txtinput.selectionstart = m_intselectionstart;
 80                                 this.txtinput.selectionlength = m_intselectionlength;
 81                             }
 82                         };
 83                     }
 84 
 85                     break;
 86                 case keyboardtype.uckeybordernum:
 87                     if (m_frmanchor == null)
 88                     {
 89                         uckeybordernum key = new uckeybordernum();
 90                         m_frmanchor = new forms.frmanchor(this, key);
 91                         m_frmanchor.visiblechanged += (a, b) =>
 92                         {
 93                             if (m_frmanchor.visible)
 94                             {
 95                                 this.txtinput.selectionstart = m_intselectionstart;
 96                                 this.txtinput.selectionlength = m_intselectionlength;
 97                             }
 98                         };
 99                     }
100                     break;
101                 case hzh_controls.controls.keyboardtype.uckeyborderhand:
102 
103                     m_frmanchor = new forms.frmanchor(this, new size(504, 361));
104                     m_frmanchor.visiblechanged += m_frmanchor_visiblechanged;
105                     m_frmanchor.disposed += m_frmanchor_disposed;
106                     panel p = new panel();
107                     p.dock = dockstyle.fill;
108                     p.name = "keyborder";
109                     m_frmanchor.controls.add(p);
110 
111                     ucbtnext btndelete = new ucbtnext();
112                     btndelete.name = "btndelete";
113                     btndelete.size = new size(80, 28);
114                     btndelete.fillcolor = color.white;
115                     btndelete.isradius = false;
116                     btndelete.conerradius = 1;
117                     btndelete.isshowrect = true;
118                     btndelete.rectcolor = color.fromargb(189, 197, 203);
119                     btndelete.location = new point(198, 332);
120                     btndelete.btnfont = new system.drawing.font("微软雅黑", 8);
121                     btndelete.btntext = "删除";
122                     btndelete.btnclick += (a, b) =>
123                     {
124                         sendkeys.send("{backspace}");
125                     };
126                     m_frmanchor.controls.add(btndelete);
127                     btndelete.bringtofront();
128 
129                     ucbtnext btnenter = new ucbtnext();
130                     btnenter.name = "btnenter";
131                     btnenter.size = new size(82, 28);
132                     btnenter.fillcolor = color.white;
133                     btnenter.isradius = false;
134                     btnenter.conerradius = 1;
135                     btnenter.isshowrect = true;
136                     btnenter.rectcolor = color.fromargb(189, 197, 203);
137                     btnenter.location = new point(278, 332);
138                     btnenter.btnfont = new system.drawing.font("微软雅黑", 8);
139                     btnenter.btntext = "确定";
140                     btnenter.btnclick += (a, b) =>
141                     {
142                         sendkeys.send("{enter}");
143                         m_frmanchor.hide();
144                     };
145                     m_frmanchor.controls.add(btnenter);
146                     btnenter.bringtofront();
147                     m_frmanchor.visiblechanged += (a, b) =>
148                     {
149                         if (m_frmanchor.visible)
150                         {
151                             this.txtinput.selectionstart = m_intselectionstart;
152                             this.txtinput.selectionlength = m_intselectionlength;
153                         }
154                     };
155                     break;
156             }
157             if (!m_frmanchor.visible)
158                 m_frmanchor.show(this.findform());
159             if (keyboardclick != null)
160             {
161                 keyboardclick(sender, e);
162             }
163         }
164 
165         void m_frmanchor_disposed(object sender, eventargs e)
166         {
167             if (m_handappwin != intptr.zero)
168             {
169                 if (m_handpwin != null && !m_handpwin.hasexited)
170                     m_handpwin.kill();
171                 m_handpwin = null;
172                 m_handappwin = intptr.zero;
173             }
174         }
175 
176 
177         intptr m_handappwin;
178         process m_handpwin = null;
179         string m_handexename = system.io.path.combine(system.appdomain.currentdomain.basedirectory, "handinput\\handinput.exe");
180 
181         void m_frmanchor_visiblechanged(object sender, eventargs e)
182         {
183             if (m_frmanchor.visible)
184             {
185                 var lstp = process.getprocessesbyname("handinput");
186                 if (lstp.length > 0)
187                 {
188                     foreach (var item in lstp)
189                     {
190                         item.kill();
191                     }
192                 }
193                 m_handappwin = intptr.zero;
194 
195                 if (m_handpwin == null)
196                 {
197                     m_handpwin = null;
198 
199                     m_handpwin = system.diagnostics.process.start(this.m_handexename);
200                     m_handpwin.waitforinputidle();
201                 }
202                 while (m_handpwin.mainwindowhandle == intptr.zero)
203                 {
204                     thread.sleep(10);
205                 }
206                 m_handappwin = m_handpwin.mainwindowhandle;
207                 control p = m_frmanchor.controls.find("keyborder", false)[0];
208                 setparent(m_handappwin, p.handle);
209                 controlhelper.setforegroundwindow(this.findform().handle);
210                 movewindow(m_handappwin, -111, -41, 626, 412, true);
211             }
212             else
213             {
214                 if (m_handappwin != intptr.zero)
215                 {
216                     if (m_handpwin != null && !m_handpwin.hasexited)
217                         m_handpwin.kill();
218                     m_handpwin = null;
219                     m_handappwin = intptr.zero;
220                 }
221             }
222         }
223 
224         private void uctextboxex_mousedown(object sender, mouseeventargs e)
225         {
226             this.activecontrol = txtinput;
227         }
228 
229         private void uctextboxex_load(object sender, eventargs e)
230         {
231             if (!enabled)
232             {
233                 base.fillcolor = color.fromargb(240, 240, 240);
234                 txtinput.backcolor = color.fromargb(240, 240, 240);
235             }
236             else
237             {
238                 fillcolor = _fillcolor;
239                 txtinput.backcolor = _fillcolor;
240             }
241         }
242         [dllimport("user32.dll", setlasterror = true)]
243         private static extern long setparent(intptr hwndchild, intptr hwndnewparent);
244 
245         [dllimport("user32.dll", setlasterror = true)]
246         private static extern bool movewindow(intptr hwnd, int x, int y, int cx, int cy, bool repaint);
247         [dllimport("user32.dll", entrypoint = "showwindow")]
248         private static extern bool showwindow(intptr hwnd, int ncmdshow);
249         [dllimport("user32.dll")]
250         private static extern bool setwindowpos(intptr hwnd, int hwndlnsertafter, int x, int y, int cx, int cy, uint flags);
251         private const int gwl_style = -16;
252         private const int ws_child = 0x40000000;//设置窗口属性为child
253 
254         [dllimport("user32.dll", entrypoint = "getwindowlong")]
255         public static extern int getwindowlong(intptr hwnd, int nindex);
256 
257         [dllimport("user32.dll", entrypoint = "setwindowlong")]
258         public static extern int setwindowlong(intptr hwnd, int nindex, int dwnewlong);
259 
260         [dllimport("user32.dll")]
261         private extern static intptr setactivewindow(intptr handle);

你也许注意到了m_frmanchor_visiblechanged事件,当键盘窗体显示的时候,启动手写输入软件(这里用了搜狗的手写),将手写软件窗体包含进键盘窗体中来实现手写功能

完整的代码

  1 // 版权所有  黄正辉  交流群:568015492   qq:623128629
  2 // 文件名称:uctextboxex.cs
  3 // 创建日期:2019-08-15 16:03:58
  4 // 功能描述:textbox
  5 // 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
  6 using system;
  7 using system.collections.generic;
  8 using system.componentmodel;
  9 using system.drawing;
 10 using system.data;
 11 using system.linq;
 12 using system.text;
 13 using system.windows.forms;
 14 using system.runtime.interopservices;
 15 using system.diagnostics;
 16 using system.threading;
 17 
 18 namespace hzh_controls.controls
 19 {
 20     [defaultevent("textchanged")]
 21     public partial class uctextboxex : uccontrolbase
 22     {
 23         private bool m_isshowclearbtn = true;
 24         int m_intselectionstart = 0;
 25         int m_intselectionlength = 0;
 26         /// <summary>
 27         /// 功能描述:是否显示清理按钮
 28         /// 作  者:hzh
 29         /// 创建日期:2019-02-28 16:13:52
 30         /// </summary>        
 31         [description("是否显示清理按钮"), category("自定义")]
 32         public bool isshowclearbtn
 33         {
 34             get { return m_isshowclearbtn; }
 35             set
 36             {
 37                 m_isshowclearbtn = value;
 38                 if (value)
 39                 {
 40                     btnclear.visible = !(txtinput.text == "\r\n") && !string.isnullorempty(txtinput.text);
 41                 }
 42                 else
 43                 {
 44                     btnclear.visible = false;
 45                 }
 46             }
 47         }
 48 
 49         private bool m_isshowsearchbtn = false;
 50         /// <summary>
 51         /// 是否显示查询按钮
 52         /// </summary>
 53 
 54         [description("是否显示查询按钮"), category("自定义")]
 55         public bool isshowsearchbtn
 56         {
 57             get { return m_isshowsearchbtn; }
 58             set
 59             {
 60                 m_isshowsearchbtn = value;
 61                 btnsearch.visible = value;
 62             }
 63         }
 64 
 65         [description("是否显示键盘"), category("自定义")]
 66         public bool isshowkeyboard
 67         {
 68             get
 69             {
 70                 return btnkeybord.visible;
 71             }
 72             set
 73             {
 74                 btnkeybord.visible = value;
 75             }
 76         }
 77         [description("字体"), category("自定义")]
 78         public new font font
 79         {
 80             get
 81             {
 82                 return this.txtinput.font;
 83             }
 84             set
 85             {
 86                 this.txtinput.font = value;
 87             }
 88         }
 89 
 90         [description("输入类型"), category("自定义")]
 91         public textinputtype inputtype
 92         {
 93             get { return txtinput.inputtype; }
 94             set { txtinput.inputtype = value; }
 95         }
 96 
 97         /// <summary>
 98         /// 水印文字
 99         /// </summary>
100         [description("水印文字"), category("自定义")]
101         public string prompttext
102         {
103             get
104             {
105                 return this.txtinput.prompttext;
106             }
107             set
108             {
109                 this.txtinput.prompttext = value;
110             }
111         }
112 
113         [description("水印字体"), category("自定义")]
114         public font promptfont
115         {
116             get
117             {
118                 return this.txtinput.promptfont;
119             }
120             set
121             {
122                 this.txtinput.promptfont = value;
123             }
124         }
125 
126         [description("水印颜色"), category("自定义")]
127         public color promptcolor
128         {
129             get
130             {
131                 return this.txtinput.promptcolor;
132             }
133             set
134             {
135                 this.txtinput.promptcolor = value;
136             }
137         }
138 
139         /// <summary>
140         /// 获取或设置一个值,该值指示当输入类型inputtype=regex时,使用的正则表达式。
141         /// </summary>
142         [description("获取或设置一个值,该值指示当输入类型inputtype=regex时,使用的正则表达式。")]
143         public string regexpattern
144         {
145             get
146             {
147                 return this.txtinput.regexpattern;
148             }
149             set
150             {
151                 this.txtinput.regexpattern = value;
152             }
153         }
154         /// <summary>
155         /// 当inputtype为数字类型时,能输入的最大值
156         /// </summary>
157         [description("当inputtype为数字类型时,能输入的最大值。")]
158         public decimal maxvalue
159         {
160             get
161             {
162                 return this.txtinput.maxvalue;
163             }
164             set
165             {
166                 this.txtinput.maxvalue = value;
167             }
168         }
169         /// <summary>
170         /// 当inputtype为数字类型时,能输入的最小值
171         /// </summary>
172         [description("当inputtype为数字类型时,能输入的最小值。")]
173         public decimal minvalue
174         {
175             get
176             {
177                 return this.txtinput.minvalue;
178             }
179             set
180             {
181                 this.txtinput.minvalue = value;
182             }
183         }
184         /// <summary>
185         /// 当inputtype为数字类型时,能输入的最小值
186         /// </summary>
187         [description("当inputtype为数字类型时,小数位数。")]
188         public int declength
189         {
190             get
191             {
192                 return this.txtinput.declength;
193             }
194             set
195             {
196                 this.txtinput.declength = value;
197             }
198         }
199 
200         private keyboardtype keyboardtype = keyboardtype.uckeyborderall_en;
201         [description("键盘打开样式"), category("自定义")]
202         public keyboardtype keyboardtype
203         {
204             get { return keyboardtype; }
205             set { keyboardtype = value; }
206         }
207         [description("查询按钮点击事件"), category("自定义")]
208         public event eventhandler searchclick;
209 
210         [description("文本改变事件"), category("自定义")]
211         public new event eventhandler textchanged;
212         [description("键盘按钮点击事件"), category("自定义")]
213         public event eventhandler keyboardclick;
214 
215         [description("文本"), category("自定义")]
216         public string inputtext
217         {
218             get
219             {
220                 return txtinput.text;
221             }
222             set
223             {
224                 txtinput.text = value;
225             }
226         }
227 
228         private bool isfocuscolor = true;
229         [description("获取焦点是否变色"), category("自定义")]
230         public bool isfocuscolor
231         {
232             get { return isfocuscolor; }
233             set { isfocuscolor = value; }
234         }
235         private color _fillcolor;
236         public new color fillcolor
237         {
238             get
239             {
240                 return _fillcolor;
241             }
242             set
243             {
244                 _fillcolor = value;
245                 base.fillcolor = value;
246                 this.txtinput.backcolor = value;
247             }
248         }
249         public uctextboxex()
250         {
251             initializecomponent();
252             txtinput.sizechanged += uctextboxex_sizechanged;
253             this.sizechanged += uctextboxex_sizechanged;
254             txtinput.gotfocus += (a, b) =>
255             {
256                 if (isfocuscolor)
257                     this.rectcolor = color.fromargb(78, 169, 255);
258             };
259             txtinput.lostfocus += (a, b) =>
260             {
261                 if (isfocuscolor)
262                     this.rectcolor = color.fromargb(220, 220, 220);
263             };
264         }
265 
266         void uctextboxex_sizechanged(object sender, eventargs e)
267         {
268             this.txtinput.location = new point(this.txtinput.location.x, (this.height - txtinput.height) / 2);
269         }
270 
271 
272         private void txtinput_textchanged(object sender, eventargs e)
273         {
274             if (m_isshowclearbtn)
275             {
276                 btnclear.visible = !(txtinput.text == "\r\n") && !string.isnullorempty(txtinput.text);
277             }
278             if (textchanged != null)
279             {
280                 textchanged(sender, e);
281             }
282         }
283 
284         private void btnclear_mousedown(object sender, mouseeventargs e)
285         {
286             txtinput.clear();
287             txtinput.focus();
288         }
289 
290         private void btnsearch_mousedown(object sender, mouseeventargs e)
291         {
292             if (searchclick != null)
293             {
294                 searchclick(sender, e);
295             }
296         }
297         forms.frmanchor m_frmanchor;
298         private void btnkeybord_mousedown(object sender, mouseeventargs e)
299         {
300             m_intselectionstart = this.txtinput.selectionstart;
301             m_intselectionlength = this.txtinput.selectionlength;
302             this.findform().activecontrol = this;
303             this.findform().activecontrol = this.txtinput;
304             switch (keyboardtype)
305             {
306                 case keyboardtype.uckeyborderall_en:
307                     if (m_frmanchor == null)
308                     {
309                         if (m_frmanchor == null)
310                         {
311                             uckeyborderall key = new uckeyborderall();
312                             key.chartype = keyborderchartype.char;
313                             key.retractclike += (a, b) =>
314                             {
315                                 m_frmanchor.hide();
316                             };
317                             m_frmanchor = new forms.frmanchor(this, key);
318                             m_frmanchor.visiblechanged += (a, b) =>
319                             {
320                                 if (m_frmanchor.visible)
321                                 {
322                                     this.txtinput.selectionstart = m_intselectionstart;
323                                     this.txtinput.selectionlength = m_intselectionlength;
324                                 }
325                             };
326                         }
327                     }
328                     break;
329                 case keyboardtype.uckeyborderall_num:
330 
331                     if (m_frmanchor == null)
332                     {
333                         uckeyborderall key = new uckeyborderall();
334                         key.chartype = keyborderchartype.number;
335                         key.retractclike += (a, b) =>
336                         {
337                             m_frmanchor.hide();
338                         };
339                         m_frmanchor = new forms.frmanchor(this, key);
340                         m_frmanchor.visiblechanged += (a, b) =>
341                         {
342                             if (m_frmanchor.visible)
343                             {
344                                 this.txtinput.selectionstart = m_intselectionstart;
345                                 this.txtinput.selectionlength = m_intselectionlength;
346                             }
347                         };
348                     }
349 
350                     break;
351                 case keyboardtype.uckeybordernum:
352                     if (m_frmanchor == null)
353                     {
354                         uckeybordernum key = new uckeybordernum();
355                         m_frmanchor = new forms.frmanchor(this, key);
356                         m_frmanchor.visiblechanged += (a, b) =>
357                         {
358                             if (m_frmanchor.visible)
359                             {
360                                 this.txtinput.selectionstart = m_intselectionstart;
361                                 this.txtinput.selectionlength = m_intselectionlength;
362                             }
363                         };
364                     }
365                     break;
366                 case hzh_controls.controls.keyboardtype.uckeyborderhand:
367 
368                     m_frmanchor = new forms.frmanchor(this, new size(504, 361));
369                     m_frmanchor.visiblechanged += m_frmanchor_visiblechanged;
370                     m_frmanchor.disposed += m_frmanchor_disposed;
371                     panel p = new panel();
372                     p.dock = dockstyle.fill;
373                     p.name = "keyborder";
374                     m_frmanchor.controls.add(p);
375 
376                     ucbtnext btndelete = new ucbtnext();
377                     btndelete.name = "btndelete";
378                     btndelete.size = new size(80, 28);
379                     btndelete.fillcolor = color.white;
380                     btndelete.isradius = false;
381                     btndelete.conerradius = 1;
382                     btndelete.isshowrect = true;
383                     btndelete.rectcolor = color.fromargb(189, 197, 203);
384                     btndelete.location = new point(198, 332);
385                     btndelete.btnfont = new system.drawing.font("微软雅黑", 8);
386                     btndelete.btntext = "删除";
387                     btndelete.btnclick += (a, b) =>
388                     {
389                         sendkeys.send("{backspace}");
390                     };
391                     m_frmanchor.controls.add(btndelete);
392                     btndelete.bringtofront();
393 
394                     ucbtnext btnenter = new ucbtnext();
395                     btnenter.name = "btnenter";
396                     btnenter.size = new size(82, 28);
397                     btnenter.fillcolor = color.white;
398                     btnenter.isradius = false;
399                     btnenter.conerradius = 1;
400                     btnenter.isshowrect = true;
401                     btnenter.rectcolor = color.fromargb(189, 197, 203);
402                     btnenter.location = new point(278, 332);
403                     btnenter.btnfont = new system.drawing.font("微软雅黑", 8);
404                     btnenter.btntext = "确定";
405                     btnenter.btnclick += (a, b) =>
406                     {
407                         sendkeys.send("{enter}");
408                         m_frmanchor.hide();
409                     };
410                     m_frmanchor.controls.add(btnenter);
411                     btnenter.bringtofront();
412                     m_frmanchor.visiblechanged += (a, b) =>
413                     {
414                         if (m_frmanchor.visible)
415                         {
416                             this.txtinput.selectionstart = m_intselectionstart;
417                             this.txtinput.selectionlength = m_intselectionlength;
418                         }
419                     };
420                     break;
421             }
422             if (!m_frmanchor.visible)
423                 m_frmanchor.show(this.findform());
424             if (keyboardclick != null)
425             {
426                 keyboardclick(sender, e);
427             }
428         }
429 
430         void m_frmanchor_disposed(object sender, eventargs e)
431         {
432             if (m_handappwin != intptr.zero)
433             {
434                 if (m_handpwin != null && !m_handpwin.hasexited)
435                     m_handpwin.kill();
436                 m_handpwin = null;
437                 m_handappwin = intptr.zero;
438             }
439         }
440 
441 
442         intptr m_handappwin;
443         process m_handpwin = null;
444         string m_handexename = system.io.path.combine(system.appdomain.currentdomain.basedirectory, "handinput\\handinput.exe");
445 
446         void m_frmanchor_visiblechanged(object sender, eventargs e)
447         {
448             if (m_frmanchor.visible)
449             {
450                 var lstp = process.getprocessesbyname("handinput");
451                 if (lstp.length > 0)
452                 {
453                     foreach (var item in lstp)
454                     {
455                         item.kill();
456                     }
457                 }
458                 m_handappwin = intptr.zero;
459 
460                 if (m_handpwin == null)
461                 {
462                     m_handpwin = null;
463 
464                     m_handpwin = system.diagnostics.process.start(this.m_handexename);
465                     m_handpwin.waitforinputidle();
466                 }
467                 while (m_handpwin.mainwindowhandle == intptr.zero)
468                 {
469                     thread.sleep(10);
470                 }
471                 m_handappwin = m_handpwin.mainwindowhandle;
472                 control p = m_frmanchor.controls.find("keyborder", false)[0];
473                 setparent(m_handappwin, p.handle);
474                 controlhelper.setforegroundwindow(this.findform().handle);
475                 movewindow(m_handappwin, -111, -41, 626, 412, true);
476             }
477             else
478             {
479                 if (m_handappwin != intptr.zero)
480                 {
481                     if (m_handpwin != null && !m_handpwin.hasexited)
482                         m_handpwin.kill();
483                     m_handpwin = null;
484                     m_handappwin = intptr.zero;
485                 }
486             }
487         }
488 
489         private void uctextboxex_mousedown(object sender, mouseeventargs e)
490         {
491             this.activecontrol = txtinput;
492         }
493 
494         private void uctextboxex_load(object sender, eventargs e)
495         {
496             if (!enabled)
497             {
498                 base.fillcolor = color.fromargb(240, 240, 240);
499                 txtinput.backcolor = color.fromargb(240, 240, 240);
500             }
501             else
502             {
503                 fillcolor = _fillcolor;
504                 txtinput.backcolor = _fillcolor;
505             }
506         }
507         [dllimport("user32.dll", setlasterror = true)]
508         private static extern long setparent(intptr hwndchild, intptr hwndnewparent);
509 
510         [dllimport("user32.dll", setlasterror = true)]
511         private static extern bool movewindow(intptr hwnd, int x, int y, int cx, int cy, bool repaint);
512         [dllimport("user32.dll", entrypoint = "showwindow")]
513         private static extern bool showwindow(intptr hwnd, int ncmdshow);
514         [dllimport("user32.dll")]
515         private static extern bool setwindowpos(intptr hwnd, int hwndlnsertafter, int x, int y, int cx, int cy, uint flags);
516         private const int gwl_style = -16;
517         private const int ws_child = 0x40000000;//设置窗口属性为child
518 
519         [dllimport("user32.dll", entrypoint = "getwindowlong")]
520         public static extern int getwindowlong(intptr hwnd, int nindex);
521 
522         [dllimport("user32.dll", entrypoint = "setwindowlong")]
523         public static extern int setwindowlong(intptr hwnd, int nindex, int dwnewlong);
524 
525         [dllimport("user32.dll")]
526         private extern static intptr setactivewindow(intptr handle);
527     }
528 }
  1 namespace hzh_controls.controls
  2 {
  3     partial class uctextboxex
  4     {
  5         /// <summary> 
  6         /// 必需的设计器变量。
  7         /// </summary>
  8         private system.componentmodel.icontainer components = null;
  9 
 10         /// <summary> 
 11         /// 清理所有正在使用的资源。
 12         /// </summary>
 13         /// <param name="disposing">如果应释放托管资源,为 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 组件设计器生成的代码
 24 
 25         /// <summary> 
 26         /// 设计器支持所需的方法 - 不要
 27         /// 使用代码编辑器修改此方法的内容。
 28         /// </summary>
 29         private void initializecomponent()
 30         {
 31             system.componentmodel.componentresourcemanager resources = new system.componentmodel.componentresourcemanager(typeof(uctextboxex));
 32             this.txtinput = new hzh_controls.controls.textboxex();
 33             this.imagelist1 = new system.windows.forms.imagelist();
 34             this.btnclear = new system.windows.forms.panel();
 35             this.btnkeybord = new system.windows.forms.panel();
 36             this.btnsearch = new system.windows.forms.panel();
 37             this.suspendlayout();
 38             // 
 39             // txtinput
 40             // 
 41             this.txtinput.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.left | system.windows.forms.anchorstyles.right)));
 42             this.txtinput.borderstyle = system.windows.forms.borderstyle.none;
 43             this.txtinput.declength = 2;
 44             this.txtinput.font = new system.drawing.font("微软雅黑", 18f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
 45             this.txtinput.inputtype = textinputtype.notcontrol;
 46             this.txtinput.location = new system.drawing.point(8, 9);
 47             this.txtinput.margin = new system.windows.forms.padding(3, 3, 10, 3);
 48             this.txtinput.maxvalue = new decimal(new int[] {
 49             1000000,
 50             0,
 51             0,
 52             0});
 53             this.txtinput.minvalue = new decimal(new int[] {
 54             1000000,
 55             0,
 56             0,
 57             -2147483648});
 58             this.txtinput.myrectangle = new system.drawing.rectangle(0, 0, 0, 0);
 59             this.txtinput.name = "txtinput";
 60             this.txtinput.oldtext = null;
 61             this.txtinput.promptcolor = system.drawing.color.gray;
 62             this.txtinput.promptfont = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
 63             this.txtinput.prompttext = "";
 64             this.txtinput.regexpattern = "";
 65             this.txtinput.size = new system.drawing.size(309, 24);
 66             this.txtinput.tabindex = 0;
 67             this.txtinput.textchanged += new system.eventhandler(this.txtinput_textchanged);
 68             // 
 69             // imagelist1
 70             // 
 71             this.imagelist1.imagestream = ((system.windows.forms.imageliststreamer)(resources.getobject("imagelist1.imagestream")));
 72             this.imagelist1.transparentcolor = system.drawing.color.transparent;
 73             this.imagelist1.images.setkeyname(0, "ic_cancel_black_24dp.png");
 74             this.imagelist1.images.setkeyname(1, "ic_search_black_24dp.png");
 75             this.imagelist1.images.setkeyname(2, "keyboard.png");
 76             // 
 77             // btnclear
 78             // 
 79             this.btnclear.backgroundimage = global::hzh_controls.properties.resources.input_clear;
 80             this.btnclear.backgroundimagelayout = system.windows.forms.imagelayout.center;
 81             this.btnclear.cursor = system.windows.forms.cursors.default;
 82             this.btnclear.dock = system.windows.forms.dockstyle.right;
 83             this.btnclear.location = new system.drawing.point(227, 5);
 84             this.btnclear.name = "btnclear";
 85             this.btnclear.size = new system.drawing.size(30, 32);
 86             this.btnclear.tabindex = 4;
 87             this.btnclear.visible = false;
 88             this.btnclear.mousedown += new system.windows.forms.mouseeventhandler(this.btnclear_mousedown);
 89             // 
 90             // btnkeybord
 91             // 
 92             this.btnkeybord.backgroundimage = global::hzh_controls.properties.resources.keyboard;
 93             this.btnkeybord.backgroundimagelayout = system.windows.forms.imagelayout.center;
 94             this.btnkeybord.cursor = system.windows.forms.cursors.default;
 95             this.btnkeybord.dock = system.windows.forms.dockstyle.right;
 96             this.btnkeybord.location = new system.drawing.point(257, 5);
 97             this.btnkeybord.name = "btnkeybord";
 98             this.btnkeybord.size = new system.drawing.size(30, 32);
 99             this.btnkeybord.tabindex = 6;
100             this.btnkeybord.visible = false;
101             this.btnkeybord.mousedown += new system.windows.forms.mouseeventhandler(this.btnkeybord_mousedown);
102             // 
103             // btnsearch
104             // 
105             this.btnsearch.backgroundimage = global::hzh_controls.properties.resources.ic_search_black_24dp;
106             this.btnsearch.backgroundimagelayout = system.windows.forms.imagelayout.center;
107             this.btnsearch.cursor = system.windows.forms.cursors.default;
108             this.btnsearch.dock = system.windows.forms.dockstyle.right;
109             this.btnsearch.location = new system.drawing.point(287, 5);
110             this.btnsearch.name = "btnsearch";
111             this.btnsearch.size = new system.drawing.size(30, 32);
112             this.btnsearch.tabindex = 5;
113             this.btnsearch.visible = false;
114             this.btnsearch.mousedown += new system.windows.forms.mouseeventhandler(this.btnsearch_mousedown);
115             // 
116             // uctextboxex
117             // 
118             this.autoscalemode = system.windows.forms.autoscalemode.none;
119             this.backcolor = system.drawing.color.transparent;
120             this.conerradius = 5;
121             this.controls.add(this.btnclear);
122             this.controls.add(this.btnkeybord);
123             this.controls.add(this.btnsearch);
124             this.controls.add(this.txtinput);
125             this.cursor = system.windows.forms.cursors.ibeam;
126             this.isshowrect = true;
127             this.isradius = true;
128             this.name = "uctextboxex";
129             this.padding = new system.windows.forms.padding(5);
130             this.size = new system.drawing.size(322, 42);
131             this.load += new system.eventhandler(this.uctextboxex_load);
132             this.mousedown += new system.windows.forms.mouseeventhandler(this.uctextboxex_mousedown);
133             this.resumelayout(false);
134             this.performlayout();
135 
136         }
137 
138         #endregion
139 
140         private system.windows.forms.imagelist imagelist1;
141         public textboxex txtinput;
142         private system.windows.forms.panel btnclear;
143         private system.windows.forms.panel btnsearch;
144         private system.windows.forms.panel btnkeybord;
145     }
146 }

 

用处及效果

(三十一)c#Winform自定义控件-文本框(四)

 

最后的话

如果你喜欢的话,请到  点个星 星吧