官网

http://www.hzhcontrols.com

前提

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

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

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

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

idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d">

目录

https://www.cnblogs.com/bfyx/p/11364884.html

准备工作

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

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

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

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

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

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

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

开始

添加用户控件,命名UCTextBoxEx,继承自UCControlBase

属性

 private bool m_isShowClearBtn = true;
int m_intSelectionStart = ;
int m_intSelectionLength = ;
/// <summary>
/// 功能描述:是否显示清理按钮
/// 作  者:HZH
/// 创建日期:2019-02-28 16:13:52
/// </summary>
[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;
}
}
} private bool m_isShowSearchBtn = false;
/// <summary>
/// 是否显示查询按钮
/// </summary> [Description("是否显示查询按钮"), Category("自定义")]
public bool IsShowSearchBtn
{
get { return m_isShowSearchBtn; }
set
{
m_isShowSearchBtn = value;
btnSearch.Visible = value;
}
} [Description("是否显示键盘"), Category("自定义")]
public bool IsShowKeyboard
{
get
{
return btnKeybord.Visible;
}
set
{
btnKeybord.Visible = value;
}
}
[Description("字体"), Category("自定义")]
public new Font Font
{
get
{
return this.txtInput.Font;
}
set
{
this.txtInput.Font = value;
}
} [Description("输入类型"), Category("自定义")]
public TextInputType InputType
{
get { return txtInput.InputType; }
set { txtInput.InputType = value; }
} /// <summary>
/// 水印文字
/// </summary>
[Description("水印文字"), Category("自定义")]
public string PromptText
{
get
{
return this.txtInput.PromptText;
}
set
{
this.txtInput.PromptText = value;
}
} [Description("水印字体"), Category("自定义")]
public Font PromptFont
{
get
{
return this.txtInput.PromptFont;
}
set
{
this.txtInput.PromptFont = value;
}
} [Description("水印颜色"), Category("自定义")]
public Color PromptColor
{
get
{
return this.txtInput.PromptColor;
}
set
{
this.txtInput.PromptColor = value;
}
} /// <summary>
/// 获取或设置一个值,该值指示当输入类型InputType=Regex时,使用的正则表达式。
/// </summary>
[Description("获取或设置一个值,该值指示当输入类型InputType=Regex时,使用的正则表达式。")]
public string RegexPattern
{
get
{
return this.txtInput.RegexPattern;
}
set
{
this.txtInput.RegexPattern = value;
}
}
/// <summary>
/// 当InputType为数字类型时,能输入的最大值
/// </summary>
[Description("当InputType为数字类型时,能输入的最大值。")]
public decimal MaxValue
{
get
{
return this.txtInput.MaxValue;
}
set
{
this.txtInput.MaxValue = value;
}
}
/// <summary>
/// 当InputType为数字类型时,能输入的最小值
/// </summary>
[Description("当InputType为数字类型时,能输入的最小值。")]
public decimal MinValue
{
get
{
return this.txtInput.MinValue;
}
set
{
this.txtInput.MinValue = value;
}
}
/// <summary>
/// 当InputType为数字类型时,能输入的最小值
/// </summary>
[Description("当InputType为数字类型时,小数位数。")]
public int DecLength
{
get
{
return this.txtInput.DecLength;
}
set
{
this.txtInput.DecLength = value;
}
} private KeyBoardType keyBoardType = KeyBoardType.UCKeyBorderAll_EN;
[Description("键盘打开样式"), Category("自定义")]
public KeyBoardType KeyBoardType
{
get { return keyBoardType; }
set { keyBoardType = value; }
}
[Description("查询按钮点击事件"), Category("自定义")]
public event EventHandler SearchClick; [Description("文本改变事件"), Category("自定义")]
public new event EventHandler TextChanged;
[Description("键盘按钮点击事件"), Category("自定义")]
public event EventHandler KeyboardClick; [Description("文本"), Category("自定义")]
public string InputText
{
get
{
return txtInput.Text;
}
set
{
txtInput.Text = value;
}
} private bool isFocusColor = true;
[Description("获取焦点是否变色"), Category("自定义")]
public bool IsFocusColor
{
get { return isFocusColor; }
set { isFocusColor = value; }
}
private Color _FillColor;
public new Color FillColor
{
get
{
return _FillColor;
}
set
{
_FillColor = value;
base.FillColor = value;
this.txtInput.BackColor = value;
}
}

