1. 程式人生 > >【框架-MFC】修改桌面 桌布

【框架-MFC】修改桌面 桌布

簡介

功    能:使用IActiveDesktop介面獲取、設定和重新整理桌面背景(桌布)

開發環境:  VC\VS2005\VS2008\VS2010\VS2012\VS2013

新建專案:MFC應用程式(基於對話方塊)

開發準備

程式碼準備

標頭檔案

#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:重新整理桌面