MFC 控件重繪(1)

僅僅更換圖片,不具有可移植性,可繼承性,則可使用以下方法。

1 創建MFC工程

2 添加按鈕和圖片(如果需要圖片)

3 按鈕屬性Owner Draw = TRUE

4 消息映射WM_DRAWITEM

5 添加如下代碼(文字改爲紅色)

void CTestButtonDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your message handler code here and/or call default


UINT uStyle = DFCS_BUTTONPUSH;
//This code only works with buttons.
ASSERT(lpDrawItemsStruct->CtlType == ODT_BUTTON);
//If drawing selected, add the pushed style to DrawFrameControl
if (lpDrawItemsStruct->itemState & ODS_SELECTED)
uStyle |= DFCS_PUSHED;
//draw the button frame
::DrawFrameControl(lpDrawItemsStruct->hDC, &lpDrawItemsStruct->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(lpDrawItemsStruct->hDC, RGB(255, 0, 0));
::DrawText(lpDrawItemsStruct->hDC, strText, strText.GetLength(),
&lpDrawItemsStruct->rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);


::SetTextColor(lpDrawItemsStruct->hDC, crOldColor);

CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

例程下載地址(0積分):http://download.csdn.net/detail/u013469110/9265517

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章