您的位置:首页 > 其它

自绘制按钮(通过重载DrawItem函数实现)

2010-09-26 19:11 597 查看
void CMyButton::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);

CBrush brushT(RGB(0,0,0)); //背景色
CBrush brushF(RGB(255, 255, 255)); //边框颜色
CDC dc;
COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,255,255)); //设置字体颜色
dc.Attach(lpDrawItemStruct->hDC);

dc.FillRect(&lpDrawItemStruct->rcItem, &brushT); //填充按钮背景色
::SetBkColor(lpDrawItemStruct->hDC, RGB(0, 0, 0)); //设置字体背景色
::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(), &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER); //重绘文字
dc.FrameRect(&lpDrawItemStruct->rcItem, &brushF); //绘按钮边框
::SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT); //设置透明模式
::SetTextColor(lpDrawItemStruct->hDC, crOldColor); //选回字体原来的颜色
dc.Detach();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