C#设置全局热键
编辑:dnawo 日期:2009-08-28
Hotkey.cs:
Form1.cs:
复制内容到剪贴板
程序代码

using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public delegate void HotkeyEventHandler(int hotkeyid);
public class Hotkey : IMessageFilter
{
Hashtable keyIDs = new Hashtable();
IntPtr hWnd;
public event HotkeyEventHandler OnHotkey;
public enum KeyFlags
{
MOD_ALT = 0x1,
MOD_CONTROL = 0x2,
MOD_SHIFT = 0x4,
MOD_WIN = 0x8
}
[DllImport("user32.dll")]
public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk);
[DllImport("user32.dll")]
public static extern UInt32 UnregisterHotKey(IntPtr hWnd, UInt32 id);
[DllImport("kernel32.dll")]
public static extern UInt32 GlobalAddAtom(String lpString);
[DllImport("kernel32.dll")]
public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);
public Hotkey(IntPtr hWnd)
{
this.hWnd = hWnd;
Application.AddMessageFilter(this);
}
public int RegisterHotkey(Keys key, KeyFlags keyflags)
{
UInt32 hotkeyid = GlobalAddAtom(Guid.NewGuid().ToString());
RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)key);
keyIDs.Add(hotkeyid, hotkeyid);
return (int)hotkeyid;
}
public void UnregisterHotkeys()
{
Application.RemoveMessageFilter(this);
foreach (UInt32 key in keyIDs.Values)
{
UnregisterHotKey(hWnd, key);
GlobalDeleteAtom(key);
}
}
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 0x312)
{
if (OnHotkey != null)
{
foreach (UInt32 key in keyIDs.Values)
{
if ((UInt32)m.WParam == key)
{
OnHotkey((int)m.WParam);
return true;
}
}
}
}
return false;
}
}
}
using System.Collections;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public delegate void HotkeyEventHandler(int hotkeyid);
public class Hotkey : IMessageFilter
{
Hashtable keyIDs = new Hashtable();
IntPtr hWnd;
public event HotkeyEventHandler OnHotkey;
public enum KeyFlags
{
MOD_ALT = 0x1,
MOD_CONTROL = 0x2,
MOD_SHIFT = 0x4,
MOD_WIN = 0x8
}
[DllImport("user32.dll")]
public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk);
[DllImport("user32.dll")]
public static extern UInt32 UnregisterHotKey(IntPtr hWnd, UInt32 id);
[DllImport("kernel32.dll")]
public static extern UInt32 GlobalAddAtom(String lpString);
[DllImport("kernel32.dll")]
public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);
public Hotkey(IntPtr hWnd)
{
this.hWnd = hWnd;
Application.AddMessageFilter(this);
}
public int RegisterHotkey(Keys key, KeyFlags keyflags)
{
UInt32 hotkeyid = GlobalAddAtom(Guid.NewGuid().ToString());
RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)key);
keyIDs.Add(hotkeyid, hotkeyid);
return (int)hotkeyid;
}
public void UnregisterHotkeys()
{
Application.RemoveMessageFilter(this);
foreach (UInt32 key in keyIDs.Values)
{
UnregisterHotKey(hWnd, key);
GlobalDeleteAtom(key);
}
}
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 0x312)
{
if (OnHotkey != null)
{
foreach (UInt32 key in keyIDs.Values)
{
if ((UInt32)m.WParam == key)
{
OnHotkey((int)m.WParam);
return true;
}
}
}
}
return false;
}
}
}
Form1.cs:
复制内容到剪贴板
程序代码

using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Hotkey _hotkey;
private int _hotkeyid;
public Form1()
{
InitializeComponent();
}
public void OnHotkey(int hotkeyid)
{
if (hotkeyid == _hotkeyid)
{
if (this.Visible == true)
this.Visible = false;
else
this.Visible = true;
}
else
{
this.Visible = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
_hotkey = new Hotkey(this.Handle);
_hotkeyid = _hotkey.RegisterHotkey(Keys.F2, Hotkey.KeyFlags.MOD_CONTROL); //定义快键(Ctrl + F2)
_hotkey.OnHotkey += new HotkeyEventHandler(OnHotkey);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
_hotkey.UnregisterHotkeys();
}
}
}
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Hotkey _hotkey;
private int _hotkeyid;
public Form1()
{
InitializeComponent();
}
public void OnHotkey(int hotkeyid)
{
if (hotkeyid == _hotkeyid)
{
if (this.Visible == true)
this.Visible = false;
else
this.Visible = true;
}
else
{
this.Visible = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
_hotkey = new Hotkey(this.Handle);
_hotkeyid = _hotkey.RegisterHotkey(Keys.F2, Hotkey.KeyFlags.MOD_CONTROL); //定义快键(Ctrl + F2)
_hotkey.OnHotkey += new HotkeyEventHandler(OnHotkey);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
_hotkey.UnregisterHotkeys();
}
}
}
评论: 2 | 引用: 0 | 查看次数: 5682





代码都在上边了,测试过的,Copy就能用。
沙发
你好,请问你能发这个程序的源文件给我吗?我邮箱是199989277@qq.com,打扰你拉~~
发表评论
请登录后再发表评论!