Monday, December 1, 2008

custom draw a CButton

create a calss

class CBDButton : public CButton

overwrite void DrawItem function and draw your button

void CBDButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
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;

CDC dc;
dc.Attach(lpDrawItemStruct->hDC);

// Draw the button frame.
dc.DrawFrameControl(&lpDrawItemStruct->rcItem, DFC_BUTTON, uStyle);

CRect rect = lpDrawItemStruct->rcItem;
rect.left = rect.left + 1;
rect.right = rect.right - 2;
rect.top = rect.left + 1;
rect.bottom = rect.bottom - 2;
dc.FillSolidRect(rect, m_clrBk);

// Get the button's text.
CString strText;
GetWindowText(strText);
// Draw the button text using the text color.
COLORREF crOldColor = dc.SetTextColor(m_crText);
dc.DrawText(strText, strText.GetLength(), &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
dc.SetTextColor(crOldColor);

dc.Detach();
}

No comments: