?? 00000006.txt
字號:
--===BBS水木清華站∶精華區===--
-===BBS水木清華站∶精華區===-
31、如何在對話框中顯示一個位圖
這要歸功于Win 32先進的靜態控件和Microsoft的資源編輯器, 在對話框中
顯示位圖是很容易的, 只需將圖形控件拖到對話中并選擇適當屬性即可,用戶也
可以顯示圖標、位圖以及增強型元文件。
32、如何改變對話或窗體視窗的背景顏色
調用CWinApp : : SetDialogBkColor可以改變所有應用程序的背景顏色。第
一個參數指定了背景顏色,第二個參數指定了文本顏色。下例將應用程序對話設置
為藍色背景和黃色文本。
BOOL CSampleApp : : InitInstance ( )
{
…
//use blue dialog with yellow text .
SetDialogBkColor (RGB (0, 0, 255 ), RGB ( 255 , 255 , 0 ) ) ;
…
}
需要重畫對話(或對話的子控件)時,Windows向對話發送消息WM_CTLCOLOR,
通常用戶可以讓Windows選擇繪畫背景的刷子,也可重置該消息指定刷子。下例說
明了創建一個紅色背景對話的步驟。
首先,給對話基類增加一人成員變量CBursh :
class CMyFormView : public CFormView
{
…
private :
CBrush m_ brush ; // background brush
…
} ;
其次, 在類的構造函數中將刷子初始化為所需要的背景顏色。
CMyFormView : : CMyFormView ( )
{
// Initialize background brush .
m_brush .CreateSolidBrush (RGB ( 0, 0, 255 ) )
}
最后,使用ClassWizard處理WM_CTLCOLOR消息并返回一個用來繪畫對話背景的
刷子句柄。注意:由于當重畫對話控件時也要調用該函數,所以要檢測nCtlColor
參量。
HBRUSH CMyFormView : : OnCtlColor (CDC* pDC , CWnd*pWnd , UINT nCtlColor )
{
// Determine if drawing a dialog box . If we are , return +handle to
//our own background brush . Otherwise let windows handle it .
if (nCtlColor = = CTLCOLOR _ DLG )
return (HBRUSH) m_brush .GetSafeHandle ( ) ;
return CFormView : : OnCtlColor (pDC, pWnd , nCtlColor );
}
33、如何獲取一個對話控件的指針
有兩種方法。其一,調用CWnd: : GetDlgItem,獲取一個CWnd*指針調用成
員函數。下例調用GetDlgItem,將返回值傳給一個CSpinButtonCtrl*以便調用
CSpinButtonCtrl : : SetPos 函數:
BOOL CSampleDialog : : OnInitDialog ( )
{
CDialog : : OnInitDialog ( ) ;
//Get pointer to spin button .
CSpinButtonCtrl * pSpin - ( CSpinButtonCtrl *) GetDlgItem (IDC_SPIN) ;
ASSERT _ VALID (pSpin) ;
//Set spin button's default position .
pSpin —> SetPos (10) ;
return TRUE ;
}
其二, 可以使用ClassWizard將控件和成員變量聯系起來。在ClassWizard中簡
單地選擇Member Variables標簽,然后選擇Add Variable …按鈕。如果在對話資源
編輯器中,按下Ctrl鍵并雙擊控件即可轉到Add Member Variable對話。
34、如何禁止和使能控件
控件也是窗口,所以可以調用CWnd : : EnableWindow使能和禁止控件。
//Disable button controls .
m_wndOK.EnableWindow (FALSE ) ;
m_wndApply.EnableWindow (FALSE ) ;
35、如何改變控件的字體
由于控件是也是窗口,用戶可以調用CWnd: : SetFont指定新字體。該函數用
一個Cfont指針,要保證在控件撤消之前不能撤消字體對象。下例將下壓按鈕的字
體改為8點Arial字體:
//Declare font object in class declaration (.H file ).
private :
Cfont m_font ;
// Set font in class implementation (.Cpp file ). Note m_wndButton is a
//member variable added by ClassWizard.DDX routines hook the member
//variable to a dialog button contrlo.
BOOL CSampleDialog : : OnInitDialog ( )
{
…
//Create an 8-point Arial font
m_font . CreateFont (MulDiv (8 , -pDC—> GetDeviceCaps (LOGPIXELSY) ,
72).
0 , 0 , 0 , FW_NORMAL , 0 , 0, 0, ANSI_CHARSER, OUT_STROKE_PRECIS ,
CLIP_STROKE _PRECIS , DRAFT _QUALITY
VARIABLE_PITCH |FF_SWISS, _T ("Arial") );
//Set font for push button .
m_wndButton . SetFont (&m _font );
…
}
36、如何在OLE控件中使用OLE_COLOR數據類型
諸如COleControl : : GetFortColor和COleControl : : GetBackColor等函數
返回OLE _COLOR數據類型的顏色,而GDI對象諸如筆和刷子使用的是COLORREF數據類
型,調用COleControl : : TranslateColor可以很容易地將OLE_COLOR類型改為
COLORREF類型。下例創建了一個當前背景顏色的刷子:
void CSampleControl : : OnDraw (CDC* pdc
const Crect& rcBounds , const Crect& rcInvalid )
{
//Create a brush of the cuttent background color .
CBrush brushBack (TranslateColor (GetBackColor ( ) ) );
//Paint the background using the current background color .
pdc—> FilllRect (rcBounds , &brushBack) ;
//other drawign commands
…
}
37、在不使用通用文件打開對話的情況下如何顯示一個文件列表
調用CWnd: : DlgDirList或者CWnd: : DlgDirListComboBox, Windows 將自動
地向列表框或組合框填充可用的驅動器名或者指定目錄中的文件,下例將Windows目
錄中的文件填充在組合框中:
BOOL CSampleDig : : OnInitDialog ( )
{
CDialog : : OnInitDialog ( )
TCHAR szPath [MAX_PATH] = {"c:\\windows"} ;
int nReslt = DlgDirListComboBox (szPath , IDC_COMBO , IDC_CURIDIR,
DDL_READWRITE |DDL_READONLY|DDL_HIDDEN|
DDL_SYSTEM|DDL_ARCHIVE ) ;
return TRUE ;
}
38、為什么旋轉按鈕控件看起來倒轉
需要調用CSpinCtrl : : SetRange 設置旋轉按鈕控件的范圍,旋轉按鈕控件
的缺省上限為0,缺省下限為100,這意味著增加時旋轉按控件的值由100變為0。
下例將旋轉按鈕控件的范圍設置為0到100:
BOOL CAboutDlg : : OnInitDialog ( )
{
CDialog : : OnInitDialog ( )
//set the lower and upper limit of the spin button
m_wndSpin . SetRange ( 0 ,100 ) ;
return TRUE ;
}
Visual C++ 4.0 Print對話中的Copise旋轉按鈕控件也有同樣的問題:按下Up
按鈕時拷貝的數目減少,而按下Down 按鈕時拷貝的數目增加。
39為什么旋轉按鈕控件不能自動地更新它下面的編輯控件
如果使用旋轉按鈕的autu buddy特性, 則必須保證在對話的標記順序中
buddy窗口優先于旋轉按鈕控件。從Layout菜單中選擇Tab Order菜單項(或者按
下Crtl+D)可以設置對話的標簽順序。
40、如何用位圖顯示下壓按鈕
Windows 95按鈕有幾處新的創建風格,尤其是BS_BITMAP和BS_ICON,要想具
有位圖按鈕,創建按鈕和調用CButton : : SetBitmap或CButton : : SetIcon時
要指定BS_BITMAP或BS_ICON風格。
首先,設置按鈕的圖標屬性。
然后,當對話初始化時調用CButton: : SetIcon。注意:下例用圖標代替位
圖,使用位圖時要小心,因為不知道背景所有的顏色——并非每個人都使用淺灰
色。
BOOL CSampleDlg : : OnInitDialog ( )
{
CDialog : : OnInitDialog ( ) ;
//set the images for the push buttons .
BOOL CSampleDlg : : OnInitDialog ( )
{
CDialog : : OnInitDialog ( ) ;
//set the images for the push buttons .
m_wndButton1.SetIcon (AfxGetApp ( ) —> LoadIcon (IDI _ IPTION1) )
m_wndButton2.SetIcon (AfxGetApp ( ) —> LoadIcon (IDI _ IPTION2) )
m_wndButton3.SetIcon (AfxGetApp ( ) —> LoadIcon (IDI _ IPTION3) )
return TRUE ;
}
-===BBS水木清華站∶精華區===-
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -