简体   繁体   English

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

[英]Bringing Window to the Front in C# using Win32 API

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?使用 win32 API 解决此问题的最佳方法是什么? I have tried SetForeground() but it always steals focus and does not consistenly work.我试过 SetForeground() 但它总是偷走焦点并且不能始终如一地工作。

What is the best way to go about this?解决这个问题的最佳方法是什么? Any thoughts?有什么想法吗?

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. SetForegroundWindow 函数将创建指定窗口的线程置于前台并激活该窗口。 Keyboard input is directed to the window键盘输入被定向到窗口

Try capturing the focus with SetCapture prior to making the call.在拨打电话之前尝试使用SetCapture捕捉焦点。 Also look into different ways of bringing the window to the front: SetForeGroundWindow, SetActiveWindow, even simulating a mouse click can do this.还要研究将窗口置于最前面的不同方法:SetForeGroundWindow、SetActiveWindow,甚至模拟鼠标单击也可以做到这一点。

What is the difference between SetForeGroundWindow, SetActiveWindow, and BringWindowToTop? SetForeGroundWindow、SetActiveWindow 和BringWindowToTop 之间有什么区别? It appears as if they all do the same thing.看起来好像他们都在做同样的事情。

According to MSDN, SetForeGroundWindow will activate the window and direct keyboard focus to it.根据 MSDN, SetForeGroundWindow 将激活窗口并将键盘焦点指向它。 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. SetActiveWindow 与 SetForeGroundWindow 做同样的事情,但如果您的应用程序不是最前面的应用程序,它不会做任何事情。 Finally, BringWindowToTop only brings the window to the top, and doesn't change the keyboard focus.最后,BringWindowToTop 只将窗口带到顶部,不会改变键盘焦点。

Have you tried using SetWindowPos.您是否尝试过使用 SetWindowPos。 This is the canonical function for moving, resizing and setting z-order in Windows.这是在 Windows 中移动、调整大小和设置 z 顺序的规范函数。 There is a SWP_NOACTIVATE flag you can use.您可以使用 SWP_NOACTIVATE 标志。 Look at http://msdn.microsoft.com/en-us/library/ms633545(VS.85).aspx .查看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.我没有在属于另一个进程的窗口上尝试过这个,但它可能值得一试。

You can try the BringWindowToTop function to not steal focus.您可以尝试使用BringWindowToTop 函数来不窃取焦点。 I haven't used it, but it seems to be what you're looking for.我没用过,但它似乎是你要找的。

SetWindowPos + SWP_NOACTIVATE 完成这项工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM