0

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

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

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

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

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

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

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

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

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

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

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

#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";
}
4

1 回答 1

2

试试IDesktopWallpaper界面和IActiveDesktop界面。

通过创建CLSID_DesktopWallpaper和对象为他们创建CLSID_ActiveDesktop对象。

于 2021-09-23T05:41:31.870 回答