mfc求自定义按钮实现改变字体颜色,该怎么处理
mfc求自定义按钮实现改变字体颜色
继承CButton,主要是函数DrawItem()中的内容怎么写
------解决方案--------------------
如果是按钮控件的话,则改变其背景颜色和文本颜色应该重载按钮类的DrawItem()函数,并设置自绘属性,需要注意的是在DrawItem返回前,一定要恢复lpDrawItemStruct->pDC中原有对象,如果改变了的话。
继承CButton,主要是函数DrawItem()中的内容怎么写
------解决方案--------------------
如果是按钮控件的话,则改变其背景颜色和文本颜色应该重载按钮类的DrawItem()函数,并设置自绘属性,需要注意的是在DrawItem返回前,一定要恢复lpDrawItemStruct->pDC中原有对象,如果改变了的话。
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: 添加您的代码以绘制指定项
//绘制按钮框架
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);
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
//设置按钮背景颜色
CBrush brush(RGB(0,255,0));
dc.FillRect(&lpDrawItemStruct->rcItem,&brush);
//设置按钮文本颜色
CString strText;
GetWindowText(strText);
COLORREF clr_tx = dc.SetTextColor(RGB(255,0,0));
int bkMode_old = dc.SetBkMode(TRANSPARENT);
dc.DrawText(strText, strText.GetLength(), &lpDrawItemStruct->rcItem, DT_SINGLELINE
------解决方案--------------------
DT_CENTER
------解决方案--------------------
DT_VCENTER);
//恢复hDC中原有对象
dc.SetBkMode(bkMode_old);
dc.SetTextColor(clr_tx);
dc.Detach();
}