获取处理桌面/ shell窗口 [英] Get handle to desktop / shell window

查看:234
本文介绍了获取处理桌面/ shell窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的计划,我需要测试,如果用户目前集中在桌面/ shell窗口之一。目前我使用GetShellWindow()从user32.dll中并比较结果GetForegroundWindow()。

In one of my programs I need to test if the user is currently focusing the desktop/shell window. Currently I'm using GetShellWindow() from user32.dll and compare the result to GetForegroundWindow().

此方法工作,直到有人更改桌面墙纸,但一旦壁纸由GetShellWindow改变手柄()从GetForegroundWindow一个不匹配()了,我不太明白为什么那是。 ( OS:的Windows 7 32位)

This approach is working until someone changes the desktop wallpaper, but as soon as the wallpaper is changed the handle from GetShellWindow() doesn't match the one from GetForegroundWindow() anymore and I don't quite get why that is. (OS: Windows 7 32bit)

有没有更好的方法来检查,如果台式机的重点是? preferably之一,如果用户更改壁纸,不会被打破?

Is there a better approach to check if the desktop is focused? Preferably one that won't be broken if the user changes the wallpaper?

编辑:我设计了一个解决办法:我测试手柄来上课的孩子SHELLDLL_DefView。如果是,在桌面上的焦点。虽然它在我的电脑工作,这并不意味着它将所有的时间...

I designed a workaround: I'm testing the handle to have a child of class "SHELLDLL_DefView". If it has, the desktop is on focus. Whilst it's working at my PC that doesn't mean it will work all the time…

推荐答案

事情改变了一点点,因为有幻灯片壁纸在Windows 7中使用。 你说得对与WorkerW,但这仅适用于壁纸设置为幻灯片效果。

The thing changed a little bit since there are slideshows as wallpaper available in Windows 7. You are right with WorkerW, but this works only with wallpaper is set to slideshow effect.

当有设置墙纸模式幻灯片,你必须寻找类的窗口 WorkerW 并检查孩子是否有 SHELLDLL_DefView 。 如果没有幻灯片,您可以使用好老 GetShellWindow()

When there is set the wallpaper mode to slideshow, you have to search for a window of class WorkerW and check the children, whether there is a SHELLDLL_DefView. If there is no slideshow, you can use the good old GetShellWindow().

我有同样的问题在几个月前,我写了一个函数用于获取右边的窗口。不幸的是我无法找到它。但下面应该工作。只有Win32的进口数据丢失:

I had the same problem some months ago and I wrote a function for getting the right window. Unfortunately I can't find it. But the following should work. Only the Win32 Imports are missing:

public enum DesktopWindow
{
    ProgMan,
    SHELLDLL_DefViewParent,
    SHELLDLL_DefView,
    SysListView32
}

public static IntPtr GetDesktopWindow(DesktopWindow desktopWindow)
{
    IntPtr _ProgMan = GetShellWindow();
    IntPtr _SHELLDLL_DefViewParent = _ProgMan;
    IntPtr _SHELLDLL_DefView = FindWindowEx(_ProgMan, IntPtr.Zero, "SHELLDLL_DefView", null);
    IntPtr _SysListView32 = FindWindowEx(_SHELLDLL_DefView, IntPtr.Zero, "SysListView32", "FolderView");

    if (_SHELLDLL_DefView == IntPtr.Zero)
    {
        EnumWindows((hwnd, lParam) =>
        {
            if (GetClassName(hwnd) == "WorkerW")
            {
                IntPtr child = FindWindowEx(hwnd, IntPtr.Zero, "SHELLDLL_DefView", null);
                if (child != IntPtr.Zero)
                {
                    _SHELLDLL_DefViewParent = hwnd;
                    _SHELLDLL_DefView = child;
                    _SysListView32 = FindWindowEx(child, IntPtr.Zero, "SysListView32", "FolderView"); ;
                    return false;
                }
            }
            return true;
        }, IntPtr.Zero);
    }

    switch (desktopWindow)
    {
        case DesktopWindow.ProgMan:
            return _ProgMan;
        case DesktopWindow.SHELLDLL_DefViewParent:
            return _SHELLDLL_DefViewParent;
        case DesktopWindow.SHELLDLL_DefView:
            return _SHELLDLL_DefView;
        case DesktopWindow.SysListView32:
            return _SysListView32;
        default:
            return IntPtr.Zero;
    }
}

在你的情况,你会叫 GetDesktopWindow(DesktopWindow.SHELLDLL_DefViewParent); 获得顶层窗口来检查它是否是前台窗口

In your case you would call GetDesktopWindow(DesktopWindow.SHELLDLL_DefViewParent); to get the top-level window for checking whether it is the foreground window.

这篇关于获取处理桌面/ shell窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