C# WinForm如何实现全局快捷键?
如何实现让程序在后台运行的时候也能响应快捷键?就像千千静听一样。。比如一个A窗体,在后台运行时,用ctrl+alt+s让它调出来,如何实现?小弟初学,要实现的代码,最好通...
如何实现让程序在后台运行的时候也能响应快捷键?就像千千静听一样。。
比如一个A窗体,在后台运行时,用ctrl+alt+s让它调出来,如何实现?
小弟初学,要实现的代码,最好通过运行,谢谢! 展开
比如一个A窗体,在后台运行时,用ctrl+alt+s让它调出来,如何实现?
小弟初学,要实现的代码,最好通过运行,谢谢! 展开
2013-08-15
展开全部
这是个封装好的类,非常好用,不懂怎么使用可以百度给我发消息
using System;
using System.Runtime.InteropServices;
namespace SystemHotKey
{
public delegate void HotkeyEventHandler(int HotKeyID);
public class Hotkey : System.Windows.Forms.IMessageFilter
{
System.Collections.Hashtable keyIDs = new System.Collections.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;
System.Windows.Forms.Application.AddMessageFilter(this);
}
public int RegisterHotkey(System.Windows.Forms.Keys Key, KeyFlags keyflags)
{
UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString());
RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);
keyIDs.Add(hotkeyid, hotkeyid);
return (int)hotkeyid;
}
public void UnregisterHotkeys()
{
System.Windows.Forms.Application.RemoveMessageFilter(this);
foreach (UInt32 key in keyIDs.Values)
{
UnregisterHotKey(hWnd, key);
GlobalDeleteAtom(key);
}
}
public bool PreFilterMessage(ref System.Windows.Forms.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;
using System.Runtime.InteropServices;
namespace SystemHotKey
{
public delegate void HotkeyEventHandler(int HotKeyID);
public class Hotkey : System.Windows.Forms.IMessageFilter
{
System.Collections.Hashtable keyIDs = new System.Collections.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;
System.Windows.Forms.Application.AddMessageFilter(this);
}
public int RegisterHotkey(System.Windows.Forms.Keys Key, KeyFlags keyflags)
{
UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString());
RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);
keyIDs.Add(hotkeyid, hotkeyid);
return (int)hotkeyid;
}
public void UnregisterHotkeys()
{
System.Windows.Forms.Application.RemoveMessageFilter(this);
foreach (UInt32 key in keyIDs.Values)
{
UnregisterHotKey(hWnd, key);
GlobalDeleteAtom(key);
}
}
public bool PreFilterMessage(ref System.Windows.Forms.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;
}
}
}
2013-08-15
展开全部
全屏钩子 , 嵌入WIN32 API ,公共语言运行时不支持 , 还是把网上的代码好好看一遍 , 否则到最后自己都不知道干什么
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
写到全局变量里,不过这个不符合编程规范的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-15
展开全部
使用api 钩子
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询