上網找了很多個關於HotKey(熱鍵)的例子但是都看不太懂也用不上! 那位高手有沒有比較簡單、完整的例子。如果比較復雜的最好注解的!萬分的感謝!好在vs.net2003中的例子到vs.net2005裡就不能用了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 注册热键 SetHotKey(false, false, true, false, Keys.Q); #region 快捷键 [DllImport("user32.dll", SetLastError = true)] public static extern bool RegisterHotKey( IntPtr hWnd, // window 句柄 int id, // 标识符 KeyModifiers fsModifiers, Keys vk ); /// <summary> /// 卸载快捷键 /// </summary> /// <param name="hWnd"></param> /// <param name="id"></param> /// <returns></returns> [DllImport("user32.dll", SetLastError = true)] public static extern bool UnregisterHotKey( IntPtr hWnd, // window 句柄 int id // 标识符 ); [Flags()] public enum KeyModifiers { None = 0, Alt = 1, Control = 2, Shift = 4, Windows = 8 } /// <summary> /// 触发热键时要执行的命令 /// </summary> private void ProcessHotkey() { if (Status.IsHide == false) { Status.IsHide = true; //MsgBox.Alert("隐藏"); this.Hide(); } else { Status.IsHide = false; this.Show(); //MsgBox.Alert("显示"); } } protected override void WndProc(ref Message m)//监视Windows消息 { const int WM_HOTKEY = 0x0312;//按快捷键 switch (m.Msg) { case WM_HOTKEY: ProcessHotkey();//调用主处理程序 break; } base.WndProc(ref m); } private bool key_Ctrl = false; private bool key_Shift = false; private bool key_Alt = false; private bool key_Windows = false; private Keys key_other; /// <summary> /// 注册快捷键 /// </summary> /// <param name="bCtrl"></param> /// <param name="bShift"></param> /// <param name="bAlt"></param> /// <param name="bWindows"></param> /// <param name="nowKey"></param> private void SetHotKey(bool bCtrl, bool bShift, bool bAlt, bool bWindows, Keys nowKey) { try { this.key_Alt = bAlt; this.key_Ctrl = bCtrl; this.key_Shift = bShift; this.key_Windows = bWindows; this.key_other = nowKey; KeyModifiers modifier = KeyModifiers.None; if (this.key_Ctrl) modifier |= KeyModifiers.Control; if (this.key_Alt) modifier |= KeyModifiers.Alt; if (this.key_Shift) modifier |= KeyModifiers.Shift; if (this.key_Windows) modifier |= KeyModifiers.Windows; RegisterHotKey(Handle, 100, modifier, nowKey); } catch { MsgBox.AlertExclamation("快捷键定义错误!\n可能有别的程序正在使用。"); } } #endregion datagridview的数据绑定 如何重绘TreeView控件,替换默认的+-图标? c# 怎么实现像百度收索那样的文本拖放功能 C# 采集并写入sql数据库代码 大家看看这段代码,为何得到这种结果? 关于TreeView排序,节点移动问题? 关于二维数组取值问题 怎样把progressBar做成滚动条而不是进度条? 求助:WinForm下ListView添加CheckBox以后,选中CheckBox,如何得到该行的索引号?? 为什么在线程里运行的程序不能实例化别人窗口? 关于DataTable删除行的问题 C#中公有为public的变量放入哪里可以更好的被调用?
SetHotKey(false, false, true, false, Keys.Q); #region 快捷键
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, // window 句柄
int id, // 标识符
KeyModifiers fsModifiers,
Keys vk
);
/// <summary>
/// 卸载快捷键
/// </summary>
/// <param name="hWnd"></param>
/// <param name="id"></param>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, // window 句柄
int id // 标识符
);
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
} /// <summary>
/// 触发热键时要执行的命令
/// </summary>
private void ProcessHotkey()
{
if (Status.IsHide == false)
{
Status.IsHide = true;
//MsgBox.Alert("隐藏");
this.Hide();
}
else
{
Status.IsHide = false;
this.Show();
//MsgBox.Alert("显示");
}
}
protected override void WndProc(ref Message m)//监视Windows消息
{
const int WM_HOTKEY = 0x0312;//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotkey();//调用主处理程序
break;
}
base.WndProc(ref m);
}
private bool key_Ctrl = false;
private bool key_Shift = false;
private bool key_Alt = false;
private bool key_Windows = false;
private Keys key_other;
/// <summary>
/// 注册快捷键
/// </summary>
/// <param name="bCtrl"></param>
/// <param name="bShift"></param>
/// <param name="bAlt"></param>
/// <param name="bWindows"></param>
/// <param name="nowKey"></param>
private void SetHotKey(bool bCtrl, bool bShift, bool bAlt, bool
bWindows, Keys nowKey)
{
try
{
this.key_Alt = bAlt;
this.key_Ctrl = bCtrl;
this.key_Shift = bShift;
this.key_Windows = bWindows;
this.key_other = nowKey; KeyModifiers modifier = KeyModifiers.None; if (this.key_Ctrl)
modifier |= KeyModifiers.Control;
if (this.key_Alt)
modifier |= KeyModifiers.Alt;
if (this.key_Shift)
modifier |= KeyModifiers.Shift;
if (this.key_Windows)
modifier |= KeyModifiers.Windows; RegisterHotKey(Handle, 100, modifier, nowKey);
}
catch
{
MsgBox.AlertExclamation("快捷键定义错误!\n可能有别的程序正在使用。");
}
} #endregion