【发布时间】:2011-06-10 20:25:49
【问题描述】:
我正在使用 C# 应用程序来侦听全局键组合 (ctrl+F9),这会将特定窗口置于前面。
这是我用来将窗口置于前面的代码,它仅在由 Button 事件触发时才有效:
private void button3_Click(object sender, EventArgs e)
{
SetForegroundWindow(ptrActiveWindow.ToInt32());
ShowWindowAsync(ptrActiveWindow, SW_RESTORE);
}
对于挂钩,我使用了一个取自 http://www.codeproject.com/KB/cs/CSLLKeyboardHook.aspx 的类,此处完整列出:
每当我在 CTRL KeyDown 之后和 CTRL KeyUp 之前(仍然按下 CTRL)有一个 F9 KeyUp 事件,我都会调用我的方法:
private void restore(IntPtr hWnd)
{
IntPtr ptrCurrentActiveWindow = GetForegroundWindow(); //comment line
ShowWindowAsync(ptrCurrentActiveWindow, SW_MINIMIZE); //comment line
ShowWindowAsync(hWnd, SW_RESTORE);
SetFocus(hWnd);
SetForegroundWindow(hWnd.ToInt32());
}
这没有任何作用。我的窗口在后台被激活(我可以看到它在任务栏中闪烁),但没有恢复。
我可以解决这个问题的唯一方法是使用注释代码:最小化当前活动的窗口,然后恢复我想看到的窗口。
感谢所有帮助, 谢谢。
全局热键,工作版本:
private void Form1_Load(object sender, EventArgs e)
{
string atomName = Thread.CurrentThread.ManagedThreadId.ToString("X8") + this.GetType().FullName;
short HotkeyID = GlobalAddAtom(atomName);
if (!RegisterHotKey(this.Handle, HotkeyID, (uint)GlobalHotkeys.MOD_CONTROL, (uint)Keys.D5))
listBox.Items.Add("failed: " + "unable to register hotkey. Error: " + Marshal.GetLastWin32Error().ToString());
else
listBox.Items.Add("succeeded adding hotkey id"+(uint)Keys.D5);
}
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;
if (m.Msg == WM_HOTKEY)
{
if ((short)m.WParam==HotkeyID)
listBox.Items.Add("Hotkey."+ (short)m.WParam);
}
base.WndProc(ref m);
}
【问题讨论】:
标签: c#