CButton::DrawItem
呼叫框架,只有一個主控描繪 (Owner-Drawn) 按鈕的視覺外觀變更時。
virtual void DrawItem(
LPDRAWITEMSTRUCT lpDrawItemStruct
);
參數
- lpDrawItemStruct
為 DRAWITEMSTRUCT 結構的長度的指標。 結構包含項目相關資訊繪製的介面和需要的繪圖的型別。
備註
一個主控描繪 (Owner-Drawn) 按鈕有 BS_OWNERDRAW 樣式。 覆寫這個成員函式來實作一個主控描繪 CButton 物件的繪圖。 在成員函式結束之前,應用程式應該還原為顯示內容選取的所有圖形裝置介面 (GDI) 物件提供在 lpDrawItemStruct 。
請參閱 BS_ 模式值。
範例
// NOTE: CMyButton is a class derived from CButton. The CMyButton
// object was created as follows:
//
// CMyButton myButton;
// myButton.Create(_T("My button"),
// WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_OWNERDRAW,
// CRect(10,10,100,30), pParentWnd, 1);
//
// This example implements the DrawItem method for a CButton-derived
// class that draws the button's text using the color red.
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
UINT uStyle = DFCS_BUTTONPUSH;
// This code only works with buttons.
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
// If drawing selected, add the pushed style to DrawFrameControl.
if (lpDrawItemStruct->itemState & ODS_SELECTED)
uStyle |= DFCS_PUSHED;
// Draw the button frame.
::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem,
DFC_BUTTON, uStyle);
// Get the button's text.
CString strText;
GetWindowText(strText);
// Draw the button text using the text color red.
COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0));
::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
}
需求
Header: afxwin.h