分类: C/C++
2007-03-10 14:01:50
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, "C:\\xxx.bmp", SPIF_SENDWININICHANGE| SPIF_UPDATEINIFILE); |
#include #include #include ... |
#include |
BOOL CTest6Dlg::SetWallPaper(CString strPath) { HRESULT hr; IActiveDesktop* pIAD; hr = CoCreateInstance( CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,IID_IActiveDesktop, (void**)&pIAD); if ( !SUCCEEDED(hr) ) return FALSE; // 设置墙纸前先打开活动桌面开关 COMPONENTSOPT co = {0}; co.dwSize = sizeof(COMPONENTSOPT); co.fEnableComponents = TRUE; co.fActiveDesktop = TRUE; hr = pIAD->SetDesktopItemOptions(&co, 0); if ( !SUCCEEDED(hr) ) return FALSE; //做以下宽字符的转换是因为SetWallpaper的参数是用的LPCWSTR WCHAR wszPath[MAX_PATH]; LPTSTR lpStr = strPath.GetBuffer(strPath.GetLength()); MultiByteToWideChar(CP_ACP, 0, lpStr, -1, wszPath, MAX_PATH); strPath.ReleaseBuffer(); // 设置墙纸 hr = pIAD->SetWallpaper(wszPath, 0); if ( !SUCCEEDED(hr) ) return FALSE; // 设置墙纸风格 WALLPAPEROPT wp = {0}; wp.dwSize = sizeof(WALLPAPEROPT); wp.dwStyle |= WPSTYLE_CENTER; hr = pIAD->SetWallpaperOptions(&wp, 0); if ( !SUCCEEDED(hr) ) return FALSE; // 应用改变 hr = pIAD->ApplyChanges(AD_APPLY_ALL); if ( !SUCCEEDED(hr) ) return FALSE; // 最后释放接口,不要忘了 pIAD->Release(); } |