在C ++中在Windows 10墙纸上绘图 [英] Draw on Windows 10 wallpaper in C++

查看:187
本文介绍了在C ++中在Windows 10墙纸上绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用C ++处理Windows墙纸(图标后面)以进行绘制?
可以使活动桌面(Windows XP之后停产)等效,墙纸引擎等效或任何其他类似工具。 (在我的案例中,对墙纸的温度和资源使用情况进行监视)。

Is there a way to get an handle to Windows's wallpaper (behind icons) in C++ in order to draw on it? That would allow to make an active desktop (discontinued after Windows XP) equivalent, a Wallpaper Engine equivalent, or any other similar tool. (Temperature and resources usage monitoring on the wallpaper in my case).

注意: GetDesktopWindow()返回桌面图标级别的窗口,而不是它后面的窗口。

Note: the handle returned by GetDesktopWindow() returns the window at desktop icons level, not behind it.

类似的问题对我不起作用。
我专门尝试了VLC媒体播放器的墙纸模式代码。

Solutions from similar questions aren't working for me. Specifically i tried VLC media player's wallpaper mode code.

关键代码是:

hwnd = FindWindow( _T("Progman"), NULL );
 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
 if( !hwnd )
 {
     msg_Warn( p_vout, "couldn't find \"SysListView32\" window, "
               "wallpaper mode not supported" );
     return;
 }

但它不会在墙纸上绘制。

But it will not draw on the wallpaper.

推荐答案

在桌面图标C#页后面绘制作为参考。本文介绍了该解决方案背后的理论,该理论适用于无论使用哪种编程语言。

Credits to this draw behind desktop icons C# page as reference. The article explains the theory behind the solution, which applies regardless of the programming language being used.

长话短说,更改墙纸时您在Windows 10上看到的平滑淡入淡出的动画通过创建一个完全符合您要求的新窗口并在图标下方绘制来实现此目的。该窗口实现了新墙纸的淡入效果,并由程序管理器创建。

Long story short, the smooth fading animation you see on Windows 10 when changing wallpaper is achieved by creating a new window that does exactly what you're asking for, drawing under the icons. That window achieves the fade-in effect for the new wallpaper, and is created by the Program Manager.

在上述文章中,您可以与C#实现一起看到对以下内容的解释每一步。

In the mentioned article you can see together with C# implementation an explanation of every step. Here i'll write a C++ equivalent keeping comments from the source.

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
    {
    HWND p = FindWindowEx(hwnd, NULL, L"SHELLDLL_DefView", NULL);
    HWND* ret = (HWND*)lParam;

    if (p)
        {
        // Gets the WorkerW Window after the current one.
        *ret = FindWindowEx(NULL, hwnd, L"WorkerW", NULL);
        }
    return true;
    }

HWND get_wallpaper_window()
    {
    // Fetch the Progman window
    HWND progman = FindWindow(L"ProgMan", NULL);
    // Send 0x052C to Progman. This message directs Progman to spawn a 
    // WorkerW behind the desktop icons. If it is already there, nothing 
    // happens.
    SendMessageTimeout(progman, 0x052C, 0, 0, SMTO_NORMAL, 1000, nullptr);
    // We enumerate all Windows, until we find one, that has the SHELLDLL_DefView 
    // as a child. 
    // If we found that window, we take its next sibling and assign it to workerw.
    HWND wallpaper_hwnd = nullptr;
    EnumWindows(EnumWindowsProc, (LPARAM)&wallpaper_hwnd);
    // Return the handle you're looking for.
    return wallpaper_hwnd;
    }

类似C的强制类型转换可以用 reinterpret_cast代替 s,具体取决于您的编码偏好。

The C-like casts can be replaced with reinterpret_casts, according to your coding preferences.

一个音符在文章中没有提到:
由于更改墙纸时会生成一个新的WorkerW窗口以实现褪色效果,因此,如果用户在程序主动绘制和刷新WorkerW实例时尝试更改墙纸,则用户设置的背景将放置在图形的顶部,开始逐渐淡入直到不透明度达到100%,最后将其破坏,从而使WorkerW仍在运行。

One note that isn't mentioned in the article: Since when changing wallpaper a new WorkerW window is generated to achieve the fading effect, if the user tries to change wallpaper while your program is actively drawing and refreshing your instance of WorkerW, the user set background will be placed on top of your drawing, start fading in until it reaches 100% opacity, and lastly be destroyed, leaving your WorkerW still running.

这篇关于在C ++中在Windows 10墙纸上绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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