一些事件

 void UCTextBoxEx_SizeChanged(object sender, EventArgs e)
{
this.txtInput.Location = new Point(this.txtInput.Location.X, (this.Height - txtInput.Height) / );
} 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);
}
} private void btnClear_MouseDown(object sender, MouseEventArgs e)
{
txtInput.Clear();
txtInput.Focus();
} private void btnSearch_MouseDown(object sender, MouseEventArgs e)
{
if (SearchClick != null)
{
SearchClick(sender, e);
}
}
Forms.FrmAnchor m_frmAnchor;
private void btnKeybord_MouseDown(object sender, MouseEventArgs e)
{
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(, ));
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(, );
btnDelete.FillColor = Color.White;
btnDelete.IsRadius = false;
btnDelete.ConerRadius = ;
btnDelete.IsShowRect = true;
btnDelete.RectColor = Color.FromArgb(, , );
btnDelete.Location = new Point(, );
btnDelete.BtnFont = new System.Drawing.Font("微软雅黑", );
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(, );
btnEnter.FillColor = Color.White;
btnEnter.IsRadius = false;
btnEnter.ConerRadius = ;
btnEnter.IsShowRect = true;
btnEnter.RectColor = Color.FromArgb(, , );
btnEnter.Location = new Point(, );
btnEnter.BtnFont = new System.Drawing.Font("微软雅黑", );
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);
}
} 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;
}
} IntPtr m_HandAppWin;
Process m_HandPWin = null;
string m_HandExeName = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "HandInput\\handinput.exe"); void m_frmAnchor_VisibleChanged(object sender, EventArgs e)
{
if (m_frmAnchor.Visible)
{
var lstP = Process.GetProcessesByName("handinput");
if (lstP.Length > )
{
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();
}
m_HandAppWin = m_HandPWin.MainWindowHandle;
Control p = m_frmAnchor.Controls.Find("keyborder", false)[];
SetParent(m_HandAppWin, p.Handle);
ControlHelper.SetForegroundWindow(this.FindForm().Handle);
MoveWindow(m_HandAppWin, -, -, , , true);
}
else
{
if (m_HandAppWin != IntPtr.Zero)
{
if (m_HandPWin != null && !m_HandPWin.HasExited)
m_HandPWin.Kill();
m_HandPWin = null;
m_HandAppWin = IntPtr.Zero;
}
}
} private void UCTextBoxEx_MouseDown(object sender, MouseEventArgs e)
{
this.ActiveControl = txtInput;
} private void UCTextBoxEx_Load(object sender, EventArgs e)
{
if (!Enabled)
{
base.FillColor = Color.FromArgb(, , );
txtInput.BackColor = Color.FromArgb(, , );
}
else
{
FillColor = _FillColor;
txtInput.BackColor = _FillColor;
}
}
[DllImport("user32.dll", SetLastError = true)]
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
[DllImport("user32.dll", EntryPoint = "ShowWindow")]
private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, int hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);
private const int GWL_STYLE = -;
private const int WS_CHILD = 0x40000000;//设置窗口属性为child [DllImport("user32.dll", EntryPoint = "GetWindowLong")]
public static extern int GetWindowLong(IntPtr hwnd, int nIndex); [DllImport("user32.dll", EntryPoint = "SetWindowLong")]
public static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong); [DllImport("user32.dll")]
private extern static IntPtr SetActiveWindow(IntPtr handle);

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

完整的代码

 // 版权所有  黄正辉  交流群:568015492   QQ:623128629
