全局热键的问题 为什么,this.Hide()后,热键就不响应了呢 ? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没有消息到来using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;using System.Collections;using System.Windows.Forms;public class C_全局热键 : IMessageFilter{ public event Action<int> OnHotkey; public enum KeyFlags { MOD_ALT = 0x1, MOD_CONTROL = 0x2, MOD_SHIFT = 0x4, MOD_WIN = 0x8 } class NativeMethods { private NativeMethods() { } #region WIN32 API [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); #endregion } Hashtable keyIDs = new Hashtable(); IntPtr hWnd; public C_全局热键(IntPtr hWnd) { this.hWnd = hWnd; Application.AddMessageFilter(this); } public int RegisterHotkey(Keys Key, KeyFlags keyflags) { UInt32 hotkeyid = NativeMethods.GlobalAddAtom(System.Guid.NewGuid().ToString()); NativeMethods.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) { NativeMethods.UnregisterHotKey(hWnd, key); NativeMethods.GlobalDeleteAtom(key); } } public bool PreFilterMessage(ref Message m) { if (m.Msg == 0x0312) { //这里断点没有到 if (OnHotkey != null) { foreach (UInt32 key in keyIDs.Values) { if ((UInt32)m.WParam == key) { OnHotkey((int)m.WParam); return true; } } } } return false; }} 隐藏窗体: this.WindowState = FormWindowState.Minimized; this.ShowInTaskbar = false; protected override void WndProc(ref Message m) { const int WM_HOTKEY = 0x0312; //按快捷键 switch (m.Msg) { case WM_HOTKEY: switch (m.WParam.ToInt32()) { case 100: //按下的是F7 CopyPosition(); break; default: break; } break; } base.WndProc(ref m); } this.Hide() 在Net的源码中只是一个 public void Hide() { this.Visible = false; }可能隐藏了后,对于P/Invoke的某些方法不起作用了。 问题的正解是: //这里因为 this.ShowInTaskbar=false 会改变窗体句柄值,因此需要再次注册热键,不信?Debug this.Handle因为句柄改变要重新注册一下热键 C#中 使用 WebRequestMethods.Ftp.GetDateTimestamp 获取ftp文件最后修改时间 如何在一个TextBox中比较和匹配上次输入的内容? 麻烦大家看看这段程序哪里错了呀 Winform窗体布局的问题,窗体无故变宽,急急急急急急急急!!!!! 急 C#中WINFORM繼承問題 数据库建表的问题 做过一个web工程,里面的菜单是绑定到数据库表的, 请问winform 里面是否也这样做? ComBox内容添加问题 “C#线程”和“Timer计时器控件”的不同 关于继承问一二? 垂直滚动条(vScrollBar)怎样点上面,数值增加?
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections;
using System.Windows.Forms;public class C_全局热键 : IMessageFilter
{
public event Action<int> OnHotkey;
public enum KeyFlags
{
MOD_ALT = 0x1,
MOD_CONTROL = 0x2,
MOD_SHIFT = 0x4,
MOD_WIN = 0x8
} class NativeMethods
{
private NativeMethods() { } #region WIN32 API [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); #endregion
} Hashtable keyIDs = new Hashtable();
IntPtr hWnd; public C_全局热键(IntPtr hWnd)
{
this.hWnd = hWnd;
Application.AddMessageFilter(this);
} public int RegisterHotkey(Keys Key, KeyFlags keyflags)
{
UInt32 hotkeyid = NativeMethods.GlobalAddAtom(System.Guid.NewGuid().ToString());
NativeMethods.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)
{
NativeMethods.UnregisterHotKey(hWnd, key);
NativeMethods.GlobalDeleteAtom(key);
}
}
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 0x0312)
{ //这里断点没有到
if (OnHotkey != null)
{
foreach (UInt32 key in keyIDs.Values)
{
if ((UInt32)m.WParam == key)
{
OnHotkey((int)m.WParam);
return true;
}
}
}
} return false;
}
}
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;
//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
switch (m.WParam.ToInt32())
{
case 100: //按下的是F7
CopyPosition();
break; default:
break;
}
break;
} base.WndProc(ref m);
}
public void Hide()
{
this.Visible = false;
}
可能隐藏了后,对于P/Invoke的某些方法不起作用了。
//这里因为 this.ShowInTaskbar=false 会改变窗体句柄值,因此需要再次注册热键,不信?Debug this.Handle
因为句柄改变要重新注册一下热键