There are many ways to set up your wallpaper, you can call SystemParametersInfo, modify the registry, and you can do so through the IActiveDesktop interface provided by the Windows shell. The IActiveDesktop interface is a COM interface that needs to be used in a way that calls COM components. This explains how COM is used by applying the IActiveDesktop interface. Strangely, the wininet.h must be included, and the order cannot be wrong, see the following order:#include <afxwin.h> //MFC core components and standard components
#include <wininet.h> #include <afxext.h> //MFC ExtendedOtherwise, you will be prompted not to find the definition of iactivedesktop. 1. Call
CoInitializeInitializes the COM library on the current thread.:: CoInitialize(NULL);2. Use
CoCreateInstancefunction, create an instance of the Activedesktop object, get the IActiveDesktop interface pointer of the Activedesktop object, and add one to the use count of the successful object. iactivedesktop *pactivedesktop=NULL; //create an instance of the Active Desktop HRESULT HR = ::CoCreateInstance(
clsid_activedesktop, NULL, clsctx_inproc_server, iid_iactivedesktop, (void*) &pactivedesktop);
if (FAILED(hr)) { printf("Create activedesktop failure:0x%08x\n",hr); return -1; }2.1 Call the SetWallpaper method to set the desktop wallpaper. uses_conversion; hr=pactivedesktop-setwallpaper( a2w("F:\\My Documents\\My pictures\\xinsrc_55203051817196711110236.jpg "), 0); if (FAILED(hr)) { printf("SetWallpaper failure:0x%08x\n",hr); return -1; }2.2 Call the ApplyChanges method to apply the current settings. hr=pactivedesktop-applychanges(ad_apply _all); if (FAILED(hr)) { printf("ApplyChanges failure:0x%08x\n",hr); return -1; }3. Call the release method to release the interface, when the object's usage count is reduced by one.Pactivedesktop -Release();4. Call CoUninitialize to release the COM library on the current thread.:: CoUninitialize();
Com.2 using an instance to illustrate the use of COM (set wallpaper with IActiveDesktop)