Set Desktop Wallpaper using non-bitmap in Win32 C++

One day,I want to set the desktop wallpaper by the application which
I wrote using Win32 in c++.First,I used the api
SystemParametersInfo(SPI_SETDESKWALLPAPER,TRUE,(LPVOID)"E://my_docs//test.jpg",SPIF_SENDWININICHANGE| SPIF_UPDATEINIFILE);
But it can not work.So I checked with MSDN:

SPI_SETDESKWALLPAPER
Sets the desktop wallpaper. The value of the pvParam parameter determines the new wallpaper. To specify a wallpaper bitmap, set pvParam to point to a null-terminated string containing the full path to the bitmap file. Setting pvParam to "" removes the wallpaper. Setting pvParam to SETWALLPAPER_DEFAULT or NULL reverts to the default wallpaper.
Starting with Windows Vista, pvParam can specify a .jpg file.

So I tried the Google,it told me as below and that can be worked normally.

  139 BOOL SetWPByActiveDesktop(LPCTSTR lpszPath)

  140 {

  141     if ( lpszPath == NULL )

  142         return FALSE ;

  143 #if 1

  144     CoInitialize(NULL);

  145 

  146     HRESULT hr;

  147     IActiveDesktop* pIAD;

  148     hr = CoCreateInstance( CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,IID_IActiveDesktop, (void**)&pIAD);

  149 

  150     if ( !SUCCEEDED(hr) )

  151         return FALSE;

  152 

  153     //set activedesktop options,turn on the AD.

  154     COMPONENTSOPT co = {0};

  155     co.dwSize = sizeof(COMPONENTSOPT);

  156     co.fEnableComponents = TRUE;

  157     co.fActiveDesktop = TRUE;

  158     hr = pIAD->SetDesktopItemOptions(&co, 0);

  159     if ( !SUCCEEDED(hr) )

  160         return FALSE;

  161 

  162     //pay attention the parameter passed to IActiveDesktop.if app is unicode,ignore

  163     WCHAR wszPath[MAX_PATH]={0};

  164     //LPTSTR lpStr = strPath.GetBuffer(strPath.GetLength());

  165     MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, wszPath, MAX_PATH);

  166     //strPath.ReleaseBuffer();

  167 

  168     //set the wallpaper

  169     hr = pIAD->SetWallpaper(wszPath, 0);

  170     if ( !SUCCEEDED(hr) )

  171         return FALSE;

  172 

  173     // Set wallpaper style such as title,center,stretch

  174     WALLPAPEROPT wp = {0};

  175     wp.dwSize = sizeof(WALLPAPEROPT);

  176     wp.dwStyle |= WPSTYLE_CENTER;

  177     hr = pIAD->SetWallpaperOptions(&wp, 0);

  178     if ( !SUCCEEDED(hr) )

  179         return FALSE;

  180 

  181     // 应用改变

  182     hr = pIAD->ApplyChanges(AD_APPLY_ALL);

  183     if ( !SUCCEEDED(hr) )

  184         return FALSE;

  185 

  186     // RELEASE

  187     pIAD->Release();

  188 

  189     CoUninitialize();

  190 #endif

  191 

  192     return TRUE; 

  193 }

Testing passed in VC++6 SP6-win32 application.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值