// 文件名称:UCTextBoxEx.cs
// 创建日期:2019-08-15 16:03:58
// 功能描述:TextBox
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
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
{
[DefaultEvent("TextChanged")]
public partial class UCTextBoxEx : UCControlBase
{
private bool m_isShowClearBtn = true;
int m_intSelectionStart = ;
int m_intSelectionLength = ;
/// <summary>
/// 功能描述:是否显示清理按钮
/// 作  者:HZH
/// 创建日期:2019-02-28 16:13:52
/// </summary>
[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;
}
}
} private bool m_isShowSearchBtn = false;
/// <summary>
/// 是否显示查询按钮
/// </summary> [Description("是否显示查询按钮"), Category("自定义")]
public bool IsShowSearchBtn
{
get { return m_isShowSearchBtn; }
set
{
m_isShowSearchBtn = value;
btnSearch.Visible = value;
}
} [Description("是否显示键盘"), Category("自定义")]
public bool IsShowKeyboard
{
get
{
return btnKeybord.Visible;
}
set
{
btnKeybord.Visible = value;
}
}
[Description("字体"), Category("自定义")]
public new Font Font
{
get
{
return this.txtInput.Font;
}
set
{
this.txtInput.Font = value;
}
} [Description("输入类型"), Category("自定义")]
public TextInputType InputType
{
get { return txtInput.InputType; }
set { txtInput.InputType = value; }
} /// <summary>
/// 水印文字
/// </summary>
[Description("水印文字"), Category("自定义")]
public string PromptText
{
get
{
return this.txtInput.PromptText;
}
set
{
this.txtInput.PromptText = value;
}
} [Description("水印字体"), Category("自定义")]
public Font PromptFont
{
get
{
return this.txtInput.PromptFont;
}
set
{
this.txtInput.PromptFont = value;
}
} [Description("水印颜色"), Category("自定义")]
public Color PromptColor
{
get
{
return this.txtInput.PromptColor;
}
set
{
this.txtInput.PromptColor = value;
}
} /// <summary>
/// 获取或设置一个值,该值指示当输入类型InputType=Regex时,使用的正则表达式。
/// </summary>
[Description("获取或设置一个值,该值指示当输入类型InputType=Regex时,使用的正则表达式。")]
public string RegexPattern
{
get
{
return this.txtInput.RegexPattern;
}
set
{
this.txtInput.RegexPattern = value;
}
}
/// <summary>
/// 当InputType为数字类型时,能输入的最大值
/// </summary>
[Description("当InputType为数字类型时,能输入的最大值。")]
public decimal MaxValue
{
get
{
return this.txtInput.MaxValue;
}
set
{
this.txtInput.MaxValue = value;
}
}
/// <summary>
/// 当InputType为数字类型时,能输入的最小值
/// </summary>
[Description("当InputType为数字类型时,能输入的最小值。")]
public decimal MinValue
{
get
{
return this.txtInput.MinValue;
}
set
{
this.txtInput.MinValue = value;
}
}
/// <summary>
/// 当InputType为数字类型时,能输入的最小值
/// </summary>
[Description("当InputType为数字类型时,小数位数。")]
public int DecLength
{
get
{
return this.txtInput.DecLength;
}
set
{
this.txtInput.DecLength = value;
}
} private KeyBoardType keyBoardType = KeyBoardType.UCKeyBorderAll_EN;
[Description("键盘打开样式"), Category("自定义")]
public KeyBoardType KeyBoardType
{
get { return keyBoardType; }
set { keyBoardType = value; }
}
[Description("查询按钮点击事件"), Category("自定义")]
public event EventHandler SearchClick; [Description("文本改变事件"), Category("自定义")]
public new event EventHandler TextChanged;
[Description("键盘按钮点击事件"), Category("自定义")]
public event EventHandler KeyboardClick; [Description("文本"), Category("自定义")]
public string InputText
{
get
{
return txtInput.Text;
}
set
{
txtInput.Text = value;
}
} private bool isFocusColor = true;
[Description("获取焦点是否变色"), Category("自定义")]
public bool IsFocusColor
{
get { return isFocusColor; }
set { isFocusColor = value; }
}
private Color _FillColor;
public new Color FillColor
{
get
{
return _FillColor;
}
set
{
_FillColor = value;
base.FillColor = value;
this.txtInput.BackColor = value;
}
}
public UCTextBoxEx()
{
InitializeComponent();
txtInput.SizeChanged += UCTextBoxEx_SizeChanged;
this.SizeChanged += UCTextBoxEx_SizeChanged;
txtInput.GotFocus += (a, b) =>
{
if (isFocusColor)
this.RectColor = Color.FromArgb(, , );
};
txtInput.LostFocus += (a, b) =>
{
if (isFocusColor)
this.RectColor = Color.FromArgb(, , );
};
} void UCTextBoxEx_SizeChanged(object sender, EventArgs e)
{
this.txtInput.Location = new Point(this.txtInput.Location.X, (this.Height - txtInput.Height) / );
} 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);
}
} private void btnClear_MouseDown(object sender, MouseEventArgs e)
{
txtInput.Clear();
txtInput.Focus();
} private void btnSearch_MouseDown(object sender, MouseEventArgs e)
{
if (SearchClick != null)
{
SearchClick(sender, e);
}
}
Forms.FrmAnchor m_frmAnchor;
private void btnKeybord_MouseDown(object sender, MouseEventArgs e)
{
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(, ));
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(, );
btnDelete.FillColor = Color.White;
btnDelete.IsRadius = false;
btnDelete.ConerRadius = ;
btnDelete.IsShowRect = true;
btnDelete.RectColor = Color.FromArgb(, , );
btnDelete.Location = new Point(, );
btnDelete.BtnFont = new System.Drawing.Font("微软雅黑", );
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(, );
btnEnter.FillColor = Color.White;
btnEnter.IsRadius = false;
btnEnter.ConerRadius = ;
btnEnter.IsShowRect = true;
btnEnter.RectColor = Color.FromArgb(, , );
btnEnter.Location = new Point(, );
btnEnter.BtnFont = new System.Drawing.Font("微软雅黑", );
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);
}
} 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;
}
} IntPtr m_HandAppWin;
Process m_HandPWin = null;
string m_HandExeName = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "HandInput\\handinput.exe"); void m_frmAnchor_VisibleChanged(object sender, EventArgs e)
{
if (m_frmAnchor.Visible)
{
var lstP = Process.GetProcessesByName("handinput");
if (lstP.Length > )
{
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();
}
m_HandAppWin = m_HandPWin.MainWindowHandle;
Control p = m_frmAnchor.Controls.Find("keyborder", false)[];
SetParent(m_HandAppWin, p.Handle);
ControlHelper.SetForegroundWindow(this.FindForm().Handle);
MoveWindow(m_HandAppWin, -, -, , , true);
}
else
{
if (m_HandAppWin != IntPtr.Zero)
{
if (m_HandPWin != null && !m_HandPWin.HasExited)
m_HandPWin.Kill();
m_HandPWin = null;
m_HandAppWin = IntPtr.Zero;
}
}
} private void UCTextBoxEx_MouseDown(object sender, MouseEventArgs e)
{
this.ActiveControl = txtInput;
} private void UCTextBoxEx_Load(object sender, EventArgs e)
{
if (!Enabled)
{
base.FillColor = Color.FromArgb(, , );
txtInput.BackColor = Color.FromArgb(, , );
}
else
{
FillColor = _FillColor;
txtInput.BackColor = _FillColor;
}
}
[DllImport("user32.dll", SetLastError = true)]
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
[DllImport("user32.dll", EntryPoint = "ShowWindow")]
private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, int hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);
private const int GWL_STYLE = -;
private const int WS_CHILD = 0x40000000;//设置窗口属性为child [DllImport("user32.dll", EntryPoint = "GetWindowLong")]
public static extern int GetWindowLong(IntPtr hwnd, int nIndex); [DllImport("user32.dll", EntryPoint = "SetWindowLong")]
public static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong); [DllImport("user32.dll")]
private extern static IntPtr SetActiveWindow(IntPtr handle);
}
}
 namespace HZH_Controls.Controls
{
partial class UCTextBoxEx
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UCTextBoxEx));
this.txtInput = new HZH_Controls.Controls.TextBoxEx();
this.imageList1 = new System.Windows.Forms.ImageList();
this.btnClear = new System.Windows.Forms.Panel();
this.btnKeybord = new System.Windows.Forms.Panel();
this.btnSearch = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// txtInput
//
this.txtInput.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
this.txtInput.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txtInput.DecLength = ;
this.txtInput.Font = new System.Drawing.Font("微软雅黑", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
this.txtInput.InputType = TextInputType.NotControl;
this.txtInput.Location = new System.Drawing.Point(, );
this.txtInput.Margin = new System.Windows.Forms.Padding(, , , );
this.txtInput.MaxValue = new decimal(new int[] {
,
,
,
});
this.txtInput.MinValue = new decimal(new int[] {
,
,
,
-});
this.txtInput.MyRectangle = new System.Drawing.Rectangle(, , , );
this.txtInput.Name = "txtInput";
this.txtInput.OldText = null;
this.txtInput.PromptColor = System.Drawing.Color.Gray;
this.txtInput.PromptFont = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
this.txtInput.PromptText = "";
this.txtInput.RegexPattern = "";
this.txtInput.Size = new System.Drawing.Size(, );
this.txtInput.TabIndex = ;
this.txtInput.TextChanged += new System.EventHandler(this.txtInput_TextChanged);
//
// imageList1
//
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
this.imageList1.Images.SetKeyName(, "ic_cancel_black_24dp.png");
this.imageList1.Images.SetKeyName(, "ic_search_black_24dp.png");
this.imageList1.Images.SetKeyName(, "keyboard.png");
//
// btnClear
//
this.btnClear.BackgroundImage = global::HZH_Controls.Properties.Resources.input_clear;
this.btnClear.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.btnClear.Cursor = System.Windows.Forms.Cursors.Default;
this.btnClear.Dock = System.Windows.Forms.DockStyle.Right;
this.btnClear.Location = new System.Drawing.Point(, );
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(, );
this.btnClear.TabIndex = ;
this.btnClear.Visible = false;
this.btnClear.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnClear_MouseDown);
//
// btnKeybord
//
this.btnKeybord.BackgroundImage = global::HZH_Controls.Properties.Resources.keyboard;
this.btnKeybord.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.btnKeybord.Cursor = System.Windows.Forms.Cursors.Default;
this.btnKeybord.Dock = System.Windows.Forms.DockStyle.Right;
this.btnKeybord.Location = new System.Drawing.Point(, );
this.btnKeybord.Name = "btnKeybord";
this.btnKeybord.Size = new System.Drawing.Size(, );
this.btnKeybord.TabIndex = ;
this.btnKeybord.Visible = false;
this.btnKeybord.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnKeybord_MouseDown);
//
// btnSearch
//
this.btnSearch.BackgroundImage = global::HZH_Controls.Properties.Resources.ic_search_black_24dp;
this.btnSearch.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.btnSearch.Cursor = System.Windows.Forms.Cursors.Default;
this.btnSearch.Dock = System.Windows.Forms.DockStyle.Right;
this.btnSearch.Location = new System.Drawing.Point(, );
this.btnSearch.Name = "btnSearch";
this.btnSearch.Size = new System.Drawing.Size(, );
this.btnSearch.TabIndex = ;
this.btnSearch.Visible = false;
this.btnSearch.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnSearch_MouseDown);
//
// UCTextBoxEx
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.Transparent;
this.ConerRadius = ;
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnKeybord);
this.Controls.Add(this.btnSearch);
this.Controls.Add(this.txtInput);
this.Cursor = System.Windows.Forms.Cursors.IBeam;
this.IsShowRect = true;
this.IsRadius = true;
this.Name = "UCTextBoxEx";
this.Padding = new System.Windows.Forms.Padding();
this.Size = new System.Drawing.Size(, );
this.Load += new System.EventHandler(this.UCTextBoxEx_Load);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.UCTextBoxEx_MouseDown);
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.ImageList imageList1;
public TextBoxEx txtInput;
private System.Windows.Forms.Panel btnClear;
private System.Windows.Forms.Panel btnSearch;
private System.Windows.Forms.Panel btnKeybord;
}
}

