?? vc小知識.txt
字號:
{
// 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
)
}
(37) 如何獲取一個對話控件的指針
有兩種方法。其一,調(diào)用CWnd: : GetDlgItem,獲取一個CWnd*指針調(diào)用成員函數(shù)。下例調(diào)用GetDlgItem,將返回值傳給一個CSpinButtonCtrl*以便調(diào)用CSpinButtonCtrl : : SetPos 函數(shù):
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將控件和成員變量聯(lián)系起來。在ClassWizard中簡單地選擇Member Variables標簽,然后選擇Add Variable …按鈕。如果在對話資源編輯器中,按下Ctrl鍵并雙擊控件即可轉(zhuǎn)到Add Member Variable對話。
(38) 如何禁止和使能控件
控件也是窗口,所以可以調(diào)用CWnd : : EnableWindow使能和禁止控件。
//Disable button controls .
m_wndOK.EnableWindow (FALSE )
m_wndApply.EnableWindow (FALSE )
(39) 如何改變控件的字體
由于控件是也是窗口,用戶可以調(diào)用CWnd: : SetFont指定新字體。該函數(shù)用一個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 )
…
}
(40) 如何在OLE控件中使用OLE_COLOR數(shù)據(jù)類型
諸如COleControl : : GetFortColor和COleControl : : GetBackColor等函數(shù)返回OLE _COLOR數(shù)據(jù)類型的顏色,而GDI對象諸如筆和刷子使用的是COLORREF數(shù)據(jù)類型,調(diào)用COleControl : : TranslateColor可以很容易地將OLE_COLOR類型改為COLORREF類型。下例創(chuàng)建了一個當前背景顏色的刷子:
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 backgroundcolor .
pdc—> FilllRect (rcBounds , &brushBack)
//other drawign commands
…
}
(41) 在不使用通用文件打開對話的情況下如何顯示一個文件列表
調(diào)用CWnd: : DlgDirList或者CWnd: : DlgDirListComboBox,Windows 將自動地向列表框或組合框填充可用的驅(qū)動器名或者指定目錄中的文件,下例將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
}
(42) 為什么旋轉(zhuǎn)按鈕控件看起來倒轉(zhuǎn)
需要調(diào)用CSpinCtrl : : SetRange 設(shè)置旋轉(zhuǎn)按鈕控件的范圍,旋轉(zhuǎn)按鈕控件的缺省上限為0,缺省下限為100,這意味著增加時旋轉(zhuǎn)按控件的值由100變?yōu)?。下例將旋轉(zhuǎn)按鈕控件的范圍設(shè)置為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旋轉(zhuǎn)按鈕控件也有同樣的問題:按下Up按鈕時拷貝的數(shù)目減少,而按下Down 按鈕時拷貝的數(shù)目增加。
(43) 為什么旋轉(zhuǎn)按鈕控件不能自動地更新它下面的編輯控件
如果使用旋轉(zhuǎn)按鈕的autu buddy特性, 則必須保證在對話的標記順序中buddy窗口優(yōu)先于旋轉(zhuǎn)按鈕控件。從Layout菜單中選擇Tab Order菜單項(或者按下Crtl+D)可以設(shè)置對話的標簽順序。
(44) 如何用位圖顯示下壓按鈕
Windows 95按鈕有幾處新的創(chuàng)建風(fēng)格,尤其是BS_BITMAP和BS_ICON,要想具有位圖按鈕,創(chuàng)建按鈕和調(diào)用CButton : : SetBitmap或CButton : : SetIcon時要指定BS_BITMAP或BS_ICON風(fēng)格。
首先,設(shè)置按鈕的圖標屬性。然后,當對話初始化時調(diào)用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
}
(45) 如何一個創(chuàng)建三態(tài)下壓按鈕
可以使用新的BS_PUSHBUTTON 風(fēng)格位和檢測框以及按鈕來創(chuàng)建一個三態(tài)下壓按鈕。這很容易,只需將檢測框和按鈕拖拉到對話中并指定屬性Push—like即可。不用任何附加程序就可以成為三態(tài)下壓按鈕。
(46) 如何動態(tài)創(chuàng)建控件
分配一個控件對象的實例并調(diào)用其Create成員函數(shù)。開發(fā)者最容易忽略兩件事:忘記指定WS_VISBLE標簽和在棧中分配控件對象。下例動態(tài)地創(chuàng)建一個下壓按鈕控件:
//In class declaration (.H file ).
private : CButton* m _pButton
//In class implementation (.cpp file ) .
m_pButton =new CButton
ASSERT_VALID (m_pButton)
m_pButton —>Create (_T ("Button Title ") , WS_CHILD |WS_VISIBLE |BS_PUSHBUTTON. Crect ( 0, 0, 100 , 24) , this , IDC _MYBUTTON )
(47) 如何限制編輯框中的準許字符
如果用戶在編輯控件中只允許接收數(shù)字,可以使用一個標準的編輯控件并指定新的創(chuàng)建標志ES_NUMBERS,它是Windows 95新增加的標志,該標志限制 編輯控件只按收數(shù)字字符。如果用戶需要復(fù)雜的編輯控件,可以使用Microsoft 的屏蔽編輯控件,它是一個很有用的OLE定制控件。
如果希望不使用OLE 定制控件自己處理字符,可以派生一個CEdit類并處理WM_CHAR消息,然后從編輯控件中過濾出特定的字符。首先,使用ClassWizard建立一個 CEdit的派生類,其次,在對話類中指定一個成員變量將編輯控件分類在OnInitdialog 中調(diào)用CWnd: : SubclassDlgItem .
//In your dialog class declaration (.H file )
private : CMyEdit m_wndEdit // Instance of your new edit control .
//In you dialog class implementation (.CPP file )
BOOL CSampleDialog : : OnInitDialog ( )
{
…
//Subclass the edit lontrod .
m_wndEdit .SubclassDlgItem (IDC_EDIT,this)
…
}
使用ClassWizard處理WM_CHAR消息,計算nChar參量并決定所執(zhí)行的操作,用戶可以確定是否修改、傳送字符。下例說明了如何顯示字母字符,如果字符是字母字符,則調(diào)用CWnd OnChar,否則不調(diào)用OnChar.
//Only display alphabetic dharacters .
void CMyEdit : : OnChar (UINT nChar , UINT nRepCnt , UITN nFlags )
{
//Determine if nChar is an alphabetic character.
if (: : IsCharAlpha ( ( TCHAR) nChar ) )
CEdit : : OnChar (nChar, nRepCnt , nFlags )
}
如果要修改字符,則不能僅僅簡單地用修改過的nChar調(diào)用CEdit: : OnChar,然后CEdit: : OnChar調(diào)用CWnd: : Default獲取原來的wParam 和lParam 的值,這樣是不行的。要修改一個字符,需要首先修改nChar,然后用修改過的nChar調(diào)用CWnd: : DefWindowProc。下例說明了如何將字符轉(zhuǎn)變?yōu)榇髮懀?//Make all characters uppercase
void CMyEdit : : OnChar (UINT nChar , UINT nRepCnt , UINT nFlags )
{
//Make sure character is uppercase .
if (: : IsCharAlpha ( .( TCHAR) nChar)
nChar=: : CharUpper(nChar )
//Bypass default OnChar processing and directly call
//default window proc.
DefWindProc (WM_CHAR, nChar , MAKELPARAM (nRepCnt, nFlags ))
}
(48) 如何改變控件的顏色
有兩種方法。其一,可以在父類中指定控件的顏色,或者利用MFC4.0新的消息反射在控件類中指定顏色。 當控件需要重新著色時,工作框調(diào)用父窗口(通常是對話框)的CWnd: : OnCrtlColor,可以在父窗口類中重置該函數(shù)并指定控件的新的繪畫屬性。例如,下述代碼將對話中的所有編輯控件文本顏色改為紅色:
HBRUSH CAboutDig : : OnCtlColor (CDC * pDCM , CWnd * pWnd , UINT nCtlColor)
{
HBRUSH hbr = CDialog : : OnCtlColor (pDC, pWnd , nCtlColor )
//Draw red text for all edit controls .
if (nCtlColor= = CTLCOLOR_EDIT )
pDC —> SetTextColor (RGB (255, 0 , 0 , ) )
return hbr
}
然而,由于每個父窗口必須處理通知消息并指定每個控件的繪畫屬性,所以,這種方法不是完全的面向?qū)ο蟮姆椒ā?丶幚碓撓⒉⒅付ɡL畫屬性更合情合理。消息反射允許用戶這樣做。通知消息首先發(fā)送給父窗口,如果父窗口沒有處理則發(fā)送給控件。創(chuàng)建一個定制彩色列表框控件必須遵循下述步驟。
首先,使用ClassWizard 創(chuàng)建一個CListBox 的派生類并為該類添加下述數(shù)據(jù)成員。
class CMyListBox publilc CListBox
{
…
private
COLORREF m_clrFor // foreground color
COLORREF m_clrBack //background color
Cbrush m_brush //background brush
…
}
其次,在類的構(gòu)造函數(shù)中,初始化數(shù)據(jù)中。
CMyListBox : : CMyListBox ()
{
//Initialize data members .
m_clrFore =RGB (255 , 255 , 0) //yellow text
m_clrBack=RGB (0 , 0 , 255) // blue background
m_brush . CreateSolidBrush (m _clrBack )
}
最后,使用ClassWizard處理反射的WM_CTLCOLOR(=WM_CTLCOLOR)消息并指定新的繪畫屬性。
HBRUSH CMyListBox : : CtlColor (CDC* pDC, UINT nCtlColor )
{
pDC—>SetTextColor (m_clrFore)
pDC—>SetBkColor (m_clrBack)
return (HBRUSH) m_brush.GetSafeHandle ()
}
現(xiàn)在,控件可以自己決定如何繪畫,與父窗口無關(guān)。
(49) 當向列表框中添加多個項時如何防止閃爍
調(diào)用CWnd::SetRedraw 清除重畫標志可以禁止CListBox(或者窗口)重畫。當向列表框添加幾個項時,用戶可以清除重畫標志,然后添加項,最后恢復(fù)重畫標志。為確保重畫列表框的新項,調(diào)用SetRedraw (TRUE) 之后調(diào)用CWnd::Invalidate。
//Disable redrawing.
pListBox->SetRedraw (FALSE)
//Fill in the list box gere
//Enable drwing and make sure list box is redrawn.
pListBox->SetRedraw (TRUE)
pListBox->Invalidate ()
(50) 如何向編輯控件中添加文本
由于沒有CEdit:: AppendText函數(shù),用戶只好自己做此項工作。調(diào)用CEdit:: SetSel移動到編輯控件末尾,然后調(diào)用CEdit:: ReplaceSel添加文本。下例是AppendText 的一種實現(xiàn)方法:
void CMyEdit:: AppendText (LPCSTR pText)
{
int nLen=GetWindowTextLength ()
SetFocus ()
SetSel (nLen, nLen)
ReplaceSel (pText)
}
(51) 如何訪問預(yù)定義的GDI對象
可以通過調(diào)用CDC:: SlectStockObject使用Windows的幾個預(yù)定義的對象,諸如刷子、筆以及字體。下例使用了Windows預(yù)定義的筆和刷子GDI對象在視窗中畫一個橢圓。
//Draw ellipse using stock black pen and gray brush.
void CSampleView:: OnDraw (CDC* pDC)
{
//Determine size of view.
CRect rcView
GetClientRect (rcView)
//Use stock black pen and stock gray brush to draw ellipse.
pDC->SelectStockObject (BLACK_PEN)
pDC->SelectStockObject (GRAY_BRUSH)
//Draw the ellipse.
pDC->Ellipse (reView)
}
也可以調(diào)用新的SDK函數(shù)GetSysColorBrush獲取一個系統(tǒng)顏色刷子,下例用背景色在視窗中畫一個橢圓:
void CsampleView:: OnDraw (CDC* pDC)
{
//Determine size of view.
CRect rcView
GetClientRect (rcView)
//Use background color for tooltips brush.
CBrush * pOrgBrush=pDC->SelectObject ( CBrush ::FromHandle( ::GetSysColorBrush (COLOR_INFOBK)))
//Draw the ellipse.
pDC->Ellipse (rcView)
//Restore original brush.
pDC->SelectObject (pOrgBrush)
}
(52) 如何獲取GDI對象的屬性信息
可以調(diào)用GDIObject:: GetObject。這個函數(shù)將指定圖表設(shè)備的消息寫入到緩沖區(qū)。下例創(chuàng)建了幾個有用的輔助函數(shù)。
//Determine if font is bold.
BOOL IsFontBold (const CFont&font)
{
LOGFONT stFont
font.GetObject (sizeof (LOGFONT), &stFont)
return (stFont.lfBold)? TRUE: FALSE
}
//Return the size of a bitmap.
CSize GetBitmapSize (const CBitmap&bitmap)
{
BITMAP stBitmap
bitmap.GetObject (sizeof (BITMAP), &stBitmap)
return CSize (stBitmap.bmWidth, stBitmap.bmHeight)
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -