怎么样设置背景、字体的CStatic?
现成的控件不能完全满足我们的需求时,可以自己写个基于现有控件的子类/辅助类, 按照自己的特定需求来定制自己的控件。特别是对特殊风格的UI显示效果,般都通过重写DrawItem/OnCustomDraw这2个函数实现
1. 定义一个CStaticEx继承自CStatic
2. 属性接口
SetBkColor(COLORREFclrBkgnd);
SetFont(CFontfont,BOOLbRedraw/*=TRUE*/);
SetTextColor(COLORREFclrText);
SetWindowTextEx(CStringstrText);
SetFont(CFontfont,BOOLbRedraw/*=TRUE*/);
SetTextColor(COLORREFclrText);
SetWindowTextEx(CStringstrText);
3. 绘制定制的CStatic
voidCStaticEx::DrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct)
{
ASSERT(lpDrawItemStruct!=NULL);
CDC*pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
CRectrcItem=lpDrawItemStruct->rcItem;
if(m_clrBkgnd==-1)
//ifnospecifybackgroundcolor,settransparentstyle
pDC->SetBkMode(TRANSPARENT);
else
pDC_>FillSolidRect(&rcItem,m_clrBkgnd);//setthebackgroundcolor
//setthespecifyfontstyle
CFont*pOldFont=pDC->SelectObject(&m_font);
//setthespecifytextcolor
COLORREFclrOldText=pDC->SetTextColor(m_clrText);
CRectrcText(rcItem);
rcText.left=rcText.left+2;
//drawthetext
pDC->DrawText(m_strText,m_strText.GetLength(),rcText,DT_LEFT|DT_NOPREFIX|DT_VCENTER|DT_WORDBREAK);
//reverttheDC'sstate
pDC_>SelectObject(pOldFont);
pDC_>SetTextColor(clrOldText);
}
{
ASSERT(lpDrawItemStruct!=NULL);
CDC*pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
CRectrcItem=lpDrawItemStruct->rcItem;
if(m_clrBkgnd==-1)
//ifnospecifybackgroundcolor,settransparentstyle
pDC->SetBkMode(TRANSPARENT);
else
pDC_>FillSolidRect(&rcItem,m_clrBkgnd);//setthebackgroundcolor
//setthespecifyfontstyle
CFont*pOldFont=pDC->SelectObject(&m_font);
//setthespecifytextcolor
COLORREFclrOldText=pDC->SetTextColor(m_clrText);
CRectrcText(rcItem);
rcText.left=rcText.left+2;
//drawthetext
pDC->DrawText(m_strText,m_strText.GetLength(),rcText,DT_LEFT|DT_NOPREFIX|DT_VCENTER|DT_WORDBREAK);
//reverttheDC'sstate
pDC_>SelectObject(pOldFont);
pDC_>SetTextColor(clrOldText);
}
本文地址:http://www.45fan.com/dnjc/71217.html