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

 

 

 

 

 

 

 

 

 

發佈了60 篇原創文章 · 獲贊 2 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章