Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中使用全局热键来回更改应用程序焦点#_C#_.net_Winforms_Global Hotkey - Fatal编程技术网

C# 如何在C中使用全局热键来回更改应用程序焦点#

C# 如何在C中使用全局热键来回更改应用程序焦点#,c#,.net,winforms,global-hotkey,C#,.net,Winforms,Global Hotkey,我正在创建自己的C#剪贴板管理器,我有一个全局热键,ALT+H,它将触发从剪贴板中删除文本格式。我的应用程序在后端运行时仅带有一个托盘图标。因此,这工作正常,但我的问题是,当我在应用程序中,例如Word,按下热键时,它会显示这些应用程序中的所有菜单,我不希望这样 仅供参考,这与我目前的另一个问题非常相关。在另一个问题中,这是基于找到热键的解决方案,但另一种方法,我认为可能更好,可以是临时将焦点切换到我的应用程序,一旦我的热键不再适用,则可以将焦点切换回原始应用程序 但是,我不知道如何再次切换回原

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

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

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

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

程序.cs:

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

// Change focus to my application (when hotkey is active)
Program.SetForegroundWindow(this.Handle);
我不完全确定这是否真的有效,但我可以看到原始应用程序(例如Word)失去了焦点,我的应用程序仍然可以正常工作,所以我希望它可以正常工作

我的问题是-如何从原始应用程序获取
hWnd
(?)句柄,以便在完成后切换回它?如果我不在任何应用程序中,但仅在WIndows桌面上,该怎么办?接下来会发生什么?它能变回那样吗


我将非常感谢任何可以帮助我的提示,因为我目前还不是真正的C#开发者;-)

我自己已经找到了解决方案,并将解释我的解决方案。我这里有这个代码:

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);

至少上面的代码对我来说是有效的。

检查你正在做的事情真的很可怕,ALT+字母键应该可以进入windows中的菜单项,你为什么要破坏它?事实上,我对如何做你想做的事有一个很好的想法,但我不能宽恕这种邪恶:)在你开始之前,你有没有考虑过给GetForegroundIndow打电话并记住它?