仅获取我自己的应用程序的最顶层窗口 - 在 C 中

发布于 2024-07-27 00:34:06 字数 406 浏览 4 评论 0原文

我使用以下代码来获取最顶层窗口的句柄:

HWND hwnd;
hwnd = GetForegroundWindow();

问题是它返回系统范围内最顶层的窗口。 有什么方法可以仅从我自己的应用程序中获取最上面的内容吗?

我只想获取我的应用程序的最顶层窗口。 这意味着,我需要一个 API 来获取我自己的应用程序最顶层窗口,而不是像 GetForegroundWindow() 那样获取系统范围最顶层窗口。 谢谢!

编辑:

好的,让我在这里说清楚。 我的问题是我能够获取不属于我的应用程序的窗口的 HWND。 我想要得到的只是我的应用程序的最高值。 如果 HWND 属于另一个应用程序,那么我不应该获取该信息。

I'm using the following code to get a handle of the topmost window:

HWND hwnd;
hwnd = GetForegroundWindow();

The problem with this is that it returns the top most system-wide.
Is there any way to get the topmost ONLY from my own application?

I want to get the top most window ONLY of my application. This means, that I need an API to get my own's app top most window and NOT the systemwide top most window as GetForegroundWindow() does.
Thanks!

EDIT:

OK, let me be clear here. My problem is that I am able to get the HWND for a window that doesn't belong to MY application.
What I want to get is the TOPMOST for ONLY my application.
If the HWND belongs to another application then I should not get the information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽容 2024-08-03 00:34:06

下面是一个可以与 EnumWindows() 一起使用的回调:

BOOL CALLBACK FindTopmostWnd(HWND hwnd, LPARAM lParam)
{
    HWND* pHwnd = (HWND*)lParam;

    HWND myParent = hwnd;
    do
    {
        myParent = GetParent(myParent);
    }
    while (myParent && (myParent != *pHwnd));

    if (myParent != 0)
    {
        // If the window is a menu_worker window then use it's parent
        TCHAR szClassName[7];
        while (0 != GetClassName(hwnd, szClassName, 7)
            && 0 != _tcsncmp(szClassName, TEXT("Dialog"), 6)
            && 0 != _tcsncmp(szClassName, TEXT("Afx"), 3)
            )
        {
            // find the worker's parent
            hwnd = GetParent(hwnd);
        }

        *pHwnd = hwnd;

        return FALSE;
    }

    return TRUE;
}

正如 Adam 指出的,传递给 EnumWindows() 的 LPARAM 应该是指向 HWND 的指针。 所以你可能想做这样的事情:

HWND hTopmostWnd = hWnd;
EnumWindows(FindTopmostWnd, (LPARAM)&hTopmostWnd);

Here is a callback you can use with EnumWindows():

BOOL CALLBACK FindTopmostWnd(HWND hwnd, LPARAM lParam)
{
    HWND* pHwnd = (HWND*)lParam;

    HWND myParent = hwnd;
    do
    {
        myParent = GetParent(myParent);
    }
    while (myParent && (myParent != *pHwnd));

    if (myParent != 0)
    {
        // If the window is a menu_worker window then use it's parent
        TCHAR szClassName[7];
        while (0 != GetClassName(hwnd, szClassName, 7)
            && 0 != _tcsncmp(szClassName, TEXT("Dialog"), 6)
            && 0 != _tcsncmp(szClassName, TEXT("Afx"), 3)
            )
        {
            // find the worker's parent
            hwnd = GetParent(hwnd);
        }

        *pHwnd = hwnd;

        return FALSE;
    }

    return TRUE;
}

As Adam points out, the LPARAM passed to EnumWindows() should be a pointer to an HWND. So you probably want to do something like this:

HWND hTopmostWnd = hWnd;
EnumWindows(FindTopmostWnd, (LPARAM)&hTopmostWnd);
漫漫岁月 2024-08-03 00:34:06

使用 GetTopWindow 函数
像这样:

HWND hwnd;
hwnd = GetTopWindow(NULL);

Use the GetTopWindow Function,
like this:

HWND hwnd;
hwnd = GetTopWindow(NULL);
鹊巢 2024-08-03 00:34:06

我不知道是否有一个函数可以完全执行此操作,但您可能可以自己编写一个。 如果您的应用程序窗口都具有特定的窗口类,那么您可以使用 FindWindow 或 FindWindowEx。

或者,您可以使用 GetForegroundWindow 从所有应用程序获取前景窗口,然后使用 GetWindowLong 检查 HINSTANCE。 如果它不是来自您的应用程序,则继续按 Z 顺序枚举窗口(使用 GetWindow),直到找到应用程序中的第一个窗口。

I don't know that there is a function that does exactly this, but you could probably write one yourself. If your application windows all have a particular window class, then you can use FindWindow or FindWindowEx.

Alternatively, you could use GetForegroundWindow to get the foreground window from all applications and then use GetWindowLong to check the HINSTANCE. If it's not from your application, then keep enumerating the windows by Z-order (using GetWindow) until you find the first one from your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文