Hi.
can anyone please help me find out why this code doesn't work:
char* nextWpName = "mypic.jpg"
SystemParametersInfo (SPI_SETDESKWALLPAPER,0,nextWpName,SPIF_UPDATEINIF ILE);
my wallpaper simply disappears and goes blank.
help...
:(
Printable View
Hi.
can anyone please help me find out why this code doesn't work:
char* nextWpName = "mypic.jpg"
SystemParametersInfo (SPI_SETDESKWALLPAPER,0,nextWpName,SPIF_UPDATEINIF ILE);
my wallpaper simply disappears and goes blank.
help...
:(
From msdn:So try it with a bitmap and not a jpeg.Quote:
...To specify a wallpaper bitmap, set pvParam to point to a null-terminated string containing the name of a bitmap file....
If you wish to use JPEGS or GIFS (as the system does when you change wallpaper manually), you need to roll your sleeves up and use a COM interface - IActiveDesktop
This code opens a dialog allowing you to shose a BMP,GIF or JPEG......then it sets that as wallpaper and stretches it accross the screen...unfortunately, it is targetted at VC++...if you use a different compiler, come back and I will try again
Code:#include <windows.h>//Essential stuff
#include <wininet.h>//This has to be here...bug!!!!!!
#include <shlobj.h>//For IActiveDesktop
#include <comdef.h>//For neat VC++ COM Classes
struct COM_INIT{//To manage COM
COM_INIT(){CoInitialize(0);}
~COM_INIT(){CoUninitialize();}
}COM_INIT_;
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int){
char szFile[MAX_PATH+1] = {0};
OPENFILENAME ofn = {sizeof(OPENFILENAME)};
ofn.hInstance = GetModuleHandle(0);
ofn.lpstrFilter = "BMP\0*.BMP\0JPeg\0*.JPG\0Gif\0*.GIF\0";
ofn.lpstrFile = szFile;
ofn.nMaxFile = MAX_PATH+1;
ofn.lpstrTitle = "Pick Bitmap, JPeg or Gif to replace wallpaper";
if(!GetOpenFileName(&ofn))return 0;
try{
WALLPAPEROPT wpo = {sizeof(wpo),WPSTYLE_STRETCH};
_com_ptr_t<_com_IIID<IActiveDesktop,
&__uuidof(IActiveDesktop)> > lpIAD;
lpIAD.CreateInstance(CLSID_ActiveDesktop);
lpIAD->SetWallpaper(_bstr_t(szFile),0);
lpIAD->SetWallpaperOptions(&wpo,0);
lpIAD->ApplyChanges(AD_APPLY_ALL);
MessageBox(0,"Wallpaper changed","",MB_OK);
}
catch(_com_error& e){
MessageBox(0,(e.Description().length() ?
e.Description() : "COM Error"),0,MB_OK);
}
return 0;
}