Desktop控制第一部分 - 设置Desktop背景图


因为某些原因, 最近写了点控制桌面的程序.
往往有很多程序员, 对这些东西感兴趣, 而自己研究又需要太多时间和精力.
而本人进行了一点点粗浅的研究, 就共享给大家. 希望能得到大家的批评.

本文旨在说明如果使用VC6进行: Desktop背景图的设置

1. 桌面Wnd层
有很多文章里面都说桌面分成多少成什么的, 我这里不想对这个太多说明.

因为大家完全可以使用: Spy 这个vs提供的工具看清楚, 桌面到底是哪些wnd组成的.

如果要找桌面Wnd, 可以使用下面代码: 


void CDesktop::FindDesktopWnd()
{
    ::EnumWindows( EnumTopWndProc, (LPARAM)
this );
}

BOOL CALLBACK EnumTopWndProc(HWND hWnd, LPARAM lp)
{
    CDesktop
* p = (CDesktop*) lp;

    
if( GetWindowTextLength( hWnd ) == 0 )
        
return TRUE;

    
char str[100];
    GetWindowText( hWnd, str, 
99 );

    
if( strncmp(str,"Program Manager"100== 0 )
    {
        p
->m_hWndDesktop = hWnd;        
        
return false;
    }

    
return TRUE;
}

 

 

CDesktop 为自己建的一个类, p->m_hWndDesktop 就是找到的桌面: Program Manager

另外说明: 这部分内容为下一篇提供基础.

 

2. Desktop背景

虽然系统提供n多API但没有一个好用的, 事实上只需要设置注册表就可以了.

另外桌面有两种模式, 一种是active desktop 其实就是使用IE了.

所以相关设置都需要设置.  以至于不管什么模式桌面背景图永远是我们指定的图.


声明: CDVReg::RegString(... ) API 为设置注册表值的API


那么我们设置桌面图就可以使用如下代码:

 


BOOL CDesktop::SetBackImage(LPCTSTR lpszImage)
{
    CString strWallPaper 
= lpszImage;

    CString strSubKey 
= "";

    HKEY keyUsers 
= NULL;
    
forint i = 0; i < 24; i ++ )
    {
        DWORD dwRet 
= RegEnumKey( HKEY_USERS, 
            i,
            strSubKey.GetBuffer( 
256 ),
            
256 );
        strSubKey.ReleaseBuffer();

        
if( ERROR_SUCCESS == dwRet )
        {
            CString strCheck 
= strSubKey + "/Software/Microsoft/Internet Explorer/Desktop/General";
            
            HKEY hKeySub 
= NULL;
            
if( ERROR_SUCCESS == RegOpenKey( HKEY_USERS, strCheck, &hKeySub ) )
            {
                RegCloseKey( hKeySub );
                
break;
            }
        }
        
else
        {
            strSubKey 
= "";
            
break;
        }

        strSubKey 
= "";
    }


    
if( strSubKey.IsEmpty() )
        
return FALSE;


    
// 1. 
    CString strPath = "Control Panel/Desktop";
    CString strField 
= "Wallpaper";
    CDVReg::RegString( REG_SET_CURRENT, strPath, strField, 
&strWallPaper );

    
// 2. 
    strPath = "Software/Microsoft/Internet Explorer/Desktop/General";
    CDVReg::RegString( REG_SET_CURRENT, strPath, strField, 
&strWallPaper );

    
// 3. 
    strField = "BackupWallpaper";
    CDVReg::RegString( REG_SET_CURRENT, strPath, strField, 
&strWallPaper );


    
// 4. 
    strPath = strSubKey + "/Control Panel/Desktop";
    CDVReg::RegString( REG_SET_USERS, strPath, strField, 
&strWallPaper );

    
// 5. 
    strPath = strSubKey + "/Software/Microsoft/Internet Explorer/Desktop/General";
    CDVReg::RegString( REG_SET_USERS, strPath, strField, 
&strWallPaper );

    
// 6. 
    strPath = strSubKey + "/Software/Microsoft/Internet Explorer/Desktop/General";
    strField 
= "BackupWallpaper";
    CDVReg::RegString( REG_SET_USERS, strPath, strField, 
&strWallPaper );

    BOOL b 
= SystemParametersInfo( 0x0014
        NULL, 
        (PVOID)lpszImage, 
        SPIF_UPDATEINIFILE 
| SPIF_SENDCHANGE );

    
return TRUE;
}

 

 

3. 下一篇说明桌面创建链接等内容.

本文相关的完整的代码工程, 将在以后(下一篇或下下一篇)提供.

本文提供所有代码任何人都可以随意引用和修改.


VC技术群: 30107096

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值