用处及效果

最后的话

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

(三十一)c#Winform自定义控件-文本框(四)的更多相关文章

  1. (十六)c#Winform自定义控件-文本框哪里去了?-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

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

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  3. (二十八)c#Winform自定义控件-文本框(一)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  4. (二十九)c#Winform自定义控件-文本框(二)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  5. (八十二)c#Winform自定义控件-穿梭框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  6. (十八)c#Winform自定义控件-提示框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  7. winform中文本框的一些案例

    项目中经常看到在输入金额时,会加逗号,最近在复习正则表达式,就联系下,界面如下:

  8. winform中文本框添加拖拽功能

    对一个文本框添加拖拽功能: private void txtFolder_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataP ...

  9. (三)c#Winform自定义控件-有图标的按钮

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

随机推荐

  1. EPG开发《异常排查以及解决方案》

    [框架]

  2. Bzoj 3124: [Sdoi2013]直径 题解

    3124: [Sdoi2013]直径 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1222  Solved: 580[Submit][Status] ...

  3. Java编程思想:标准I/O

    import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils; import java.io.*; public class Test { ...

  4. Windows10 OpenSSH 快捷设置 避免 Bad owener or permission on

    配置ssh 有两个地方 ~/.ssh/config 这个亲测失败,怎么搞都报错 bad owner .... c:/programdata/ssh/ssh_config 亲测有效 (显示隐藏文件才看的 ...

  5. 个人永久性免费-Excel催化剂功能第46波-区域集合函数,超乎所求所想

    在常规自定义函数的世界中,一般情况下,仅会输入一堆的参数,最终输出一个结果值,在以往Excel催化剂的自定义函数,已经大量出现输入一堆参数返回多个结果值并自动输出到多个单元格区域内.此项技术可运用的场 ...

  6. Excel催化剂开源第1波-自定义函数的源代码全公开

    Excel催化剂插件从2018年1月1日开始运营,到今天刚好一周年,在过去一年时间里,感谢社区里的许多友人们的关心和鼓励,得以坚持下来,并收获一定的用户量和粉丝数和少量的经济收入回报和个人知名度的提升 ...

  7. python面向过程编程小程序- 模拟超市收银系统

    6.16自我总结 功能介绍 程序功能介绍: 商品信息再读取修改买卖均已xlsx格式 且生成购物记录也按/用户名/购买时间.xlsx格式生成 账号密码输入错误三次按照时间进行冻结 用户信息已json格式 ...

  8. 深入理解 JavaScript 单例模式 (Singleton Pattern)

    概念 单例模式,也叫单子模式,是一种常用的软件设计模式.在应用这个模式时,单例对象的类必须保证只有一个实例存在. 核心:确保只有一个实例,并提供全局访问. 实现思路 一个类能返回对象一个引用(永远是同 ...

  9. web页面保存图片到本地

    web页生成分享海报功能踩坑经验 https://blog.csdn.net/candy_home/article/details/78424642 https://www.jianshu.com/p ...

  10. nginx处理302、303和修改response返回的header和网页内容

    背景 遇到一个限制域名的平台,于是使用nginx在做网站转发,其中目标网站在访问过程中使用了多个302.303的返回状态,以便跳转到指定目标(为什么限制,就是防止他的网站的镜像). 在查找了一段资料后 ...