【问题标题】:How to change back and forth application focus with global hotkey in C#如何在 C# 中使用全局热键来回更改应用程序焦点
【发布时间】:2020-10-15 22:05:36
【问题描述】:

我正在创建自己的 C# 剪贴板管理器,并且我有一个全局热键 ALT+H,它将触发从剪贴板中删除文本格式。我的应用程序在后端运行,仅带有托盘图标。因此,这工作正常,但我的问题是当我在应用程序中时,例如Word 并且我正在按下热键,然后它会显示这些应用程序中的所有类型的菜单,我不希望这样。

仅供参考,这与我目前在 SO 上的另一个问题非常相关,How to stop further processing global hotkeys in C#。在另一个问题中,这是基于找到热键的解决方案,但我认为可能更好的另一种方法可能是将焦点临时切换到我的应用程序,一旦我的热键不再适用,则可以切换焦点返回原始应用程序。

但是我不知道如何再次切换回原始应用程序!?

到目前为止,我的代码只关注应用程序更改机制:

Programs.cs:

// Import the required "SetForegroundWindow" method
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

Form1.cs:

// Change focus to my application (when hotkey is active)
Program.SetForegroundWindow(this.Handle);

我不确定这是否真的有效,但我可以看到原始应用程序(例如 Word)失去了焦点,我的应用程序仍然可以正常工作,所以我确实希望它可以正常工作。

我的问题是 - 如何从原始应用程序获取 hWnd(?) 句柄,以便在完成后切换回它?如果我在任何应用程序中不是,但例如只是在 Windows 桌面上?然后会发生什么 - 它可以变回那个吗?

我将不胜感激任何可以提供帮助的提示,因为我目前还不是真正的 C# 开发人员;-)

【问题讨论】:

  • 你在做什么真的很可怕,ALT+字母键应该可以进入窗口中的菜单项,你为什么要打破它?我实际上对如何做你想做的事有一个很好的想法,但不能容忍这种邪恶:)
  • 在 Set.. 之前,您是否考虑过调用 GetForegroundWindow 并记住它?

标签: c# .net winforms global-hotkey


【解决方案1】:

我自己找到了解决方案,并将解释对我有用的方法。我这里有这段代码:

using System.Runtime.InteropServices;

[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();

// I get in to here when the clipboard has changed and I need to find the application that has changed the clipboard

// Get the active/originating application handle
IntPtr originatingHandle = GetForegroundWindow();

// ------------------
// Do "some stuff" - this is not required for the application switching but it will get the application process name for the active/originating application

// Get the process ID from the active application
uint processId = 0;
GetWindowThreadProcessId(originatingHandle, out processId);

// Get the process name from the process ID
string appProcessName = Process.GetProcessById((int)processId).ProcessName;

// End "some stuff"
// ------------------

// Change focus to my application - this code is inside my main form (Form1)
SetForegroundWindow(this.Handle);

// Do some more stuff - whatever is required for my application to do
// ...

// Change focus back to the originating application again
SetForegroundWindow(originatingHandle);

至少上面的代码对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-22
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2010-11-14
    • 2016-07-12
    相关资源
    最近更新 更多