windows 如何让我的最顶层窗口保持在顶部?

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

How to keep my topmost window on top?

c++windowstopmost

提问by Misko Mare

I will first explain why I need it, because I anticipate that the first response will be "Why do you need it?". I want to detect when the mouse cursor is on an edge of the screen and I don't want to use hooks. Hence, I created one pixel wide TOPMOST invisible window.

我将首先解释为什么我需要它,因为我预计第一反应将是“你为什么需要它?”。我想检测鼠标光标何时位于屏幕边缘并且我不想使用钩子。因此,我创建了一个像素宽的TOPMOST 隐形窗口。

I am using C++ on Win XP, so when the window is created (CreateWindowEx(WS_EX_TOPMOST | WS_EX_TRANSPARENT ...) everything works fine.

我在 Win XP 上使用 C++,所以当创建窗口时 (CreateWindowEx(WS_EX_TOPMOST | WS_EX_TRANSPARENT ...) 一切正常。

Unfortunately, if a user moves another topmost window, for example the taskbar over my window, I don't get mouse movements.

不幸的是,如果用户移动另一个最顶层的窗口,例如我的窗口上的任务栏,我不会得到鼠标移动。

I tried to solve this similarly to approaches suggested in: How To Keep an MDI Window Always on Top

我试图以类似于以下建议的方法解决这个问题: 如何保持 MDI 窗口始终在最前面

I tried to check for Z-order of my topmost window in WM_WINDOWPOSCHANGED first with

我尝试首先检查 WM_WINDOWPOSCHANGED 中最顶层窗口的 Z 顺序

case WM_WINDOWPOSCHANGED :
    WINDOWPOS* pWP = (WINDOWPOS*)lParam;

yet pWP->hwnd points to my window and pWP->hwndInsertAfter is 0, which should mean that my window is on the top of the Z, even though it is covered with the taskbar. Then I tried:

然而 pWP->hwnd 指向我的窗口并且 pWP->hwndInsertAfter 是 0,这应该意味着我的窗口在 Z 的顶部,即使它被任务栏覆盖。然后我尝试:

case WM_WINDOWPOSCHANGED :
    HWND topWndHndl = GetNextWindow(myHandle, GW_HWNDPREV)
    GetWindowText(topWndHndl, pszMem, cTxtLen + 1);

and I'll always get that the "Default IME" window is on top of my window. Even if try to bring my window to the top with SetWindowPos() or BringWindowToTop (), "Default IME" stays on the top. I don't know what is "Default IME" and how to detect if the taskbar is on top of my window.

而且我总是会发现“默认 IME”窗口位于我的窗口顶部。即使尝试使用 SetWindowPos() 或 BringWindowToTop () 将我的窗口置于顶部,“默认 IME”仍会保持在顶部。我不知道什么是“默认 IME”以及如何检测任务栏是否在我的窗口顶部。

So my question is: How to detect that my topmost window is not the top topmost window anymore and how to keep it on the top?

所以我的问题是:如何检测我的最顶层窗口不再是最顶层的窗口以及如何将其保持在顶部?

P.S. I know that a "brute force" approach of periodically bringing my window to the top works, yet is ugly and could have some unwanted inference with the notification window for example. (Bringing my window to the top will hide the notification window.)

PS我知道定期将我的窗口带到顶部的“蛮力”方法有效,但很丑陋,并且可能对通知窗口产生一些不必要的推断。(将我的窗口置于顶部将隐藏通知窗口。)

Thank you on your time and suggestions!

感谢您的时间和建议!

回答by John Weldon

TopMost, is always a tricky thing. There is no way to override another window that specifies itself as TopMost.

TopMost,总是一件棘手的事情。无法覆盖将自身指定为 TopMost 的另一个窗口。

Raymond Chen has a good article on this.

Raymond Chen 在这方面有一篇很好的文章。

Also a duplicate of this.

也是this的副本。