改变CButton按钮控件的⽂本和背景颜⾊派⽣⼀个CButton类(下⾯的CSXBtn就是), 重载DrawItem函数:
void CSXBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
UINT uStyle = BS_DEFPUSHBUTTON ;//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* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
// Get the button\'s text.
CString strText;
GetWindowText(strText);
// Draw the button text using the text color red.
CBrush B;
CRect rect;
CRect focusRect;
focusRect.CopyRect(&lpDrawItemStruct->rcItem);
改变button按钮的形状DrawFocusRect(lpDrawItemStruct->hDC, (LPRECT)&focusRect);
focusRect.left += 4;
focusRect.right -= 4;
focusRect.bottom -= 4;
rect.CopyRect(&lpDrawItemStruct->rcItem);
pDC->Draw3dRect(rect, ::GetSysColor(COLOR_BTNSHADOW), ::GetSysColor(COLOR_BTNHILIGHT));
B.CreateSolidBrush(RGB(0,255,0));
::FillRect(lpDrawItemStruct->hDC,&rect, (HBRUSH)B.m_hObject);
::SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);
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);
}
将对话框资源模板上的按钮添加⼀个控制变量, 类型就是你派⽣的这个类,
最后, 记得把按钮的ownerdraw属性选上. 搞定!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论