以编程方式更改桌面背景

Changing a desktop background programmatically

本文关键字:桌面 背景 方式更 编程      更新时间:2023-10-16

作为一个有趣的项目,我想为我的笔记本电脑的背景重新创建矩阵雨。我研究了如何做矩阵雨,并且有很多关于如何做到这一点的想法,但我还没有真正找到任何关于以编程方式更改或设置桌面背景的内容。所以,这是我的问题。如何以编程方式更改桌面背景?我最好使用 C 或 C++ 来做到这一点,非常感谢任何帮助!

我的一个程序的一些摘录:

在Windows 7中,系统中只有一个壁纸文件。因此,我们将当前壁纸保存在临时文件中,并将壁纸替换为我们的图像。稍后我们恢复原始文件:

// Get the system's wallpaper filename from the registry
GetRegKeyStrHK(HKEY_CURRENT_USER, "Control Panel\Desktop","WallPaper", szFilename, sizeof(szFilename));
// Now copy that file to a temporary file
CopyFile(szFilename, "C:\myTmpWallpaper.bmp",FALSE);
// Then tell the system to use a new file (it will copy it to the old filename)
SystemParametersInfo (SPI_SETDESKWALLPAPER, 0, (LPSTR) szMyDesktopImage, 0);

函数GetRegKeyStrHK()来自我的库,它从注册表中获取值(壁纸文件名(。


int GetRegKeyStrHK (HKEY hK, const char *szRoot, const char *szName, char *szValue, int iValueSize)
{
    HKEY hkResult;
    int  iKeyType, bufsize, result;
    if (RegOpenKeyEx(hK, szRoot, 0, KEY_READ, &hkResult)
                != ERROR_SUCCESS) return(FALSE);        // no such key
    bufsize=iValueSize;
    result= RegQueryValueEx(hkResult,szName,0, &iKeyType, (BYTE *)szValue, &bufsize);
    RegCloseKey (hkResult);
    if (result != ERROR_SUCCESS) return(FALSE);         // no such name/value pair or buffer too small
    return (TRUE);
}