可设置背景、字体的CStatic
2006-11-01 08:56
302 查看
现成的控件不能完全满足我们的需求时,可以自己写个基于现有控件的子类/辅助类, 按照自己的特定需求来定制自己的控件。特别是对特殊风格的UI显示效果,般都通过重写DrawItem/OnCustomDraw这2个函数实现
1. 定义一个CStaticEx继承自CStatic
2. 属性接口
SetBkColor(COLORREF clrBkgnd);
SetFont(CFont font, BOOL bRedraw /**//*= TRUE*/);
SetTextColor(COLORREF clrText);
SetWindowTextEx(CString strText);
3. 绘制定制的CStatic
void CStaticEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
...{
ASSERT(lpDrawItemStruct != NULL);
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rcItem = lpDrawItemStruct->rcItem;
if(m_clrBkgnd == -1)
//if no specify background color, set transparent style
pDC->SetBkMode(TRANSPARENT);
else
pDC_>FillSolidRect(&rcItem, m_clrBkgnd); //set the back ground color
//set the specify font style
CFont* pOldFont = pDC->SelectObject(&m_font);
//set the specify text color
COLORREF clrOldText = pDC->SetTextColor(m_clrText);
CRect rcText(rcItem);
rcText.left = rcText.left + 2;
//draw the text
pDC->DrawText(m_strText, m_strText.GetLength(), rcText, DT_LEFT|DT_NOPREFIX|DT_VCENTER|DT_WORDBREAK);
//revert the DC's state
pDC_>SelectObject(pOldFont);
pDC_>SetTextColor(clrOldText);
}
1. 定义一个CStaticEx继承自CStatic
2. 属性接口
SetBkColor(COLORREF clrBkgnd);
SetFont(CFont font, BOOL bRedraw /**//*= TRUE*/);
SetTextColor(COLORREF clrText);
SetWindowTextEx(CString strText);
3. 绘制定制的CStatic
void CStaticEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
...{
ASSERT(lpDrawItemStruct != NULL);
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rcItem = lpDrawItemStruct->rcItem;
if(m_clrBkgnd == -1)
//if no specify background color, set transparent style
pDC->SetBkMode(TRANSPARENT);
else
pDC_>FillSolidRect(&rcItem, m_clrBkgnd); //set the back ground color
//set the specify font style
CFont* pOldFont = pDC->SelectObject(&m_font);
//set the specify text color
COLORREF clrOldText = pDC->SetTextColor(m_clrText);
CRect rcText(rcItem);
rcText.left = rcText.left + 2;
//draw the text
pDC->DrawText(m_strText, m_strText.GetLength(), rcText, DT_LEFT|DT_NOPREFIX|DT_VCENTER|DT_WORDBREAK);
//revert the DC's state
pDC_>SelectObject(pOldFont);
pDC_>SetTextColor(clrOldText);
}
相关文章推荐
- CStatic设置字体大小及背景透明的方法
- CStatic设置字体大小及背景透明的方法
- Android Studio设置主题背景&背景图片&字体
- 设置UITableView Section的背景颜色和字体颜色-自定义Section
- Eclipse设置:背景与字体大小和xml文件中字体大小调整
- C/C++控制台输出时设置字体及背景颜色
- VS2010编程字体和背景颜色设置
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- TextView字体和背景图片 设置透明度
- CStatic字体设置
- EXTJS根据值Value改变gridpanel单元格背景颜色或者设置整行字体颜色
- android studio 更改背景和设置字体大小
- 3、如何设置SecureCRT的字体及背景颜色
- OnCtlColor函数:MFC设置静态文本框,编辑框等控件背景和字体颜色
- OnCtlColor函数:MFC设置静态文本框,编辑框等控件背景和字体颜色
- CSS基础—背景、字体的设置
- Eclipse中背景颜色(护眼)及字体设置
- Eclipse设置:背景与字体大小和xml文件中字体大小调整
- 动态设置Edit控件的背景及字体颜色
- eclipse设置背景颜色、字体和导出