windows Win32,C++:创建一个不窃取焦点的弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/852856/
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-09-15 12:27:44  来源:igfitidea点击:

Win32, C++: Creating a popup window without stealing focus

c++windowswinapi

提问by RichieHindle

I am creating a program that displays a popup at certain times (just like some chat clients for example) on which the user can click. However, I do not want to take away the focus from the current application.

我正在创建一个程序,该程序在特定时间显示一个弹出窗口(例如一些聊天客户端),用户可以点击该弹出窗口。但是,我不想从当前的应用程序中拿走重点。

The way I'm doing it now is by using a HWND with WS_POPUPWINDOW and minimizing and then restoring the window. However, this steals the focus from the current application. Setting foreground or hiding and showing a window did not make it appear on the foreground. I would like to be able to keep using a HWND so I can use other elements in this window, but I have no idea how to give it foreground without stealing focus.

我现在这样做的方法是使用带有 WS_POPUPWINDOW 的 HWND 并最小化然后恢复窗口。但是,这从当前应用程序中窃取了焦点。设置前景或隐藏和显示窗口不会使其出现在前景中。我希望能够继续使用 HWND,以便我可以在此窗口中使用其他元素,但我不知道如何在不窃取焦点的情况下将其置于前景。

I use win32 and c++.

我使用 win32 和 C++。

回答by RichieHindle

To show without activating:

要显示而不激活:

ShowWindow(hwnd, SW_SHOWNOACTIVATE);

To raise without activating:

要在不激活的情况下加注:

SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);

回答by Bemipefe

Unfortunately this is not working for me. The window is created with CreateWindowExAand is showed using ShowWindow(hwnd, SW_SHOWNOACTIVATE)however the keyboard focus is still stolen from the window which has the focus at the moment of the creation. (The window is created with layered and trasparent attributes by using SetWindowLong()and SetLayeredWindowAttributes()).

不幸的是,这对我不起作用。该窗口是使用CreateWindowExA创建的,并使用ShowWindow(hwnd, SW_SHOWNOACTIVATE)显示但是键盘焦点仍然从创建时具有焦点的窗口中窃取。(使用SetWindowLong()SetLayeredWindowAttributes()创建具有分层和透明属性的窗口)。

PS: The window which has the focus is not parent of the new created window.

PS:具有焦点的窗口不是新创建的窗口的父窗口。

Solved: It worked when I removed the SetForegroundWindowcall. This function cause the window passed as parameter to be activated.

已解决:当我删除SetForegroundWindow调用时它起作用了。该函数使作为参数传递的窗口被激活。