设为首页 加入收藏

TOP

MFC学习笔记之绘图控制(五)
2014-11-23 21:30:32 】 浏览:1812
Tags:MFC 学习 笔记 绘图 控制
{
pDC->SelectObject(&m_font);
}

if(pWnd->GetDlgCtrlID()==IDOK)
{
pDC->SetTextColor(RGB(255,0,0));
return m_brush;
}
return hbr;
}
发现上面对OK按钮的操作是没有作用的。实际上,要想改变按钮控件的背景色和文本颜色,需要使用CButton类的一个成员函数 DrawItem

view plain
virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );
该函数是一个虚函数,当一个自绘按钮(具有BS_OWNWEDRAE风格的按钮)在绘制的时候,框架就会自动调用这个虚函数。因此想要实现自绘按钮就要重载DraeItem函数。

该函数的参数是以一个 DRAWITEMSTRUCT 的指针,该结构体可以去MSDN中查,在该结构体中有一个hDC的成员,为了绘制按钮,可以在DC中加入自定义的颜色、画刷等对象,但是在该函数结束前,一定有要记得恢复hDC中原有对象。

下面我们添加一个新类 CTestBtn 派生于 CButton类 ,然后在该函数中重载 DrawItem函数,然后将MSDN上的例子复制进去:

view plain
void CTestBtn::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;

// Draw the button frame.
::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->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(lpDrawItemStruct->hDC, RGB(255,0,0));
::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
}
写完这个函数之后,我们现在用OK按钮来试试,首先要为该按钮关联一个 CtestBtn 类的变量,然后在OK按钮的属性中将 owner Draw 选上,再运行,就会发现该按钮变红色了。下面的这个DrawItem函数是孙鑫老师写的,显示效果就是图中的显示按钮:

view plain
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);
DrawFocusRect(lpDrawItemStruct->hDC, (LPRECT)&focusRect);
focusRect.left += 4;
focusRect.right -= 4;
focusRect.top += 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, crOldC

首页 上一页 2 3 4 5 下一页 尾页 5/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇VC控制台颜色设置 下一篇TFS 无法使用,显示 "No Com..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目