如何改变在c + +中的活动桌面壁纸

嗨,我想写一个小程序,在Windows 7中更改壁纸

我想使用下面的代码:

#include "windows.h" #include "wininet.h" #include "shlobj.h" #include "wchar.h" #include <iostream> void SetWallpaper(LPCWSTR file){ CoInitializeEx(0,COINIT_APARTMENTTHREADED); IActiveDesktop* desktop; HRESULT status = CoCreateInstance(CLSID_ActiveDesktop,NULL,CLSCTX_INPROC_SERVER,IID_IActiveDesktop,(void**)&desktop); WALLPAPEROPT wOption; ZeroMemory(&wOption, sizeof(WALLPAPEROPT)); wOption.dwSize=sizeof(WALLPAPEROPT); wOption.dwStyle = WPSTYLE_CENTER; status = desktop->SetWallpaper(file,0); wcout << status << endl; status = desktop->SetWallpaperOptions(&wOption,0); wcout << status << endl; status = desktop->ApplyChanges(AD_APPLY_ALL); wcout << status << endl; desktop->Release(); CoUninitialize(); } int wmain(int argc, wchar* argv[]){ if(argc<=1){ wcout << "use: " << argv[0] <<" path_to_pic.bmp" <<endl; }else{ wchar_t* file = argv[1]; SetWallpaper(file); } getchar(); return 0; } 

但是这个代码不会改变壁纸,它只会在调用ApplyChanges后给我的hresult错误代码80070002。

我做错了什么,请帮忙

Solutions Collecting From Web of "如何改变在c + +中的活动桌面壁纸"

请从您的主要输入功能

 int main(int argc, char* argv[]) 

 int wmain(int argc, wchar_t* argv[] ) 

不需要像wchar_t* file = (wchar_t*)argv[1]; 它只会工作,因为你的wmain参数已经在wchar_t *

我能够使用你的代码和我的修改,并改变我的电脑墙纸

这里是一个看起来很有前途的代码http://answers.google.com/answers/threadview/id/512662.html,虽然我没有自己测试&#xFF1A;

 #include <windows.h> #include <stdio.h> const SPI_GETDESKWALLPAPER=115; void printusage(char *program) { fprintf(stderr, "Usage: %s background-file.bmp\n", program); fprintf(stderr, " Changes desktop background to background-file\n"); return; } int main(int argc, char *argp[]) { DWORD dResult; BOOL result; char oldWallPaper[255]; if (argc != 2) { printusage(argp[0]); return 1; } result = SystemParametersInfo( SPI_GETDESKWALLPAPER, sizeof(oldWallPaper)-1, oldWallPaper, 0); fprintf(stderr, "Current desktop background is %s\n", oldWallPaper); result = SystemParametersInfo( SPI_SETDESKWALLPAPER, 0, argp[1], 0); if (!result) { dResult = GetLastError(); fprintf(stderr, "Attempt to set new desktop background failed; code %d\n", dResult); fprintf(stderr, "Will restore prior setting (%s)\n", oldWallPaper); result = SystemParametersInfo( SPI_SETDESKWALLPAPER, 0, oldWallPaper, 0); return 2; } fprintf(stderr, "Desktop background changed to %s\n", argp[1]); return 0; } 

用两个(或多个,添加更多条件)图像来更改您的壁纸的代码

 #include <windows.h> int main() { int i; for(i=0;;i++) { Sleep(1600); if(i%2==0) { const wchar_t *filenm = L"C:\\Pictures\\image1.jpg"; //ADDRESS of first image bool isWallSet=SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0,(void*)filenm,SPIF_UPDATEINIFILE); } else { const wchar_t *filenm = L"C:\\Pictures\\image2.jpg"; //ADDRESS of second image bool isWallSet=SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0,(void*)filenm,SPIF_UPDATEINIFILE); } } return 0; }