// VS2005
//head file
#include <wininet.h>
#include<shlobj.h>
// define
IActiveDesktop* m_pADesktop;
///////////////////////////////////
// init com env
HRESULT hr;
CoInitialize(NULL);
m_pADesktop = NULL;
hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void**)&m_pADesktop);
if (FAILED(hr))
{
return FALSE;
}
CstringArray m_strArr; /// 图片路径
CString strPath;
for(int i=0; i<10; ++i)
{
strPath.Format(_T("%d.bmp"), i); // 0.bmp, 1.bmp and so on, for test
m_str.Add(strPath);
}
SetTimer(1, 5000, NULL); //每5s更换一次桌布
////////////////////////////////////////////////////////////
// /// key code
//// HRESULT hr;
// for test,在定时器中,间隔指定时间,设置桌布
OnTimer(UINT_PTR nIDEvent)
static int index = 0;
if (index >= m_strArr.GetCount())
{
index = 0;
return;
}
hr = m_pADesktop->SetWallpaper(m_strArr.GetAt(index), 0);
if(FAILED(hr)) return;
WALLPAPEROPT wpo;
wpo.dwSize = sizeof(wpo);
wpo.dwStyle = WPSTYLE_STRETCH;
hr = m_pADesktop->SetWallpaperOptions(&wpo, 0);
if (FAILED(hr))
{
return;
}
hr = m_pADesktop->ApplyChanges(AD_APPLY_ALL);
if (FAILED(hr))
{
return;
}
index++;
////////////////////////////////////////////
// clean
KillTimer(1);
m_pADesktop->Release();
CoUninitialize();
//////////////////////////////////////////////
评论