Chinaunix首页 | 论坛 | 博客
  • 博客访问: 134343
  • 博文数量: 41
  • 博客积分: 2501
  • 博客等级: 少校
  • 技术积分: 362
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-07 17:54
文章分类
文章存档

2011年(2)

2010年(14)

2009年(11)

2008年(14)

我的朋友

分类: C/C++

2009-03-26 17:14:42

网上很难找到一个完整的使用IActiveDesktop更改桌面图片的确实可行的例子,现笔者收集多方资料整理如下,该程序在VC6.0上运行OK。

Step1.

在需要使用IActiveDesktop程序所在的C++文档里包含头文件#include

Step2.

在StdAfx.h文件中增加#include

Note:可在VC的"FileView"标签页中的Header Files文件夹下双击打开StdAfx.h文件,或者进入该工程所在目录里打开StdAfx.h文件。

#define VC_EXTRALEAN  // Exclude rarely-used stuff from Windows headers

#include          // MFC core and standard components
#include        //这里是增加的正确位置
#include          // MFC extensions

#include         // MFC Automation classes
#include   // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include    // MFC support for Windows Common Controls
//#include      有些资料说要加此头文件,但笔者在VC6.0下测试不需要
#endif // _AFX_NO_AFXCMN_SUPPORT

Step3.

下面是实际测试通过的函数例子(两个函数都测试通过)。

函数一:

//strPicFile是图像文件名,支持BMP JPEG GIF等格式
//dwStyle是墙纸的样式
//WPSTYLE_CENTER 居中 0
//WPSTYLE_TILE 平铺 1
//WPSTYLE_STRETCH 拉伸 2
//WPSTYLE_MAX 3
//返回值是TRUE时墙纸设置成功,返回FALSE时失败

bool CChgWpDlg::SetMyWallpaper(CString &strPicFile, DWORD dwStyle)
{
  HRESULT hr;
  IActiveDesktop* pIAD;
  //Applications must initialize the COM library before they can call COM library functions
  CoInitialize(NULL); 
  //创建接口的实例
  hr = CoCreateInstance ( CLSID_ActiveDesktop,  NULL, CLSCTX_INPROC_SERVER,      
              IID_IActiveDesktop, (void**) &pIAD );
  if(!SUCCEEDED(hr)) return FALSE;
 
//将文件名改为宽字符串,这是IActiveDesktop::SetWallpaper的要求
  WCHAR   wszWallpaper [MAX_PATH];
  LPTSTR lpStr = strPicFile.GetBuffer(strPicFile.GetLength() );
  MultiByteToWideChar(CP_ACP, 0, lpStr, -1, wszWallpaper, MAX_PATH);
  strPicFile.ReleaseBuffer();
  //设置墙纸
  hr = pIAD->SetWallpaper(wszWallpaper, 0);
  if(!SUCCEEDED(hr)) return FALSE;
  //设置墙纸的样式
  WALLPAPEROPT wpo;
  wpo.dwSize = sizeof(wpo);
  wpo.dwStyle = dwStyle;
  hr = pIAD->SetWallpaperOptions(&wpo, 0);
  if(!SUCCEEDED(hr)) return FALSE;
  //应用墙纸的设置
  hr = pIAD->ApplyChanges(AD_APPLY_ALL);
  if(!SUCCEEDED(hr)) return FALSE;
  //读取墙纸的文件名并打印在debug窗口内
  hr = pIAD->GetWallpaper(wszWallpaper, MAX_PATH, 0);
  CString strFile = wszWallpaper;
  TRACE(strFile);
//如果不用位图的话,这里有你意想不到的发现
  //释放接口的实例
  pIAD->Release();
  CoUninitialize();
  return TRUE;
}

 

函数二:

bool CChgWpDlg::SetMyWallpaper(CString &strPicFile, DWORD dwStyle)
{
IActiveDesktop   *pActiveDesktop;  
   
  HRESULT   hr;  
  CoInitialize(NULL);  
   
  hr   =   CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,
   IID_IActiveDesktop, (void**)&pActiveDesktop);  
   
  COMPONENTSOPT   comps;  
  comps.dwSize   =   sizeof   comps;  
  comps.fEnableComponents   =   TRUE;  
  comps.fActiveDesktop   =   TRUE;  
   
  pActiveDesktop->SetDesktopItemOptions(&comps,0);   
     //下面是使用固定path的文件的方法
  if   (FAILED(pActiveDesktop->SetWallpaper(L"C:\\cy002.jpg",0)))  
  return false;  
   
  pActiveDesktop->ApplyChanges(AD_APPLY_ALL|AD_APPLY_FORCE);  
   
  pActiveDesktop->Release();  
  CoUninitialize();  

 return true;
}

阅读(1149) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~