"活动桌面"修改桌面背景

转载自:http://blog.csdn.net/woshinia/article/details/6832455


内容如下:
网上很难找到一个完整的使用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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值