改变CButton按钮控件的文本和背景颜色
2012-08-27 13:14
357 查看
派生一个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);
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, crOldColor);
}
将对话框资源模板上的按钮添加一个控制变量, 类型就是你派生的这个类,
最后, 记得把按钮的ownerdraw属性选上. 搞定!
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, crOldColor);
}
将对话框资源模板上的按钮添加一个控制变量, 类型就是你派生的这个类,
最后, 记得把按钮的ownerdraw属性选上. 搞定!
相关文章推荐
- 自己对“改变CButton按钮控件的文本和背景颜色”的理解
- 改变按钮控件的背景及文本颜色
- 改变对话框和控件的背景及文本颜色
- VC改变控件的背景颜色 字体 文本颜色
- VC改变控件的背景颜色 字体 文本颜色
- VC++深入详解 第十章 绘图控制 用CButtonST类改变按钮控件的背景色及文本颜色 自己实现
- MFC窗体上的STATIC控件鼠标移上去改变文本和背景颜色,鼠标离开恢复正常
- 对话框背景颜色 控件文本颜色 文本背景色 位图背景 按钮位图
- VC对话框加载图片、改变背景颜色、画图、显示隐藏控件
- VC++6.0中改变窗口背景颜色和控件背景颜色
- asp.net与winform中的控件背景颜色值的改变(c#)
- 一个继承于CButton的按钮控件类,实现Button背景色与文字的共存与改变,可以自行设计背景色
- 调色板QPalette类用法详解(附实例、源码)(很清楚:窗口背景色 前景色 按钮的颜色 按钮文本的颜色 )
- Android 设置按钮点击时候颜色背景的改变
- jquery鼠标点击改变控件的背景颜色
- Flex中通过设置fillColors样式改变ComboBox控件的填充背景颜色的例子
- 从CStatic派生新类,形成具有可以改变字体、背景颜色 和字体颜色的控件类
- 手指对控件的按下,放了,背景颜色改变的监听事件
- 改变控件的背景颜色和字体颜色
- 一个继承于CButton的按钮控件类,实现Button背景色与文字的共存与改变,可以自行设计背景色