// *********************************************************************** // Assembly : HZH_Controls // Created : 08-08-2019 // // *********************************************************************** // // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com // // // Blog: https://www.cnblogs.com/bfyx // GitHub:https://github.com/kwwwvagaa/NetWinformControl // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git // // If you use this code, please keep this note. // *********************************************************************** using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics; using System.Threading; namespace HZH_Controls.Controls { /// /// Class UCTextBoxEx. /// Implements the /// /// [DefaultEvent("TextChanged")] public partial class UCTextBoxEx : UCControlBase { /// /// The m is show clear BTN /// private bool m_isShowClearBtn = true; /// /// The m int selection start /// int m_intSelectionStart = 0; /// /// The m int selection length /// int m_intSelectionLength = 0; /// /// 功能描述:是否显示清理按钮 /// 作  者:HZH /// 创建日期:2019-02-28 16:13:52 /// /// true if this instance is show clear BTN; otherwise, false. [Description("是否显示清理按钮"), Category("自定义")] public bool IsShowClearBtn { get { return m_isShowClearBtn; } set { m_isShowClearBtn = value; if (value) { btnClear.Visible = !(txtInput.Text == "\r\n") && !string.IsNullOrEmpty(txtInput.Text); } else { btnClear.Visible = false; } } } /// /// The m is show search BTN /// private bool m_isShowSearchBtn = false; /// /// 是否显示查询按钮 /// /// true if this instance is show search BTN; otherwise, false. [Description("是否显示查询按钮"), Category("自定义")] public bool IsShowSearchBtn { get { return m_isShowSearchBtn; } set { m_isShowSearchBtn = value; btnSearch.Visible = value; } } /// /// Gets or sets a value indicating whether this instance is show keyboard. /// /// true if this instance is show keyboard; otherwise, false. [Description("是否显示键盘"), Category("自定义")] public bool IsShowKeyboard { get { return btnKeybord.Visible; } set { btnKeybord.Visible = value; } } /// /// 获取或设置控件显示的文字的字体。 /// /// The font. /// /// /// /// /// /// [Description("字体"), Category("自定义")] public new Font Font { get { return this.txtInput.Font; } set { this.txtInput.Font = value; } } /// /// Gets or sets the type of the input. /// /// The type of the input. [Description("输入类型"), Category("自定义")] public TextInputType InputType { get { return txtInput.InputType; } set { txtInput.InputType = value; } } /// /// 水印文字 /// /// The prompt text. [Description("水印文字"), Category("自定义")] public string PromptText { get { return this.txtInput.PromptText; } set { this.txtInput.PromptText = value; } } /// /// Gets or sets the prompt font. /// /// The prompt font. [Description("水印字体"), Category("自定义")] public Font PromptFont { get { return this.txtInput.PromptFont; } set { this.txtInput.PromptFont = value; } } /// /// Gets or sets the color of the prompt. /// /// The color of the prompt. [Description("水印颜色"), Category("自定义")] public Color PromptColor { get { return this.txtInput.PromptColor; } set { this.txtInput.PromptColor = value; } } /// /// 获取或设置一个值,该值指示当输入类型InputType=Regex时,使用的正则表达式。 /// /// The regex pattern. [Description("获取或设置一个值,该值指示当输入类型InputType=Regex时,使用的正则表达式。")] public string RegexPattern { get { return this.txtInput.RegexPattern; } set { this.txtInput.RegexPattern = value; } } /// /// 当InputType为数字类型时,能输入的最大值 /// /// The maximum value. [Description("当InputType为数字类型时,能输入的最大值。")] public decimal MaxValue { get { return this.txtInput.MaxValue; } set { this.txtInput.MaxValue = value; } } /// /// 当InputType为数字类型时,能输入的最小值 /// /// The minimum value. [Description("当InputType为数字类型时,能输入的最小值。")] public decimal MinValue { get { return this.txtInput.MinValue; } set { this.txtInput.MinValue = value; } } /// /// 当InputType为数字类型时,能输入的最小值 /// /// The length of the decimal. [Description("当InputType为数字类型时,小数位数。")] public int DecLength { get { return this.txtInput.DecLength; } set { this.txtInput.DecLength = value; } } /// /// The key board type /// private KeyBoardType keyBoardType = KeyBoardType.UCKeyBorderAll_EN; /// /// Gets or sets the type of the key board. /// /// The type of the key board. [Description("键盘打开样式"), Category("自定义")] public KeyBoardType KeyBoardType { get { return keyBoardType; } set { keyBoardType = value; } } /// /// Occurs when [search click]. /// [Description("查询按钮点击事件"), Category("自定义")] public event EventHandler SearchClick; /// /// Occurs when [text changed]. /// [Description("文本改变事件"), Category("自定义")] public new event EventHandler TextChanged; /// /// Occurs when [keyboard click]. /// [Description("键盘按钮点击事件"), Category("自定义")] public event EventHandler KeyboardClick; /// /// Gets or sets the input text. /// /// The input text. [Description("文本"), Category("自定义")] public string InputText { get { return txtInput.Text; } set { txtInput.Text = value; } } /// /// The focus border color /// private Color focusBorderColor = Color.FromArgb(255, 77, 59); /// /// Gets or sets the color of the focus border. /// /// The color of the focus border. [Description("获取焦点时边框颜色,当IsFocusColor=true有效"), Category("自定义")] public Color FocusBorderColor { get { return focusBorderColor; } set { focusBorderColor = value; } } /// /// The is focus color /// private bool isFocusColor = true; /// /// Gets or sets a value indicating whether this instance is focus color. /// /// true if this instance is focus color; otherwise, false. [Description("获取焦点是否变色"), Category("自定义")] public bool IsFocusColor { get { return isFocusColor; } set { isFocusColor = value; } } /// /// The fill color /// private Color _FillColor; /// /// 当使用边框时填充颜色,当值为背景色或透明色或空值则不填充 /// /// The color of the fill. public new Color FillColor { get { return _FillColor; } set { _FillColor = value; base.FillColor = value; this.txtInput.BackColor = value; } } /// /// Initializes a new instance of the class. /// public UCTextBoxEx() { InitializeComponent(); txtInput.SizeChanged += UCTextBoxEx_SizeChanged; this.SizeChanged += UCTextBoxEx_SizeChanged; txtInput.GotFocus += (a, b) => { if (isFocusColor) this.RectColor = focusBorderColor; }; txtInput.LostFocus += (a, b) => { if (isFocusColor) this.RectColor = Color.FromArgb(220, 220, 220); }; } /// /// Handles the SizeChanged event of the UCTextBoxEx control. /// /// The source of the event. /// The instance containing the event data. void UCTextBoxEx_SizeChanged(object sender, EventArgs e) { this.txtInput.Location = new Point(this.txtInput.Location.X, (this.Height - txtInput.Height) / 2); } /// /// Handles the TextChanged event of the txtInput control. /// /// The source of the event. /// The instance containing the event data. private void txtInput_TextChanged(object sender, EventArgs e) { if (m_isShowClearBtn) { btnClear.Visible = !(txtInput.Text == "\r\n") && !string.IsNullOrEmpty(txtInput.Text); } if (TextChanged != null) { TextChanged(sender, e); } } /// /// Handles the MouseDown event of the btnClear control. /// /// The source of the event. /// The instance containing the event data. private void btnClear_MouseDown(object sender, MouseEventArgs e) { txtInput.Clear(); txtInput.Focus(); } /// /// Handles the MouseDown event of the btnSearch control. /// /// The source of the event. /// The instance containing the event data. private void btnSearch_MouseDown(object sender, MouseEventArgs e) { if (SearchClick != null) { SearchClick(sender, e); } } /// /// The m FRM anchor /// Forms.FrmAnchor m_frmAnchor; /// /// Handles the MouseDown event of the btnKeybord control. /// /// The source of the event. /// The instance containing the event data. private void btnKeybord_MouseDown(object sender, MouseEventArgs e) { if (keyBoardType == HZH_Controls.Controls.KeyBoardType.Null) return; m_intSelectionStart = this.txtInput.SelectionStart; m_intSelectionLength = this.txtInput.SelectionLength; this.FindForm().ActiveControl = this; this.FindForm().ActiveControl = this.txtInput; switch (keyBoardType) { case KeyBoardType.UCKeyBorderAll_EN: if (m_frmAnchor == null) { if (m_frmAnchor == null) { UCKeyBorderAll key = new UCKeyBorderAll(); key.CharType = KeyBorderCharType.CHAR; key.RetractClike += (a, b) => { m_frmAnchor.Hide(); }; m_frmAnchor = new Forms.FrmAnchor(this, key); m_frmAnchor.VisibleChanged += (a, b) => { if (m_frmAnchor.Visible) { this.txtInput.SelectionStart = m_intSelectionStart; this.txtInput.SelectionLength = m_intSelectionLength; } }; } } break; case KeyBoardType.UCKeyBorderAll_Num: if (m_frmAnchor == null) { UCKeyBorderAll key = new UCKeyBorderAll(); key.CharType = KeyBorderCharType.NUMBER; key.RetractClike += (a, b) => { m_frmAnchor.Hide(); }; m_frmAnchor = new Forms.FrmAnchor(this, key); m_frmAnchor.VisibleChanged += (a, b) => { if (m_frmAnchor.Visible) { this.txtInput.SelectionStart = m_intSelectionStart; this.txtInput.SelectionLength = m_intSelectionLength; } }; } break; case KeyBoardType.UCKeyBorderNum: if (m_frmAnchor == null) { UCKeyBorderNum key = new UCKeyBorderNum(); m_frmAnchor = new Forms.FrmAnchor(this, key); m_frmAnchor.VisibleChanged += (a, b) => { if (m_frmAnchor.Visible) { this.txtInput.SelectionStart = m_intSelectionStart; this.txtInput.SelectionLength = m_intSelectionLength; } }; } break; case HZH_Controls.Controls.KeyBoardType.UCKeyBorderHand: m_frmAnchor = new Forms.FrmAnchor(this, new Size(504, 361)); m_frmAnchor.VisibleChanged += m_frmAnchor_VisibleChanged; m_frmAnchor.Disposed += m_frmAnchor_Disposed; Panel p = new Panel(); p.Dock = DockStyle.Fill; p.Name = "keyborder"; m_frmAnchor.Controls.Add(p); UCBtnExt btnDelete = new UCBtnExt(); btnDelete.Name = "btnDelete"; btnDelete.Size = new Size(80, 28); btnDelete.FillColor = Color.White; btnDelete.IsRadius = false; btnDelete.ConerRadius = 1; btnDelete.IsShowRect = true; btnDelete.RectColor = Color.FromArgb(189, 197, 203); btnDelete.Location = new Point(198, 332); btnDelete.BtnFont = new System.Drawing.Font("微软雅黑", 8); btnDelete.BtnText = "删除"; btnDelete.BtnClick += (a, b) => { SendKeys.Send("{BACKSPACE}"); }; m_frmAnchor.Controls.Add(btnDelete); btnDelete.BringToFront(); UCBtnExt btnEnter = new UCBtnExt(); btnEnter.Name = "btnEnter"; btnEnter.Size = new Size(82, 28); btnEnter.FillColor = Color.White; btnEnter.IsRadius = false; btnEnter.ConerRadius = 1; btnEnter.IsShowRect = true; btnEnter.RectColor = Color.FromArgb(189, 197, 203); btnEnter.Location = new Point(278, 332); btnEnter.BtnFont = new System.Drawing.Font("微软雅黑", 8); btnEnter.BtnText = "确定"; btnEnter.BtnClick += (a, b) => { SendKeys.Send("{ENTER}"); m_frmAnchor.Hide(); }; m_frmAnchor.Controls.Add(btnEnter); btnEnter.BringToFront(); m_frmAnchor.VisibleChanged += (a, b) => { if (m_frmAnchor.Visible) { this.txtInput.SelectionStart = m_intSelectionStart; this.txtInput.SelectionLength = m_intSelectionLength; } }; break; } if (!m_frmAnchor.Visible) m_frmAnchor.Show(this.FindForm()); if (KeyboardClick != null) { KeyboardClick(sender, e); } } /// /// Handles the Disposed event of the m_frmAnchor control. /// /// The source of the event. /// The instance containing the event data. void m_frmAnchor_Disposed(object sender, EventArgs e) { if (m_HandAppWin != IntPtr.Zero) { if (m_HandPWin != null && !m_HandPWin.HasExited) m_HandPWin.Kill(); m_HandPWin = null; m_HandAppWin = IntPtr.Zero; } } /// /// The m hand application win /// IntPtr m_HandAppWin; /// /// The m hand p win /// Process m_HandPWin = null; /// /// The m hand executable name /// string m_HandExeName = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "HandInput\\handinput.exe"); /// /// Handles the VisibleChanged event of the m_frmAnchor control. /// /// The source of the event. /// The instance containing the event data. void m_frmAnchor_VisibleChanged(object sender, EventArgs e) { if (m_frmAnchor.Visible) { var lstP = Process.GetProcessesByName("handinput"); if (lstP.Length > 0) { foreach (var item in lstP) { item.Kill(); } } m_HandAppWin = IntPtr.Zero; if (m_HandPWin == null) { m_HandPWin = null; m_HandPWin = System.Diagnostics.Process.Start(this.m_HandExeName); m_HandPWin.WaitForInputIdle(); } while (m_HandPWin.MainWindowHandle == IntPtr.Zero) { Thread.Sleep(10); } m_HandAppWin = m_HandPWin.MainWindowHandle; Control p = m_frmAnchor.Controls.Find("keyborder", false)[0]; SetParent(m_HandAppWin, p.Handle); ControlHelper.SetForegroundWindow(this.FindForm().Handle); MoveWindow(m_HandAppWin, -111, -41, 626, 412, true); } else { if (m_HandAppWin != IntPtr.Zero) { if (m_HandPWin != null && !m_HandPWin.HasExited) m_HandPWin.Kill(); m_HandPWin = null; m_HandAppWin = IntPtr.Zero; } } } /// /// Handles the MouseDown event of the UCTextBoxEx control. /// /// The source of the event. /// The instance containing the event data. private void UCTextBoxEx_MouseDown(object sender, MouseEventArgs e) { this.ActiveControl = txtInput; } /// /// Handles the Load event of the UCTextBoxEx control. /// /// The source of the event. /// The instance containing the event data. private void UCTextBoxEx_Load(object sender, EventArgs e) { if (!Enabled) { base.FillColor = Color.FromArgb(240, 240, 240); txtInput.BackColor = Color.FromArgb(240, 240, 240); } else { FillColor = _FillColor; txtInput.BackColor = _FillColor; } } /// /// Sets the parent. /// /// The h WND child. /// The h WND new parent. /// System.Int64. [DllImport("user32.dll", SetLastError = true)] private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent); /// /// Moves the window. /// /// The HWND. /// The x. /// The y. /// The cx. /// The cy. /// if set to true [repaint]. /// true if XXXX, false otherwise. [DllImport("user32.dll", SetLastError = true)] private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint); /// /// Shows the window. /// /// The HWND. /// The n command show. /// true if XXXX, false otherwise. [DllImport("user32.dll", EntryPoint = "ShowWindow")] private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow); /// /// Sets the window position. /// /// The h WND. /// The h wndlnsert after. /// The x. /// The y. /// The cx. /// The cy. /// The flags. /// true if XXXX, false otherwise. [DllImport("user32.dll")] private static extern bool SetWindowPos(IntPtr hWnd, int hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags); /// /// The GWL style /// private const int GWL_STYLE = -16; /// /// The ws child /// private const int WS_CHILD = 0x40000000;//设置窗口属性为child /// /// Gets the window long. /// /// The HWND. /// Index of the n. /// System.Int32. [DllImport("user32.dll", EntryPoint = "GetWindowLong")] public static extern int GetWindowLong(IntPtr hwnd, int nIndex); /// /// Sets the window long. /// /// The HWND. /// Index of the n. /// The dw new long. /// System.Int32. [DllImport("user32.dll", EntryPoint = "SetWindowLong")] public static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong); /// /// Sets the active window. /// /// The handle. /// IntPtr. [DllImport("user32.dll")] private extern static IntPtr SetActiveWindow(IntPtr handle); } }