Windows Shell API提供了接口IActiveDesktop来完成墙纸的设置。
//IActiveDesktop 接口方法表 (详情参见MSDN)
AddDesktopItem
AddDesktopItemWithUI AddUrl ApplyChange GenerateDesktopItemHtml GetDesktopItem GetDesktopItemByID GetDesktopItemBySource GetDesktopItemCount GetDesktopItemOptions GetPattern GetWallpaper GetWallpaperOptions ModifyDesktopItem RemoveDesktopItem SetDesktopItemOptionsSetPattern SetWallpaper SetWallpaperOptions C++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | BOOL SetWallpaper(CString &strPicFile, DWORD dwStyle) { HRESULT hr = S_OK; IActiveDesktop* pIAD = NULL ; //创建接口的实例 hr = CoCreateInstance(CLSID_ActiveDesktop, NULL , CLSCTX_INPROC_SERVER, IID_IActiveDesktop, ( void **) &pIAD ); if (FAILED(hr)) { return FALSE; } //将文件名改为宽字符串,这是IActiveDesktop::SetWallpaper的要求 WCHAR wszWallpaper [MAX_PATH] = { 0 }; LPTSTR lpStr = strPicFile.GetBuffer(strPicFile.GetLength() ); MultiByteToWideChar(CP_ACP, 0 , lpStr, - 1 , wszWallpaper, MAX_PATH); strPicFile.ReleaseBuffer(); //设置墙纸 hr = pIAD->SetWallpaper(wszWallpaper, 0 ); if (FAILED(hr)) { return FALSE; } //设置墙纸的样式 WALLPAPEROPT wpo; wpo.dwSize = sizeof (wpo); wpo.dwStyle = dwStyle; hr = pIAD->SetWallpaperOptions(&wpo, 0 ); if (!SUCCEEDED(hr)) { return FALSE; } //应用墙纸的设置 hr = pIAD->ApplyChanges(AD_APPLY_ALL); if (!SUCCEEDED(hr)) { return FALSE; } //读取墙纸的文件名并打印在debug窗口内 hr = pIAD->GetWallpaper(wszWallpaper, MAX_PATH, 0 ); CString strFile = wszWallpaper; TRACE(strFile); //释放接口的实例 pIAD->Release(); return TRUE; } BOOL EnableActiveDesktop(BOOL bEnable) { HRESULT hr = S_OK; IActiveDesktop* pIAD = NULL ; //创建接口的实例 hr = CoCreateInstance ( CLSID_ActiveDesktop, NULL , CLSCTX_INPROC_SERVER, IID_IActiveDesktop, ( void **) &pIAD ); if (!SUCCEEDED(hr)) { return FALSE; } //启用或关闭Active desktop COMPONENTSOPT comp; comp.dwSize = sizeof (comp); comp.fEnableComponents = bEnable; comp.fActiveDesktop = bEnable; hr = pIAD->SetDesktopItemOptions(&comp, 0 ); if (!SUCCEEDED(hr)) { return FALSE; } //释放接口的实例 pIAD->Release; return TRUE; } |
Demo下载: