Title How to dynamically set wallpaper Zwvista in the program Keyword wallpaper IactiveDesktop
Everyone knows that the Win32 API settings Windows desktop wallpaper is SystemParametersInfo, and the wallpaper can be set with the SPI_SETDESKWALLPAPER parameter:
:: SystemParametersInfo (SPI_SETDESKWALLPAPER, 0, "C: //abitmap.bmp", spif_sendwiniIfile | spif_updateinifile;
However, the problem is not solved because this Win32 API has a large limit 1. Unable to set pictures of other non-alignment formats such as JPEG, GIF to wallpaper. 2. Unable to adjust the style of wallpaper, that is, the wallpaper cannot be set to hit, tile or stretch. (Of course we can do this by programming the value of the registry hkey_current_user / control panel / desktop)
Since it is easy to manually set in the Property dialog box, we have reason to believe that the Windows Shell API must provide the corresponding high-level functions, interfaces, or classes to complete the settings of the wallpaper. In fact, the interface between this task in the Windows Shell API: IACTIVEDESKTOP.
The following is the function I have completed // strpicfile is the image file name, supports the format of BMP JPEG GIF // DWStyle is the style of wallpaper // wpStyle_center home 0 // WPStyle_tile Tile 1 // WPStyle_stretch stretch 2 // WPSTYLE_MAX 3 // the return value is TRUE if the wallpaper setting success, failure BOOL SetWallpaper (CString & strPicFile, DWORD dwStyle) {HRESULT hr return FALSE; IActiveDesktop * pIAD; // Create an instance of an interface hr = CoCreateInstance (CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER, IID_IAACTIVEDESKTOP, (Void **) & piad; if (! Successded (hr)) return false; // change the file name to a wide string, this is the requirements of IactiveDesktop :: SetWallpaper, Wchar WSzWallpaper [Max_Path]; lptstr lpstr = strpicfile .GetBuffer (STRPICFILE.GETLENGTH ()); MultibyTetowideChar (CP_ACP, 0, LPSTR, -1, WSZWALLPAPER, MAX_PATH); strpicfile.releaseBuffer (); // Set wallpaper HR = Piad-> setwallpaper (WSZWALLPAPER, 0); IF ! Succeededed (hr)) Return false; // Set the style of wallpaper WallPaperopt WallPaPaperopt WPO; WPO.DWSIZE = SizeOf (WPO); WPO.DWSTYLE = DWStyle; hr = piad-> setwallpaperOptions (& WPO, 0); if (! Successded) HR)) Return False; // Application Wallpaper Set HR = PIAD-> ApplyChanges (Ad_Apply_all); if (! succeededed (hr)) RETU Rn false; // read the file name of the wallpaper and printed in the Debug window HR = Piad-> getWallpaper (WSZWALLPAPER, MAX_PATH, CSTRING STRFILE = WSZWALLPAPER; trace (strfile); // If you don't need a bitmap, here There is an instance of the discovery // release interface. PIAD-> Release (); return true;} When using this function in the MFC program, pay attention to the following three points.
1. Plus the header file that declares IACTIVEDESKTOP in the file where the function is located
#include
2. Insert #include
#include
3. Since this function references the COM component, you must insert the following statement in c ** app :: oninitInstance (), initialize the COM component.
Afxoleinit ();
supplement:
The IACTIVEDESKTOP interface can only be applied when IE4.0 and the above version is installed, and only the setwallpaper function can be applied if the Active Desktop is turned on, the following is the function that enables or close Active Desktop: // Benable is True is enabled active desktop, closed // the return value is TRUE if enable or disable the active desktop is successful FALSE, failure BOOL EnableActiveDesktop (BOOL bEnable) {HRESULT hr return FALSE; IActiveDesktop * pIAD; // Create an instance of an interface hr = CoCreateInstance (CLSID_ActiveDesktop , NULL, CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void **) & pIAD); if) return FALSE (SUCCEEDED (hr!); COMPONENTSOPT comp; comp.dwSize = sizeof (comp); comp.fEnableComponents = bEnable; // enable or disable active Desktop Comp.FactiveDesktop = Benable; HR = PIAD-> SetDesktopItemOptions (& Comp, 0); if (! successded (hr)) Return False; // Release the instance of the interface PIAD-> Release; Return True;}