這個例子展示了如何繪製定製(自繪)視窗架構(包括標題、邊框等)。
一、前言 如今,支援定製皮膚功能的軟體越來越流行。這樣使用者就可以自己修改程式的外觀。甚至Windows作業系統本身做到這點了。Windows XP提供的主題(theme)技術可以修改視窗、按鈕、捲軸等的外觀。 最近,我想用MFC設計一個可以換膚的程式。在網上我沒有搜尋到任何想要的東西,所以我決定自己寫一個。這不是一個很難的問題,但是需要對Windows作業系統的繪製視窗的機制比較熟悉。 二、背景 我提供了下面的一些類: 1. CSkinWin----CSkinWin類是一個繪製定製(自繪)視窗的類。它繪製視窗上、下、左、右邊框和標題列按鈕如最大化、關閉按鈕。為了作到這一點,CSkinWin類從一個ini檔案讀入配置資訊,在ini檔案配置視窗各邊框的位元影像。需要指出的是,ini檔案的格式是從Windows Blinds(Stardock的傑作)的UIS格式拷貝過來的,因為我希望在我的程式裡支援Windows Blinds的主題。 2. CSkinButton---CSkinButton類是繪製定製(自繪)按鈕的類。它用四個位元影像分別代表正常、有焦點、按下和無效狀態。位元影像格式也是Windows Blinds格式,參數在同一個ini檔案中定義。因為一個視窗會有多個按鈕執行個體,所以我設計了CSkinButtonResource類來存放定製(自繪)按鈕的皮膚。 3. CMyBimtap 等--- 一些相關類,其中有些是來自Codeproject. 儘管我已經不記得這些作者了,但我還是要感謝它們. 三、用法 這些代碼的用法很直接。我包含了一個MFC基於視窗的工程,關鍵代碼列在下面。順便提一下,這些代碼在單文檔的工程裡也測試通過了。 //defines the following member in the dialog classCSkinButtonResourcem_btnres;//skin button resourceCSkinWinm_skinWin;//skin winBOOLm_bFirst;//first time callCObList m_wndList;//hold button instance//In OnInitDialogm_bFirst = TRUE;SetSkin( "skin//neostyle//theme.ini" );//SetSkin is a function to change skinBOOL CSkinTestDlg::SetSkin(CString file){m_skinWin.LoadSkin( file );//load skin bitmap and parametersm_btnres.LoadSkin( file );if ( m_bFirst ){//if it''s the first time call, InstallSkinm_skinWin.InstallSkin( this );//call EnumChildWindows to subclass all buttonsEnumChildWindows( m_hWnd, EnumChildProc, (LPARAM)this );m_bFirst = FALSE;}//redraw window after change skinSetWindowPos( 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |SWP_FRAMECHANGED );return TRUE;}//enum child window and take chance to subclass them#define GetWindowStyle(hwnd) ((DWORD)GetWindowLong(hwnd, GWL_STYLE))BOOL CALLBACK EnumChildProc( HWND hwnd, // handle to child windowLPARAM lParam // application-defined value){char classname[200];CSkinTestDlg *dlg = (CSkinTestDlg *)lParam;DWORD style;GetClassName( hwnd, classname, 200 );style = GetWindowStyle( hwnd );if ( strcmp( classname, "Button" ) == 0 ){style = (UINT)GetWindowLong(hwnd, GWL_STYLE) & 0xff;if ( style == BS_PUSHBUTTON || style == BS_DEFPUSHBUTTON )dlg->SubClassButton( hwnd );}return TRUE;}//subclass push buttonsBOOL CSkinTestDlg::SubClassButton( HWND hwnd ){CSkinButton * btn = new CSkinButton();CWnd* pWnd = CWnd::FromHandlePermanent(hwnd);if ( pWnd == NULL){btn->SubclassWindow( hwnd );btn->SetResource( &m_btnres );return TRUE;}return FALSE;}//free CSkinButton instancesvoid CSkinTestDlg::OnDestroy() {CDialog::OnDestroy();// TODO: Add your message handler code herePOSITION pos;pos = m_wndList.GetHeadPosition();while( pos ){CObject *ob = m_wndList.GetAt(pos);if ( ob->GetRuntimeClass() == RUNTIME_CLASS(CSkinButton) ){delete (CSkinButton*)ob;}m_wndList.GetNext(pos);}}四、曆史 這是最初版本,我只在Windows XP環境中進行了測試。 我希望聽到您的評論,謝謝,請自由使用這些代碼... |