随意更改桌面壁纸

1.  stdafx可的#i nclude <afxwin.h>下加入

#i nclude <wininet.h> // 必须在此位置加上则IActiveDesktop将找不到定义

 

2.在定义文件中加入#i nclude <shlobj.h>  // IActiveDesktop

 

3.只有在安装了IE4.0及以上版本时才能应用IActiveDesktop接口,只有active desktop(活动桌面)在打开的情况下才能应用SetWallpaper函数,所以先定义打开或关闭活动桌面的方法如下:

BOOL EnableActiveDesktop(BOOL bEnable)

{

  // bEnable TRUE则打开,反之

  HRESULT hr;

  IActiveDesktop* pIADesk;

  //创建接口的实例

  hr = CoCreateInstance ( CLSID_ActiveDesktop,  NULL, CLSCTX_INPROC_SERVER,      

              IID_IActiveDesktop, (void**) & pIADesk );

  If ( !SUCCEEDED(hr) )

return FALSE;

  COMPONENTSOPT comp;

  comp.dwSize = sizeof(comp);

  comp.fEnableComponents = bEnable;

  //启用或关闭active desktop

  comp.fActiveDesktop = bEnable;

  hr = pIADesk ->SetDesktopItemOptions(&comp, 0);

  if ( !SUCCEEDED(hr) )

 return FALSE;

  //释放接口的实例

pIADesk ->Release;

  return TRUE;

}

 

4.当活动桌面开启后即可进行壁纸设置,代码如下:

//strPicFile是图像文件名,支持BMP JPEG GIF等格式

//dwStyle是墙纸的样式

//WPSTYLE_CENTER 居中 0

//WPSTYLE_TILE    平铺 1

//WPSTYLE_STRETCH 拉伸 2

//WPSTYLE_MAX 3

 

//返回值是TRUE时墙纸设置成功,返回FALSE时失败

BOOL SetWallpaper(CString &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(wszWallpaper, 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窗口内

       hr = pIAD->GetWallpaper(wszWallpaper, MAX_PATH, 0);

       CString strFile = wszWallpaper;

       TRACE(strFile);

       pIAD->Release();

       return TRUE;

}

你可能感兴趣的:(更改)