以编程方式更改桌面背景

时间:2017-02-11 10:13:14

标签: c++ c background desktop

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

1 个答案:

答案 0 :(得分:2)

我的一个节目摘录:

在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);
}
相关问题