Change desktop wallpaper

xiaoxiao2021-04-08  4

1. STDAFX can be joined in #i nclude

#I nclude // must be added here, IACTIVEDESKTOP will not be defined

2. Add #i nClude // IactiveDesktop in the definition file

3. The IACTIVEDESKTOP interface can only be applied when installing IE4.0 and above, only Active Desktop can apply the setwallpaper function in the case, so the way to turn the active desktop is as follows:

BOOL EnableActiveDesktop (Bool Benable)

{

// Benable is turned on, reverse

HRESULT HR;

IactiveDesktop * piadesk;

// Create an instance of an interface

HR = COCREATEINSTANCE (CLSID_ACTIVEDESKTOP, NULL, CLSCTX_INPROC_SERVER,

IID_IACTIVEDESKTOP, (void **) & piadesk;

IF (! Succeeded (hr))

Return False;

Componentsopt Comp;

Comp.dwsize = sizeof (comp);

Comp.FenableComponents = BENABLE;

// Enable or close Active Desktop

Comp.FactiveDesktop = BENABLE;

HR = PIADESK -> SetDesktopItemOptions (& Comp, 0);

IF (! succeeded (hr))

Return False;

/ / Release the instance of the interface

PIADESK -> RELEASE

Return True;

}

4. Wallpaper settings can be made when the event desktop is turned on, the code is as follows:

// strpicfile is an image file name, supports formats such as 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, the wallpaper is set success, and it failed when returning FALSE.

Bool Setwallpaper (CString & Strpicfile, DWORD DWSTYLE)

{

HRESULT HR;

IactiveDesktop * piad;

// Create an instance of an interface

HR = COCREATEINSTANCE (CLSID_ACTIVEDESKTOP, NULL, CLSCTX_INPROC_SERVER,

IID_IACTIVEDESKTOP, (void **) & piad);

IF (! Succeeded (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 ());

MultibyToWideChar (CP_ACP, 0, LPSTR, -1, WSZWALLPAPER, MAX_PATH);

Strpicfile.releasebuffer ();

// Set wallpaper

HR = PIAD-> SetWallpaper (WSZWALLPAPER, 0);

IF (! succeeded (hr)) Return False; // Set the style of wallpaper

Wallpaperopt WPO;

WPO.DWSIZE = SIZEOF (WPO);

WPO.DWSTYLE = DWStyle;

HR = Piad-> SetWallPaperOptions (& WPO, 0);

IF (! succeeded (hr))

Return False;

// Application wallpaper settings

HR = PIAD-> ApplyChanges (ad_apply_all);

IF (! succeeded (hr))

Return False;

/ / Read the file name of the wallpaper and print within the Debug window

HR = Piad-> getWallpaper (Wszwallpaper, Max_Path, 0);

CString strfile = wszwallpaper;

Trace (strfile);

Piad-> Release ();

Return True;

}

转载请注明原文地址: http://www.9cbs.com/read-132850.html

New Post(0)