C++ 如何在c++;
嗨,我想写一个小程序来改变Windows7的墙纸 我想使用以下代码:C++ 如何在c++;,c++,windows,C++,Windows,嗨,我想写一个小程序来改变Windows7的墙纸 我想使用以下代码: #include "windows.h" #include "wininet.h" #include "shlobj.h" #include "wchar.h" #include <iostream> void SetWallpaper(LPCWSTR file){ CoInitializeEx(0,COINIT_APARTMENTTHREADED); IActiveDesktop* deskt
#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;
}
#包括“windows.h”
#包括“wininet.h”
#包括“shlobj.h”
#包括“wchar.h”
#包括
无效设置墙纸(LPCWSTR文件){
CoInitializeX(0,Conit_公寓线程化);
IActiveDesktop*桌面;
HRESULT status=CoCreateInstance(CLSID_ActiveDesktop,NULL,CLSCTX_INPROC_服务器,IID_IActiveDesktop,(void**)和桌面);
壁纸选择;
零内存(&wooption,sizeof(WALLPAPEROPT));
wooption.dwSize=sizeof(壁纸选项);
wooption.dwStyle=WPSTYLE\u中心;
状态=桌面->设置壁纸(文件,0);
wcout请将您的主输入功能从
int main(int argc, char* argv[])
到
不需要像wchar\u t*file=(wchar\u t*)argv[1];
这样的强制转换,它将作为您的
我可以使用您的代码以及我的修改和更改我的电脑墙纸这是一段很有前途的代码
虽然我自己没有测试过:
#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;
}
#包括
#包括
const SPI_getdeskwallpar=115;
无效打印使用(字符*程序)
{
fprintf(stderr,“用法:%s后台文件.bmp\n”,程序);
fprintf(stderr,“将桌面背景更改为背景文件”);
返回;
}
int main(int argc,char*argp[])
{
德沃德·德雷苏特;
布尔结果;
彩色壁纸[255];
如果(argc!=2){
打印使用(argp[0]);
返回1;
}
结果=系统参数INFO(
SPI_Getdesk壁纸,
sizeof(旧墙纸)-1,
旧墙纸,
0);
fprintf(标准,“当前桌面背景为%s\n”,旧墙纸);
结果=系统参数INFO(
SPI_Setdesk壁纸,
0,
argp[1],,
0);
如果(!结果){
dResult=GetLastError();
fprintf(stderr,“尝试设置新桌面背景失败;代码
%d\n“,dResult);
fprintf(标准,“将恢复先前的设置(%s)\n)”,旧墙纸;
结果=系统参数INFO(
SPI_Setdesk壁纸,
0,
旧墙纸,
0);
返回2;
}
fprintf(stderr,“桌面背景更改为%s\n”,argp[1]);
返回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;
}
#包括
int main()
{
int i;
对于(i=0;i++)
{
睡眠(1600);
如果(i%2==0)
{
const wchar_t*filenm=L“C:\\Pictures\\image1.jpg”;//第一个图像的地址
bool isWallSet=系统参数sinfow(SPI_SETDESKWALLPAPER,0,(void*)filenm,SPIF_UPDATEINIFILE);
}
其他的
{
const wchar_t*filenm=L“C:\\Pictures\\image2.jpg”;//第二个图像的地址
bool isWallSet=系统参数sinfow(SPI_SETDESKWALLPAPER,0,(void*)filenm,SPIF_UPDATEINIFILE);
}
}
返回0;
}
一个HRESULT
code of0x80070002
意味着系统找不到指定的文件。尝试硬编码文件的完整绝对路径,看看是否有效。我知道在几个google结果页面之后,错误代码是否总是相同的。我如何给出路径类型并不重要将一个char*
插入一个wchar\u t*
并不能神奇地使它成为一个宽字符串。这会使它变得毫无意义。@peterbining:你应该继续读下去,因为COM函数就是这样指示错误(或缺少错误)的.好的,我将main改为wmain并删除了cast。错误仍然存在。可能我的导入错误?我对此感到惊讶..我能够使用您的代码更改墙纸改为wmain可能问题不在于代码,而在于我的系统?我明天在另一台机器上试用在我的另一个系统上工作,再次尝试,现在它在这里工作。如果我从visual studio中运行此代码,我在main.exe:0xC0000005:访问冲突读取位置0x00000004中的0x00df1500处获得:第一次机会异常。在main.exe:0xC0000005:访问冲突中的0x76f915ee处未经处理的异常。程序“[8604]main.exe:Native”已退出,代码为-1073741819(0xC0000005)。
#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;
}