您的位置:首页 > 其它

可设置背景、字体的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);




}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: