【框架-MFC】修改桌面 壁纸

简介

功    能: 使用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:刷新桌面

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值