我建了一个公共窗体 publicform
公共窗体中有一个设置快捷键的方法,如下
public void ButtonShorKey(Button ButtonControl, Keys keyData)
{
try
{
string sTitle = ButtonControl.Text;
string sKeyName = "[" + GetKeysShortName(keyData) + "]"; if (ownKeys.ContainsKey(ButtonControl))
{
string oldKey = "[" + ownKeys[ButtonControl].ToString() + "]";
ownKeys[ButtonControl] = keyData;
if (sTitle.EndsWith(oldKey))
sTitle = sTitle.Substring(0, sTitle.Length - oldKey.Length);
}
else
ownKeys.Add(ButtonControl, keyData);
ButtonControl.Text = sTitle + sKeyName;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "ButtonShorKey");
}
}
然后,我在另一个窗体Form1中继承了publicform,并在初始化时调用了上面的方法,如下
this.ButtonShorKey(this.btnReturn, Keys.Escape);
但是运行时只看到按钮的Text上已经把[Esc]加上了,但是按[Esc]键确达不到点击按钮的效果,这是什么原因?我就是希望按[Esc]键和点击按钮的效果一样,怎么做呢?
公共窗体中有一个设置快捷键的方法,如下
public void ButtonShorKey(Button ButtonControl, Keys keyData)
{
try
{
string sTitle = ButtonControl.Text;
string sKeyName = "[" + GetKeysShortName(keyData) + "]"; if (ownKeys.ContainsKey(ButtonControl))
{
string oldKey = "[" + ownKeys[ButtonControl].ToString() + "]";
ownKeys[ButtonControl] = keyData;
if (sTitle.EndsWith(oldKey))
sTitle = sTitle.Substring(0, sTitle.Length - oldKey.Length);
}
else
ownKeys.Add(ButtonControl, keyData);
ButtonControl.Text = sTitle + sKeyName;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "ButtonShorKey");
}
}
然后,我在另一个窗体Form1中继承了publicform,并在初始化时调用了上面的方法,如下
this.ButtonShorKey(this.btnReturn, Keys.Escape);
但是运行时只看到按钮的Text上已经把[Esc]加上了,但是按[Esc]键确达不到点击按钮的效果,这是什么原因?我就是希望按[Esc]键和点击按钮的效果一样,怎么做呢?
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;namespace KeyHook
{
/// <summary>
/// 键盘Hook管理类
/// </summary>
public class KeyHook
{
private const int WH_KEYBOARD_LL = 13; //键盘
private const int WM_KEYDOWN = 0x0108;
private const int WM_SYSKEYDOWN = 0x0104;
//键盘处理事件委托.
private delegate int HookHandle(int nCode, int wParam, IntPtr lParam);
//客户端键盘处理事件
public delegate void ProcessKeyHandle(HookStruct param, out bool handle);
//接收SetWindowsHookEx返回值
private static int _hHookValue = 0;
//勾子程序处理事件
private static HookHandle _KeyBoardHookProcedure;
//Hook结构
[StructLayout(LayoutKind.Sequential)]
public class HookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
//设置钩子
[DllImport("user32.dll")]
private static extern int SetWindowsHookEx(int idHook, HookHandle lpfn, IntPtr hInstance, int threadId);
//取消钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern bool UnhookWindowsHookEx(int idHook);
//调用下一个钩子
[DllImport("user32.dll")]
private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
//获取当前线程ID
[DllImport("kernel32.dll")]
private static extern int GetCurrentThreadId();
//Gets the main module for the associated process.
[DllImport("kernel32.dll")]
private static extern IntPtr GetModuleHandle(string name);
private IntPtr _hookWindowPtr = IntPtr.Zero;
//外部调用的键盘处理事件
private static ProcessKeyHandle _clientMethod = null;
/// <summary>
/// 安装勾子
/// <summary>
/// <param name="hookProcess">外部调用的键盘处理事件</param>
public void InstallHook(ProcessKeyHandle clientMethod)
{
_clientMethod = clientMethod;
// 安装键盘钩子
if (_hHookValue == 0)
{
_KeyBoardHookProcedure = new HookHandle(GetHookProc);
_hookWindowPtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);
_hHookValue = SetWindowsHookEx(WH_KEYBOARD_LL, _KeyBoardHookProcedure, _hookWindowPtr, 0);
//如果设置钩子失败.
if (_hHookValue == 0)
UninstallHook();
}
}
//取消钩子事件
public void UninstallHook()
{
if (_hHookValue != 0)
{
bool ret = UnhookWindowsHookEx(_hHookValue);
if (ret) _hHookValue = 0;
}
}
//钩子事件内部调用,调用_clientMethod方法转发到客户端应用。
private static int GetHookProc(int nCode, int wParam, IntPtr lParam)
{
if (nCode >= 0)
{
//转换结构
HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct));
if (_clientMethod != null)
{
bool handle = false;
//调用客户提供的事件处理程序。
_clientMethod(hookStruct, out handle);
if (handle) return 1; //1:表示拦截键盘,return 退出
}
}
return CallNextHookEx(_hHookValue, nCode, wParam, lParam);
}
}
}
private Dictionary<Keys, Button> Buttons = new Dictionary<Keys, Button>();
protected void ButtonShorKey(Button ButtonControl, Keys keyData)
{
if (Buttons.ContainsKey(keyData))
throw new ArgumentException("keyData 已经存在!");
Buttons[keyData] = ButtonControl;
}protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (Buttons.ContainsKey(keyData))
{
Buttons[keyData].PerformClick();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
调用方法,在子窗体直接ButtonShorKey(button1, Keys.Control | Keys.D);即可注册Ctrl+D为button1的快捷键
我的ownKeys是这样定义的,
private Dictionary<Component, Keys> ownKeys = new Dictionary<Component, Keys>();
这和button间如何转换呢,怎么写?
不用转,你传的不就是Button吗,直接取传的那个就行
private Dictionary<Keys, Button> Buttons = new Dictionary<Keys, Button>();
我定义的是
private Dictionary<Component, Keys> ownKeys = new Dictionary<Component, Keys>();
我是想问下,这样我在
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 里面应该怎么写呢?
文本框如果想获取焦点,设置好TabIndex和Label的TabIndex就行了
菜单可以直接设置快捷键,不需要这种来设置
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;namespace ICardinal
{
public delegate void ParameterReceive(params object[] objs); public class WuForm : Form
{
private string m_ModuleID = null;
private Dictionary<Component, Keys> ownKeys = new Dictionary<Component, Keys>(); public event ParameterReceive OnParameterReceive;
public void SetParameters(params object[] objs)
{
if (OnParameterReceive != null)
OnParameterReceive(objs);
} public string ModuleID
{
get
{
if (m_ModuleID == null)
{
WuForm fParent = this;
while (fParent.ParentForm != null)
{
if (fParent.ParentForm is WuForm)
{
fParent = (this.ParentForm as WuForm);
if (fParent.ModuleID != null)
{
m_ModuleID = fParent.ModuleID;
break;
}
}
}
}
return m_ModuleID;
}
} internal void SetModuleID(string ModuleID)
{
m_ModuleID = ModuleID;
} public WuForm()
{
this.Font = new System.Drawing.Font("SimSun", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
} protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
} private string GetKeysShortName(Keys keyData)
{
string sTmp = keyData.ToString();
if (sTmp.Length > 3)
return sTmp.Substring(0, 3);
else
return sTmp;
} /// <summary>
/// 将按钮分配一个快捷键
/// </summary>
/// <param name="ButtonControl">铵钮对象</param>
/// <param name="keyData">快捷键</param>
public void ButtonShorKey(Button ButtonControl, Keys keyData)
{
try
{
string sTitle = ButtonControl.Text;
string sKeyName = "[" + GetKeysShortName(keyData) + "]"; if (ownKeys.ContainsKey(ButtonControl))
{
string oldKey = "[" + ownKeys[ButtonControl].ToString() + "]";
ownKeys[ButtonControl] = keyData;
if (sTitle.EndsWith(oldKey))
sTitle = sTitle.Substring(0, sTitle.Length - oldKey.Length);
}
else
ownKeys.Add(ButtonControl, keyData);
ButtonControl.Text = sTitle + sKeyName;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "ButtonShorKey");
}
}
public void ButtonShorKey(ToolStripButton ButtonControl, Keys keyData)
{
try
{
string sTitle = ButtonControl.Text;
string sKeyName = "[" + GetKeysShortName(keyData) + "]"; if (ownKeys.ContainsKey(ButtonControl))
{
string oldKey = "[" + ownKeys[ButtonControl].ToString() + "]";
ownKeys[ButtonControl] = keyData;
if (sTitle.EndsWith(oldKey))
sTitle = sTitle.Substring(0, sTitle.Length - oldKey.Length);
}
else
ownKeys.Add(ButtonControl, keyData);
ButtonControl.Text = sTitle + sKeyName;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "ButtonShorKey");
}
}
public void ButtonShorKey(TextBox TxtControl, Keys keyData)
{
try
{
if (!ownKeys.ContainsKey(TxtControl))
ownKeys.Add(TxtControl, keyData);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "ButtonShorKey");
}
}
public void ButtonShorKey(TabPage TabControl, Keys keyData)
{
try
{
if (!ownKeys.ContainsKey(TabControl))
ownKeys.Add(TabControl, keyData);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "ButtonShorKey");
}
} protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (this.IsMdiChild)
{
if (m.Msg == 0x0112)
{
if (m.WParam.ToInt32() == 0xF120)
return;
if (m.WParam.ToInt32() == 0xF060)
return;
if (m.WParam.ToInt32() == 0xF020)
return;
}
}
base.WndProc(ref m);
} }
}
按快捷键执行文本框keypress?keypress只是在按键的时候出发,手动出发干什么?ToolStrip快捷键可以直接设置ShortcutKeys属性就可以了。