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

二次信任 提交于 2019-11-27 13:06:26

前提

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

开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control

如果觉得写的还行,请点个 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 }
View Code
  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 }
View Code

 

用处及效果

 

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!