简介
功 能: 使用IActiveDesktop 接口获取、设置和刷新桌面背景(壁纸)
开发环境: VC\VS2005\VS2008\VS2010\VS2012\VS2013
新建项目:MFC应用程序(基于对话框)
源码及程序下载:http://pan.baidu.com/s/1nv2aZ8X
开发准备
代码准备
头文件
#include <shlobj.h>
#include <shlwapi.h>
shlobj.h
包含了修改桌面的Api函数(SetDesktopItemOptions、SetWallpaper、SetWallpaperOptions、ApplyChanges、GetWallpaper)、结构对象(IActiveDesktop、COMPONENTSOPT、WALLPAPEROPT)。
shlwapi.h
包含了对文件判别的Api函数(PathFileExists、)
输入参数源码
int GetWallpaperStyle()
{
int reint = GetPrivateProfileInt(_T("Param"),_T("WallpaperStyle"),-1,CString(g_szHomeDir)+_T("\\SQWallpaper.ini"));
if (reint < 0 ||reint > 2)
{
return 2;
}
return reint;
}
CString GetWallpaper()
{
CString refile ;
WCHAR re[2048];
GetPrivateProfileString(_T("Param"),_T("Wallpaper"),_T(""),re,2048 ,CString(g_szHomeDir)+_T("\\SQWallpaper.ini"));
refile = CString(re);
if (!PathFileExists(refile))
{
if (PathFileExists(CString(g_szHomeDir)+_T("\\")+refile))
{
return CString(g_szHomeDir)+_T("\\")+refile;
}
else
{
return CString(g_szHomeDir) + _T("\\SCDesktop.bmp");
}
}
return CString(g_szHomeDir)+_T("\\")+refile;
}
GetWallpaperStyle
获取壁纸风格
GetWallpaper
获取壁纸的全路径
输入文件
文件名
SQWallpaper.ini
内容
[Param]
WallpaperStyle=2
Wallpaper=Desktop.bmp
WallpaperStyle:壁纸风格(0居中、1平铺、2拉伸)
Wallpaper:自定义的文件名,需要与程序在同一目录下(支持jpg、bmp、gif等格式)
设置壁纸
BOOL SetWallpaper(CString &strPicFile, DWORD dwStyle)
{
OutputDebugString(strPicFile);
HRESULT hr;
IActiveDesktop* pIAD;
//创建接口的实例
hr = CoCreateInstance ( CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,
IID_IActiveDesktop, (void**) &pIAD );
if(!SUCCEEDED(hr))
{
OutputDebugString(_T("CoCreateInstance failed"));
return FALSE;
}
hr = pIAD->SetWallpaper(strPicFile, 0);
if(!SUCCEEDED(hr))
{
OutputDebugString(_T("SetWallpaper failed"));
return FALSE;
}
WALLPAPEROPT wpo;
wpo.dwSize = sizeof(wpo);
wpo.dwStyle = dwStyle;
hr = pIAD->SetWallpaperOptions(&wpo, 0);
if(!SUCCEEDED(hr))
{
OutputDebugString(_T("SetWallpaperOptions failed"));
return FALSE;
}
hr = pIAD->ApplyChanges(AD_APPLY_ALL);
if(!SUCCEEDED(hr))
{
OutputDebugString(_T("ApplyChanges failed"));
return FALSE;
}
WCHAR wszWallpaper [MAX_PATH];
CString strFile ;
hr = pIAD->GetWallpaper(wszWallpaper, MAX_PATH, 0);
if(!SUCCEEDED(hr))
{
OutputDebugString(_T("GetWallpaper failed"));
}
strFile = wszWallpaper;
TRACE(strFile);
pIAD->Release();
return TRUE;
}
CoCreateInstance:创建接口实例
SetWallpaper:设置桌面壁纸
SetWallpaperOptions:设置壁纸风格
ApplyChanges:应用壁纸
GetWallpaper:获取当前使用壁纸的名称
刷新桌面
BOOL EnableActiveDesktop(BOOL bEnable)
{
HRESULT hr;
IActiveDesktop* pIAD;
hr = CoCreateInstance ( CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,
IID_IActiveDesktop, (void**) &pIAD );
if(!SUCCEEDED(hr)) return FALSE;
COMPONENTSOPT comp;
comp.dwSize = sizeof(comp);
comp.fEnableComponents = bEnable;
comp.fActiveDesktop = bEnable;
hr = pIAD->SetDesktopItemOptions(&comp, 0);
if(!SUCCEEDED(hr)) return FALSE;
pIAD->Release();
return TRUE;
}
CoCreateInstance:创建接口实例
SetDesktopItemOptions:更新桌面
调用流程
CSQWallpaperApp::CSQWallpaperApp()
{
GetModuleFileName(NULL, g_szHomeDir, _MAX_PATH);
PTSTR pszPos = _tcsrchr( g_szHomeDir, _T('\\'));
pszPos[0] = 0;
}
CSQWallpaperApp theApp;
TCHAR g_szHomeDir[MAX_PATH+1];
BOOL CSQWallpaperApp::InitInstance()
{
CWinApp::InitInstance();
AfxOleInit();
if(SetWallpaper(GetWallpaper(), GetWallpaperStyle()) == FALSE)
{
AfxMessageBox(_T("Replacing failed wallpaper!"));
}
if (EnableActiveDesktop(TRUE)==TRUE)
{
OutputDebugString(_T("Enable Active Desktop Success"));
}
return FALSE;
}
AfxOleInit : 初始化接口
GetWallpaper:获取壁纸全路径
GetWallpaperStyle:获取壁纸风格
SetWallpaper:更换壁纸
EnableActiveDesktop:刷新桌面