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

时间:2011-12-08 21:39:07

标签: c++ windows

嗨我想写一个小程序来改变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。

我做错了什么,请帮忙

3 个答案:

答案 0 :(得分:1)

请从

更改主要输入功能
int main(int argc, char* argv[])

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

不需要像wchar_t* file = (wchar_t*)argv[1];那样进行投射,它只会作为wmain arguments are already in wchar_t*

我能够使用您的代码和我的修改并更改我的电脑墙纸

答案 1 :(得分:0)

这是一个看起来很有前途的代码 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.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;

}

答案 2 :(得分: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;
}