Desktophintergrundbild verändern
-
Frage
Ich verwende Windows XP und würde gerne ein Programm schreiben, das bei jedem Start von Windows einen anderen Desktiphintergrund lädt. Wie könnte ich das bewerkstelligen
Danke im Voraus
-
Hab ich auch mal gemacht. Kannste auf meiner HP bewundern. Du brauchst dazu die Schnittstelle IActiveDesktop, also COM. Um mir (und jetzt auch dir
) das Leben leichter zu machen, habe ich mir dafür ne Unit geschrieben:
ActiveDesktop.h:
//--------------------------------------------------------------------------- #ifndef ActiveDesktopH #define ActiveDesktopH //--------------------------------------------------------------------------- /* ACHTUNG: Diese Unit benutzt COM. Um COM zu initialisieren, rufen Sie CoInitialize(NULL); auf, und um COM zu beenden, rufen Sie CoUninitialize(); auf. */ //--------------------------------------------------------------------------- #include <windows.h> #include <wininet.h> #include <shlobj.h> //--------------------------------------------------------------------------- // Return values for IActiveDesktop functions #define AD_SUCCESS 0 #define AD_ERR_APPLYCHANGES 1 #define AD_ERR_SETSTYLE 2 #define AD_ERR_CREATECOMOBJ 3 #define AD_ERR_SETWALLPAPER 4 #define AD_ERR_GETWALLPAPER 5 #define AD_ERR_SETITEMOPTS 6 #define AD_ERR_GETITEMOPTS 7 //--------------------------------------------------------------------------- UINT ExtractFileExt(LPTSTR lpszFileName, LPTSTR lpszBuf, UINT uiBufSize); BOOL IsBitmapFile(LPTSTR lpszFileName); BOOL IsADEnabled(IActiveDesktop* pAD); DWORD EnableActiveDesktop(IActiveDesktop* pAD, BOOL enable); DWORD RefreshDesktop(); DWORD GetWallpaper(LPTSTR lpszBuf, UINT uiBufSize); DWORD SetWallpaper(LPTSTR lpszFileName, DWORD dwStyle); //--------------------------------------------------------------------------- #endif
ActiveDesktop.cpp:
//--------------------------------------------------------------------------- #include "ActiveDesktop.h" //--------------------------------------------------------------------------- /* ACHTUNG: Diese Unit benutzt COM. Um COM zu initialisieren, rufen Sie CoInitialize(NULL); auf, und um COM zu beenden, rufen Sie CoUninitialize(); auf. */ //--------------------------------------------------------------------------- UINT ExtractFileExt(LPTSTR lpszFileName, LPTSTR lpszBuf, UINT uiBufSize) { LPTSTR lpszPtr = &lpszFileName[lstrlen(lpszFileName) - 1]; while(*lpszPtr != '.' && *lpszPtr != ':' && *lpszPtr != '\\' && lpszPtr != lpszFileName) lpszPtr--; if(*lpszPtr == '.' && lpszPtr != lpszFileName) { if(uiBufSize != 0) { LPTSTR lpszExt = (LPTSTR)malloc( lstrlen(lpszPtr) + 1 ); strcpy(lpszExt, lpszPtr); lpszExt[uiBufSize - 1] = '\0'; lstrcpy(lpszBuf, lpszExt); free(lpszExt); } return lstrlen(lpszPtr) + 1; } else { if(uiBufSize != 0) lpszBuf[0] = '\0'; return 0; } } //--------------------------------------------------------------------------- BOOL IsBitmapFile(LPTSTR lpszFileName) { UINT uiBufSize = ExtractFileExt(lpszFileName, NULL, 0); LPTSTR lpszExt = (LPTSTR)malloc(uiBufSize); ExtractFileExt(lpszFileName, lpszExt, uiBufSize); CharUpperBuff(lpszExt, lstrlen(lpszExt)); return !lstrcmp(lpszExt, ".BMP"); } //--------------------------------------------------------------------------- BOOL IsADEnabled(IActiveDesktop* pAD) { COMPONENTSOPT co; HRESULT hr; co.dwSize = sizeof(COMPONENTSOPT); hr = pAD->GetDesktopItemOptions(&co, 0); if(hr == S_OK) return co.fActiveDesktop; return FALSE; } //--------------------------------------------------------------------------- DWORD EnableActiveDesktop(IActiveDesktop* pAD, BOOL enable) { COMPONENTSOPT co; HRESULT hr; co.dwSize = sizeof(COMPONENTSOPT); hr = pAD->GetDesktopItemOptions(&co, 0); if(hr == S_OK) { if(co.fActiveDesktop == enable) return AD_SUCCESS; else { co.fActiveDesktop = enable; hr = pAD->SetDesktopItemOptions(&co, 0); if(hr != S_OK) return AD_ERR_SETITEMOPTS; } } else return AD_ERR_GETITEMOPTS; return AD_SUCCESS; } //--------------------------------------------------------------------------- DWORD RefreshDesktop() { HRESULT hr; IActiveDesktop* pIAD; DWORD result = AD_SUCCESS; // Create a COM object, using the Active Desktop coclass provided by the shell. // The 4th parameter tells COM what interface we want (IActiveDesktop). hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void**)&pIAD); if( SUCCEEDED(hr) ) { hr = pIAD->ApplyChanges(AD_APPLY_ALL | AD_APPLY_FORCE); if(hr != S_OK) result = AD_ERR_APPLYCHANGES; pIAD->Release(); } else result = AD_ERR_CREATECOMOBJ; return result; } //--------------------------------------------------------------------------- DWORD GetWallpaper(LPTSTR lpszBuf, UINT uiBufSize) { HRESULT hr; IActiveDesktop* pIAD; DWORD result = AD_SUCCESS; // Create a COM object, using the Active Desktop coclass provided by the shell. // The 4th parameter tells COM what interface we want (IActiveDesktop). hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void**)&pIAD); if( SUCCEEDED(hr) ) { WCHAR wPath[sizeof(WORD) * MAX_PATH]; hr = pIAD->GetWallpaper(wPath, sizeof(WORD) * uiBufSize, 0); if(hr == S_OK) { WideCharToMultiByte(CP_ACP, 0, wPath, -1, lpszBuf, uiBufSize, NULL, NULL); } else result = AD_ERR_GETWALLPAPER; pIAD->Release(); } else result = AD_ERR_CREATECOMOBJ; return result; } //--------------------------------------------------------------------------- // dwStyle can be one of the following: // WPSTYLE_CENTER, WPSTYLE_TILE or WPSTYLE_STRETCH DWORD SetWallpaper(LPTSTR lpszFileName, DWORD dwStyle) { HRESULT hr; IActiveDesktop* pIAD; DWORD result = AD_SUCCESS; BOOL bIsBmp; // Create a COM object, using the Active Desktop coclass provided by the shell. // The 4th parameter tells COM what interface we want (IActiveDesktop). hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void**)&pIAD); if( SUCCEEDED(hr) ) { WCHAR wPath[sizeof(WORD) * MAX_PATH]; UINT wCharLen; bIsBmp = IsBitmapFile(lpszFileName); EnableActiveDesktop(pIAD, !bIsBmp); // Convert character string to wide char wCharLen = MultiByteToWideChar(CP_ACP, 0, lpszFileName, -1, NULL, 0); MultiByteToWideChar(CP_ACP, 0, lpszFileName, -1, wPath, wCharLen + 1); hr = pIAD->SetWallpaper(wPath, 0); if(hr == S_OK) { WALLPAPEROPT wpo; wpo.dwSize = sizeof(WALLPAPEROPT); wpo.dwStyle = dwStyle; hr = pIAD->SetWallpaperOptions(&wpo, 0); if(hr == S_OK) { hr = pIAD->ApplyChanges(AD_APPLY_ALL | AD_APPLY_FORCE); if(hr != S_OK) result = AD_ERR_APPLYCHANGES; } else result = AD_ERR_SETSTYLE; } else result = AD_ERR_SETWALLPAPER; pIAD->Release(); } else result = AD_ERR_CREATECOMOBJ; return result; } //---------------------------------------------------------------------------
Du wirst wohl nur die Funktion SetWallpaper() gebrauchen. Viel Spaß.
-
So genau wollte ich es gar nicht wissen
Wollte auch noch ein bischen Programmierspaß.
Aber trotzdem vielen Dank WebFritzi
-
Ja, soll ich's etwa wieder rausnehmen?
-
Nein!!!
-