天天看點

vc程式設計更改桌面背景

網上很難找到一個完整的使用IActiveDesktop更改桌面圖檔的确實可行的例子,現筆者收集多方資料整理如下,該程式在VC6.0上運作OK。

Step1.

在需要使用IActiveDesktop程式所在的C++文檔裡包含頭檔案#include<shlobj.h>

Step2.

在StdAfx.h檔案中增加#include <wininet.h>

Note:可在VC的"FileView"标簽頁中的Header Files檔案夾下輕按兩下打開StdAfx.h檔案,或者進入該工程所在目錄裡打開StdAfx.h檔案。

#define VC_EXTRALEAN  // Exclude rarely-used stuff from Windows headers

#include <afxwin.h>         // MFC core and standard components

#include <wininet.h>       //這裡是增加的正确位置

#include <afxext.h>         // MFC extensions

#include <afxdisp.h>        // MFC Automation classes

#include <afxdtctl.h>  // MFC support for Internet Explorer 4 Common Controls

#ifndef _AFX_NO_AFXCMN_SUPPORT

#include <afxcmn.h>   // MFC support for Windows Common Controls

//#include   <comdef.h>   有些資料說要加此頭檔案,但筆者在VC6.0下測試不需要

#endif // _AFX_NO_AFXCMN_SUPPORT

Step3.

下面是實際測試通過的函數例子(兩個函數都測試通過)。

函數一:

//strPicFile是圖像檔案名,支援BMP JPEG GIF等格式

//dwStyle是牆紙的樣式

//WPSTYLE_CENTER 居中 0

//WPSTYLE_TILE 平鋪 1

//WPSTYLE_STRETCH 拉伸 2

//WPSTYLE_MAX 3

//傳回值是TRUE時牆紙設定成功,傳回FALSE時失敗

bool CChgWpDlg::SetMyWallpaper(CString &strPicFile, DWORD dwStyle)

{

  HRESULT hr;

  IActiveDesktop* pIAD;

  //Applications must initialize the COM library before they can call COM library functions

  CoInitialize(NULL); 

  //建立接口的執行個體

  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();

  CoUninitialize();

  return TRUE;

}

函數二:

bool CChgWpDlg::SetMyWallpaper(CString &strPicFile, DWORD dwStyle)

{

IActiveDesktop   *pActiveDesktop;  

  HRESULT   hr;  

  CoInitialize(NULL);  

  hr   =   CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,

   IID_IActiveDesktop, (void**)&pActiveDesktop);  

  COMPONENTSOPT   comps;  

  comps.dwSize   =   sizeof   comps;  

  comps.fEnableComponents   =   TRUE;  

  comps.fActiveDesktop   =   TRUE;  

  pActiveDesktop->SetDesktopItemOptions(&comps,0);   

     //下面是使用固定path的檔案的方法

  if   (FAILED(pActiveDesktop->SetWallpaper(L"C://cy002.jpg",0)))  

  return false;  

  pActiveDesktop->ApplyChanges(AD_APPLY_ALL|AD_APPLY_FORCE);  

  pActiveDesktop->Release();  

  CoUninitialize();  

 return true;

}