如何改变活动桌面壁纸在c ++ [英] how to change active desktop wallpaper in c++

查看:106
本文介绍了如何改变活动桌面壁纸在c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi我要写一个小程序来更改Windows 7中的壁纸

hi i want to write a little program to change the wallpaper in 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;
}

但是这段代码不会改变墙纸,它只给我hresult错误

but this code does not change the wallpaper, it only gives me the hresult error-code 80070002 after calling ApplyChanges.

推荐答案

请更改您的主输入功能

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

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

$ c> wchar_t * file =(wchar_t *)argv [1]; wmar参数已在wchar_t *

No casting is necessary like wchar_t* file = (wchar_t*)argv[1]; and it will just work as your wmain arguments are already in wchar_t*

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

这篇关于如何改变活动桌面壁纸在c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