如何告诉 Windows 10 使用 WIN32 C/C++ API 平铺、居中或拉伸桌面墙纸?

How does one tell Windows 10 to tile, center, or stretch desktop wallpaper using WIN32 C/C++ API?

目标:使用 C++、Win32 SDK 和 Visual Studio 2019 将桌面墙纸设置为居中、平铺或拉伸

可以使用 SystemParametersInfo() 来更改壁纸。完全没问题。

问题是告诉系统平铺或居中或拉伸墙纸图像。

在网上阅读,壁纸图像是居中、平铺还是拉伸取决于一对注册表项:

HKCU\Control Panel\Desktop\TileWallpaper
HKCU\Control Panel\Desktop\WallpaperStyle

MS 的 WIN32 文档告诉我如何更改图像,但我找不到任何描述如何更改布局的内容。

我有以下代码。这是一个控制台应用程序项目,功能从我较大的 MFC 应用程序中剥离出来,因此是函数名称。该项目的字符集设置为Unicode,因此我使用了W函数。

它确实改变了墙纸图像,但墙纸总是平铺的,无论调用哪个 onWallpaper___() 函数。

Windows 似乎完全忽略了注册表更改。我已验证我的代码确实更改了注册表项的值。

问题:如何告诉 Windows10 使用 WIN32 平铺、居中或拉伸桌面墙纸 C/C++ API?

问题:是否应该使用不同的注册表项?

#include <Windows.h>
#include <iostream>
#include <string>
#include <cassert>

const int CENTERED = 0;
const int TILED = 1;
const int STRETCHED = 2;


void set_wallpaper_registry_keys(int discriminant) {
    BOOL rtn;
    HKEY hKey;
    DWORD TileWallpaper = 0;
    DWORD WallpaperStyle = 0;
    switch (discriminant) {
        case CENTERED: {
            TileWallpaper = 0;
            WallpaperStyle = 1;     // some sources say use 6, makes no difference.
        }
        break;
        case TILED: {
            TileWallpaper = 1;
            WallpaperStyle = 0;
        }
        break;
        case STRETCHED: {
            TileWallpaper = 0;
            WallpaperStyle = 2;
        }
        break;
        default: {
            assert(false);
        }
        break;
    }
    std::wstring key_name(L"Control Panel\Desktop");
    rtn = RegOpenKeyEx(HKEY_CURRENT_USER, key_name.c_str(), 0, KEY_ALL_ACCESS, &hKey);
    assert(rtn == ERROR_SUCCESS);
    rtn = RegSetValueEx(hKey, L"TileWallpaper",  0, REG_DWORD, (BYTE *)&TileWallpaper,  sizeof(DWORD));
    assert(rtn == ERROR_SUCCESS);
    rtn = RegSetValueEx(hKey, L"WallpaperStyle", 0, REG_DWORD, (BYTE *)&WallpaperStyle, sizeof(DWORD));
    assert(rtn == ERROR_SUCCESS);
    rtn = RegFlushKey(hKey);
    assert(rtn == ERROR_SUCCESS);
    rtn = RegCloseKey(hKey);
    assert(rtn == ERROR_SUCCESS);
}


void OnWallpaperCentered() {
    BOOL rtn;
    set_wallpaper_registry_keys(CENTERED);
    // set current image as wallpaper: SPI_SETDESKWALLPAPER
    std::wstring fn = L"c:\tmp\stars.jpg";
    rtn = SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, (void *) (fn.c_str()), SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
    assert(rtn == TRUE);
}


void OnWallpaperTiled() {
    // TODO: Add your command handler code here
    BOOL rtn;
    set_wallpaper_registry_keys(TILED);
    std::wstring fn = L"c:\tmp\snail.jpg";
    rtn = SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, (void *) (fn.c_str()), SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
    assert(rtn == TRUE);
}


void OnWallpaperStretched() {
    // TODO: Add your command handler code here
    BOOL rtn;
    set_wallpaper_registry_keys(STRETCHED);
    std::wstring fn = L"c:\tmp\civ4.jpg";
    rtn = SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, (void*) (fn.c_str()), SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
    assert(rtn == TRUE);
}


int main() {
    //OnWallpaperTiled();     // Tiles the wallpaper
    OnWallpaperCentered();    // Tiles the wallpaper as well
    //OnWallpaperStretched(); // Tiles the wallpaper too
    std::cout << "Hello World!\n";
}

尝试 IDesktopWallpaper interface and IActiveDesktop 个界面。

通过创建 CLSID_DesktopWallpaperCLSID_ActiveDesktop 对象为他们创建对象。