如何在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后的结果错误代码80070002。

我做错了什么请帮忙

请更改主入口函数

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虽然我还没有亲自测试过:

#include <windows.h>
#include <stdio.h>
const SPI_GETDESKWALLPAPER=115;
void printusage(char *program)
{
    fprintf(stderr, "Usage:  %s background-file.bmpn", program);
    fprintf(stderr, "   Changes desktop background to background-filen");
    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 %sn", oldWallPaper);
    result = SystemParametersInfo(
        SPI_SETDESKWALLPAPER,
        0,
        argp[1],
        0);
    if (!result) {
        dResult = GetLastError();
        fprintf(stderr, "Attempt to set new desktop background failed; code
%dn", 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 %sn", 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;
}

最新更新