vc变成修改/设置桌面背景(1)

来源:互联网 发布:内网怎么访问阿里云 编辑:程序博客网 时间:2024/06/06 14:11

这是转的另一个博客的,也是csdn的博客,但是没有防止用户修改的功能。很多时候,公司内部制定了统一的桌面,是不希望随便修改的,过几天有空的时候一起发上来。

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

Step1.

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

Step2.

在StdAfx.h文件中增加#include <wininet.h>

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

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

#include <afxwin.h>         // MFC core and standard components
#include <wininet.h>       //这里是增加的正确位置
#include <afxext.h>         // MFC extensions

#include <afxdisp.h>        // MFC Automation classes
#include <afxdtctl.h>  // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>   // MFC support for Windows Common Controls
//#include   <comdef.h>   有些资料说要加此头文件,但笔者在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;
}

原创粉丝点击