这个程序仅供编程参考
若想在平时用,请下载微软的wallpaper changer
我这个程序实现了定时更换桌面背景的功能
命令行 wallpaper "your images' directory path" time_in_minute
编程要点
findfirstfile findnextfile 查找图像文件
SystemParametersInfo 修改系统信息,在这儿当然是桌面背景
注册表操作 保存信息
如果做得实用,应加入开机启动功能,自然还要gui
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
/* argc=3;
argv[1]="E://picture//testbmp";
argv[2]="1";
*/ LPCSTR currentDir;
LPCSTR currentImage;
int interval;
HKEY hKey;
WIN32_FIND_DATAA FileData;
HANDLE hFind;
//initialize parameters
if(argc==1){
//find parameters in registry
if(RegOpenKeyA(HKEY_LOCAL_MACHINE,(LPCSTR)"Software//wallpaper",
&hKey)!=ERROR_SUCCESS||
RegQueryValueExA(hKey,(LPCSTR)"interval",0,NULL,
(LPBYTE)interval,NULL)!=ERROR_SUCCESS||
RegQueryValueExA(hKey,(LPCSTR)"directory",0,NULL,
(LPBYTE)currentDir,NULL)!=ERROR_SUCCESS||
RegQueryValueExA(hKey,(LPCSTR)"image",0,NULL,
(LPBYTE)currentImage,NULL)!=ERROR_SUCCESS){
//if connot be found ,wrong usage
cout<<"wrong usage"<<endl
<<"Please use it like this"<<endl
<<"wallpaper /"D://Files//Images/" 30"<<endl
<<"30 is the interval in minutes or"<<endl
<<"wallpaper /"D://Files//Images/""<<endl
<<"with 30 minutes as default interval"<<endl;
exit(1);
}else{
hFind=FindFirstFileA(currentImage,&FileData);
if(hFind==INVALID_HANDLE_VALUE){
cout<<"connot open file";
exit(1);
}
}
}else if(argc==3){
interval=60000*atoi(argv[2]);
currentDir=(LPCSTR)argv[1];
SetCurrentDirectoryA(currentDir);
hFind = FindFirstFileA((LPCSTR)"*.jpg",&FileData);
//hFind = FindFirstFileA((LPCSTR)"228973.jpg",&FileData);
if (hFind == INVALID_HANDLE_VALUE) {
cout<<"Invalid directory.Last Error reports "<<endl;
exit(1);
} else {
currentImage=FileData.cFileName;
//register the directory
RegCreateKeyA(HKEY_LOCAL_MACHINE,(LPCSTR)"Software//wallpaper",&hKey);
RegSetValueExA(hKey,(LPCSTR)"directory",0,REG_SZ,
(CONST BYTE*)currentDir,strlen((const char*)currentDir));
RegSetValueExA(hKey,(LPCSTR)"image",0,REG_SZ,
(CONST BYTE*)currentImage,strlen((const char*)currentImage));
RegSetValueExA(hKey,(LPCSTR)"interval",0,REG_DWORD,
(CONST BYTE*)&interval,4);
}
}else{//wrong parameters
cout<<"wrong parameters"<<endl;
exit(1);
}
//circle and wait
while(1){
//set wallpaper
bool b=false;
string fullpath((char*)currentDir);
fullpath+=string("//");
fullpath+=string((char*)currentImage);
b=SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0,(PVOID)fullpath.c_str(),SPIF_UPDATEINIFILE+SPIF_SENDWININICHANGE);
if(b==false){
cout<<"failed to call /"SystemParametersInfoA/" ";
exit(1);
}
Sleep(interval);
if (!FindNextFileA(hFind, &FileData)){
if (GetLastError() == ERROR_NO_MORE_FILES){
hFind = FindFirstFileA((LPCSTR)"*.jpg",&FileData);
}else{
printf("Couldn't find next file.");
exit(1);
}
}
currentImage=FileData.cFileName;
//register the currentfile
RegSetValueExA(hKey,(LPCSTR)"image",0,REG_SZ,
(CONST BYTE*)currentImage,strlen((const char*)currentImage));
}
//FindClose(hFind);
//RegCloseKey(hKey);
return 0;
}