//窗体控制器
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace com.filamm.gui.Default
{
class clsCtl_frmForm1
{
#region 变量
//窗体句柄
frmForm1 m_objViewer;
#endregion
#region 构造函数
/// <summary>
/// 无参构造
/// </summary>
public clsCtl_frmForm1()
{ }
/// <summary>
/// 有参构造
/// </summary>
/// <param name="p_frmForm1"></param>
public clsCtl_frmForm1(frmForm1 p_frmForm1)
{
this.m_objViewer = p_frmForm1;
}
#endregion
#region 获取或设置窗体句柄
/// <summary> /// 获取或设置窗体句柄
/// </summary>
public frmForm1 m_ObjViewer
{
get { return this.m_objViewer; }
set { this.m_objViewer = value; }
}
#endregion
#region 初始化
/// <summary>
/// 初始化
/// </summary>
public void m_mthFrmInit()
{
m_mthSubscriable();
}
#endregion
#region 事件订阅
/// <summary>
/// 事件订阅
/// </summary>
private void m_mthSubscriable()
{
this.m_objViewer.txtBox1.KeyPress += new KeyPressEventHandler(m_mthNumericable);
this.m_objViewer.Activated += new EventHandler(m_mthFrmActived);
this.m_objViewer.Leave += new EventHandler(m_mthFrmLeave);
}
#endregion
#region 只能输入数字
/// <summary>
/// 只能输入数字
/// </summary>
/// <param name="sender"></param>
/// <param name="p_eKeyPress"></param>
public void m_mthNumericable(Object sender, System.Windows.Forms.KeyPressEventArgs p_eKeyPress)
{
string regPattern = @"^[0-9]*$";
if(!Regex.IsMatch(p_eKeyPress.KeyChar.ToString(), regPattern))
{
p_eKeyPress.Handled = true;
}
}
#endregion
#region 只能输入字母
/// <summary>
/// 只能输入英文字母
/// </summary>
/// <param name="sender"></param>
/// <param name="p_eKeyPress"></param>
public void m_mthEnglishable(Object sender, System.Windows.Forms.KeyPressEventArgs p_eKeyPress)
{
string regPattern = @"^[A-Za-z]+$";
if (!Regex.IsMatch(p_eKeyPress.KeyChar.ToString(), regPattern))
{
p_eKeyPress.Handled = true;
}
}
#endregion
#region 只能输入数字与字母
/// <summary>
/// 只能输入英文字母
/// </summary>
/// <param name="sender"></param>
/// <param name="p_eKeyPress"></param>
public void m_mthNumEngable(Object sender, System.Windows.Forms.KeyPressEventArgs p_eKeyPress)
{
string regPattern = @"^[A-Za-z0-9]+$";
if (!Regex.IsMatch(p_eKeyPress.KeyChar.ToString(), regPattern))
{
p_eKeyPress.Handled = true;
}
}
#endregion
#region 只能输入汉字
/// <summary>
/// 只能输入汉字
/// </summary>
/// <param name="sender"></param>
/// <param name="p_eKeyPress"></param>
public void m_mthChieseable(Object sender, System.Windows.Forms.KeyPressEventArgs p_eKeyPress)
{
string regPattern = @"^[\u4e00-\u9fa5]{0,}$";
if (!Regex.IsMatch(p_eKeyPress.KeyChar.ToString(), regPattern))
{
p_eKeyPress.Handled = true;
}
}
#endregion
#region 注册热键
/// <summary>
/// 注册热键
/// </summary>
private void m_mthFrmActived(object sender, EventArgs e)
{
clsHotKey.RegisterHotKey(this.m_objViewer.Handle, 100, clsHotKey.KeyModifiers.Ctrl, Keys.O);
clsHotKey.RegisterHotKey(this.m_objViewer.Handle, 200, clsHotKey.KeyModifiers.None, Keys.Enter);
}
#endregion
#region 撤销热键
/// <summary>
/// 撤销热键
/// </summary>
private void m_mthFrmLeave(object sender, EventArgs e)
{
clsHotKey.UnregisterHotKey(this.m_objViewer.Handle, 100);
clsHotKey.UnregisterHotKey(this.m_objViewer.Handle, 200);
}
#endregion
public void m_mthBtnValidate()
{
if (MessageBox.Show("确定") != DialogResult.OK)
{
return;
}
}
#region 热键事件处理
/// <summary>
/// 热键事件处理
/// </summary>
/// <param name="m"></param>
public void m_mthWndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;
switch(m.Msg)
{
case WM_HOTKEY:
switch (m.WParam.ToInt32())
{
case 100:
m_mthBtnValidate();
break;
case 200:
if (this.m_objViewer.ActiveControl.Parent.Equals(this.m_objViewer.panel1)) {
if (this.m_ObjViewer.ActiveControl is TextBox)
{
if (this.m_ObjViewer.ActiveControl.Name == "textBox5") {
m_mthBtnValidate();
}
else
{
SendKeys.Send("{TAB}");
}
}
}
break;
}
break;
}
}
#endregion
}
}
//clsHotKey.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace com.filamm.gui.Default
{
class clsHotKey
{
#region 热键注册
/// <summary>
/// 热键注册
/// </summary>
/// <param name="hWnd"></param>
/// <param name="id"></param>
/// <param name="fsModifiers"></param>
/// <param name="vk"></param>
[DllImport("user32.dll")]
public static extern void RegisterHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk);
#endregion
#region 热键撤销
/// <summary>
/// 热键撤销,
/// 如果函数执行成功,返回值不为0。
/// 如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError
/// </summary>
/// <param name="hWnd"></param>
/// <param name="id"></param>
[DllImport("user32.dll")]
public static extern void UnregisterHotKey(IntPtr hWnd, int id);
#endregion
#region 定义辅助键
/// <summary>
/// 定义辅助键
/// 定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
/// </summary>
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
#endregion
}
}
浙公网安备 33010602011771号