//strPicFile是图像文件名,支持BMP JPEG GIF等格式
//dwStyle是墙纸的样式
//WPSTYLE_CENTER 居中 0
//WPSTYLE_TILE 平铺 1
//WPSTYLE_STRETCH 拉伸 2
//WPSTYLE_MAX 3
//返回值是TRUE时墙纸设置成功,返回FALSE时失败
需要包括的头文件方法如下所示:
一、在 stdafx.h 文件中添加如下头文件:
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include "wininet.h " //you should Add
#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
#endif // _AFX_NO_AFXCMN_SUPPORT
#include <afxconv.h> //you should Add
二、在应用程序BOOL CBZApp::InitInstance() 中添加
AfxOleInit();
三、在使用IActiveDesktop的文件中添加:
#include "Shlobj.h"
示例代码如下:
BOOL CBZDlg::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();
return TRUE;
}
//dwStyle是墙纸的样式
//WPSTYLE_CENTER 居中 0
//WPSTYLE_TILE 平铺 1
//WPSTYLE_STRETCH 拉伸 2
//WPSTYLE_MAX 3
//返回值是TRUE时墙纸设置成功,返回FALSE时失败
需要包括的头文件方法如下所示:
一、在 stdafx.h 文件中添加如下头文件:
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include "wininet.h " //you should Add
#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
#endif // _AFX_NO_AFXCMN_SUPPORT
#include <afxconv.h> //you should Add
二、在应用程序BOOL CBZApp::InitInstance() 中添加
AfxOleInit();
三、在使用IActiveDesktop的文件中添加:
#include "Shlobj.h"
示例代码如下:
BOOL CBZDlg::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();
return TRUE;
}