void SetDesktopWallPaper(LPWSTR wallpaper) { CoInitialize(NULL); IActiveDesktop *pDesk; if( S_OK != CoCreateInstance ( CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void**) &pDesk ) ) { pDesk->Release(); return ; } pDesk->SetWallpaper(wallpaper,0); WALLPAPEROPT wallpaperopt; wallpaperopt.dwSize = sizeof(WALLPAPEROPT); wallpaperopt.dwStyle = WPSTYLE_CENTER; pDesk->SetWallpaperOptions(&wallpaperopt,0); pDesk->ApplyChanges(AD_APPLY_ALL); pDesk->Release(); CoFreeUnusedLibraries(); CoUninitialize(); }
int _tmain(int argc, _TCHAR* argv[]) { int AutoStartOption = 0; IActiveDesktop *pActiveDesktop; ::CoInitialize( 0 ); HRESULT hr = CoCreateInstance( CLSID_ActiveDesktop, 0, CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void**) &pActiveDesktop ); WALLPAPEROPT wopt; wopt.dwSize = sizeof(wopt); wopt.dwStyle = WPSTYLE_MAX; pActiveDesktop->SetWallpaperOptions(&wopt, 0); pActiveDesktop->SetWallpaper( argv[1], 0 ); pActiveDesktop->ApplyChanges( AD_APPLY_ALL ); pActiveDesktop->Release(); ::CoUninitialize(); return 0; }
// Cmyadsk 成员函数 BOOL Cmyadsk::SetWallPaper(LPWSTR strPicFile, DWORD dwStyle) { HRESULT hr; IActiveDesktop* pIAD; //创建接口的实例 hr = CoCreateInstance ( CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void**) &pIAD ); if(!SUCCEEDED(hr)) return FALSE; //将文件名改为宽字符串,这是IActiveDesktop::SetWallpaper的要求 //WCHAR wszWallpaper [MAX_PATH]; //LPTSTR lpStr = strPicFile.GetBuffer(strPicFile.GetLength() ); //MultiByteToWideChar(CP_ACP, 0, lpStr, -1, wszWallpaper, MAX_PATH); //strPicFile.ReleaseBuffer(); //设置墙纸 hr = pIAD->SetWallpaper(strPicFile, 0); if(!SUCCEEDED(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窗口内 //LPWSTR aa; //hr = pIAD->GetWallpaper(aa, MAX_PATH, 0); //CString strFile = aa; //TRACE(strFile); //如果不用位图的话,这里有你意想不到的发现 //释放接口的实例 pIAD->Release(); CoFreeUnusedLibraries(); CoUninitialize(); return TRUE; }
int wallpaper_set(LPCTSTR path, int style) { HRESULT hr; IActiveDesktop* pIAD; CoInitialize(NULL); hr = CoCreateInstance ( CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void**) &pIAD ); if(!SUCCEEDED(hr)) return FALSE; #ifndef UNICODE WCHAR wszWallpaper [2*MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, path, -1, wszWallpaper, MAX_PATH); #else const WCHAR* wszWallpaper = path; #endif hr = pIAD->SetWallpaper(wszWallpaper, 0); if(!SUCCEEDED(hr)) return FALSE; WALLPAPEROPT wpo; wpo.dwSize = sizeof(wpo); wpo.dwStyle = style; hr = pIAD->SetWallpaperOptions(&wpo, 0); if(!SUCCEEDED(hr)) return FALSE; hr = pIAD->ApplyChanges(AD_APPLY_ALL); if(!SUCCEEDED(hr)) return FALSE; pIAD->Release(); CoUninitialize(); return TRUE; }