MFC中修改Button控件文字颜色

以CButton类为基类自定义一个CTestBtn类,重载DrawItem函数;

void CTestBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
// TODO: Add your code to draw the specified item
UINT uStyle = DFCS_BUTTONPUSH;


ASSERT ( lpDrawItemStruct->CtlType == ODT_BUTTON );


if ( lpDrawItemStruct->itemState & ODS_SELECTED )
{
uStyle = DFCS_PUSHED;
}


::DrawFrameControl ( lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem,
DFC_BUTTON, uStyle );


CString strText;
GetWindowText (strText);


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 );
}


在Dialog类中,建立私有CBrush类型变量m_brush,在其构造函数中初始化为自己想设定的颜色。


在ClassWizard中,给要改名字颜色的控件添加 CTestBtn类型变量




你可能感兴趣的:(MFC中修改Button控件文字颜色)