确定 window 是否在最顶层

Determine if a window is topmost or not

我可以将 windows 位置设置为最顶层,也可以使用 SetWindowPos 将其设置为非最顶层。但是我不知道如何检查 window 是否在最上面。有没有什么方法可以用 pinvoke 检查 window 是否在最上面?

根据您*使用的 UI 技术,您可以选择以下两项:

您可以使用这些属性来检查某个 window 是否在最顶层,您还可以使用这些属性来 设置 一个 window 最顶层。我更喜欢这些而不是任何 win32 方法。

您可以使用 GetWindowLong() function to check the Extended Window Styles.

未经测试,但我相信它应该有效:

[DllImport("user32.dll", SetLastError=true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);

const int GWL_EXSTYLE = -20;
const int WS_EX_TOPMOST = 0x0008;

public static bool IsWindowTopMost(IntPtr hWnd)
{
    int exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
    return (exStyle & WS_EX_TOPMOST) == WS_EX_TOPMOST;
}