使用 Win32 API 在 C# 中将窗口置于前台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 13:33:32  来源:igfitidea点击:

Bringing Window to the Front in C# using Win32 API

提问by adeel825

I am writing an application that needs to bring window of an external app to the foreground, and not necessarily steal focus (there is a setting the user can toggle to steal/not steal focus).

我正在编写一个应用程序,需要将外部应用程序的窗口带到前台,而不一定要窃取焦点(用户可以切换为窃取/不窃取焦点)。

What is the best way to go about this using the win32 API? I have tried SetForeground() but it always steals focus and does not consistenly work.

使用 win32 API 解决此问题的最佳方法是什么?我试过 SetForeground() 但它总是偷走焦点并且不能始终如一地工作。

What is the best way to go about this? Any thoughts?

解决这个问题的最佳方法是什么?有什么想法吗?

采纳答案by adeel825

SetForegroundWindow is supposed to steal focus and there are certain cases where it will fail.

SetForegroundWindow 应该窃取焦点,并且在某些情况下它会失败。

The SetForegroundWindow function puts the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window

SetForegroundWindow 函数将创建指定窗口的线程置于前台并激活该窗口。键盘输入被定向到窗口

Try capturing the focus with SetCaptureprior to making the call. Also look into different ways of bringing the window to the front: SetForeGroundWindow, SetActiveWindow, even simulating a mouse click can do this.

在拨打电话之前尝试使用SetCapture捕捉焦点。还要研究将窗口置于最前面的不同方法:SetForeGroundWindow、SetActiveWindow,甚至模拟鼠标单击也可以做到这一点。

回答by NilObject

You can try the BringWindowToTop function to not steal focus. I haven't used it, but it seems to be what you're looking for.

您可以尝试使用BringWindowToTop 函数来不窃取焦点。我没用过,但它似乎是你要找的。

回答by NilObject

What is the difference between SetForeGroundWindow, SetActiveWindow, and BringWindowToTop? It appears as if they all do the same thing.

SetForeGroundWindow、SetActiveWindow 和BringWindowToTop 之间有什么区别?看起来好像他们都在做同样的事情。

According to MSDN, SetForeGroundWindow will activate the window and direct keyboard focus to it. This attempts to work even when your process is in the background. SetActiveWindow does the same thing as SetForeGroundWindow, but it doesn't do anything if your application isn't the frontmost application. Finally, BringWindowToTop only brings the window to the top, and doesn't change the keyboard focus.

根据 MSDN, SetForeGroundWindow 将激活窗口并将键盘焦点指向它。即使您的进程在后台,这也会尝试工作。SetActiveWindow 与 SetForeGroundWindow 做同样的事情,但如果您的应用程序不是最前面的应用程序,它不会做任何事情。最后,BringWindowToTop 只将窗口带到顶部,不会改变键盘焦点。

回答by Mike Thompson

Have you tried using SetWindowPos. This is the canonical function for moving, resizing and setting z-order in Windows. There is a SWP_NOACTIVATE flag you can use. Look at http://msdn.microsoft.com/en-us/library/ms633545(VS.85).aspx. I have not tried this on a window belonging to another process, but it is probably worth a try.

您是否尝试过使用 SetWindowPos。这是在 Windows 中移动、调整大小和设置 z 顺序的规范函数。您可以使用 SWP_NOACTIVATE 标志。查看http://msdn.microsoft.com/en-us/library/ms633545(VS.85).aspx。我没有在属于另一个进程的窗口上尝试过这个,但它可能值得一试。

回答by Kenny Zhou

SetWindowPos + SWP_NOACTIVATE does the job.

SetWindowPos + SWP_NOACTIVATE 完成这项工作。

回答by ManAmongHippos

You could use FindWindow to get the HWND of the window, then use the BringWindowToTop function found in the Win32 API.

您可以使用 FindWindow 获取窗口的 HWND,然后使用 Win32 API 中的BringWindowToTop 函数。