重载CTabCtrl类,以改变控件的显示背景及边线、显示字体,应该怎么做? 要重载CTabCtrl类,以改变控件的显示背景及边线、显示字体,应该怎么做? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.vckbase.com/document/viewdoc/?id=398参考第5种方案,不需要自绘! Tab Control需要选择“Owner Draw”选项在CTabCtrl的继承类里重写OnEraseBkgnd和DrawItem函数,(m_bgColor是背景颜色)如下:void CColoredTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct){ CRect rect = lpDrawItemStruct->rcItem; int nTabIndex = lpDrawItemStruct->itemID; if (nTabIndex < 0) return; BOOL bSelected = (nTabIndex == GetCurSel()); char label[64]; TC_ITEM tci; tci.mask = TCIF_TEXT|TCIF_IMAGE; tci.pszText = label; tci.cchTextMax = 63; if (!GetItem(nTabIndex, &tci )) return; CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); if (!pDC) return; int nSavedDC = pDC->SaveDC(); // For some bizarre reason the rcItem you get extends above the actual // drawing area. We have to workaround this "feature". rect.top += ::GetSystemMetrics(SM_CYEDGE); pDC->SetBkMode(TRANSPARENT); pDC->FillSolidRect(rect, m_bgColor); // Draw image CImageList* pImageList = GetImageList(); if (pImageList && tci.iImage >= 0) { rect.left += pDC->GetTextExtent(_T(" ")).cx; // Margin // Get height of image so we IMAGEINFO info; pImageList->GetImageInfo(tci.iImage, &info); CRect ImageRect(info.rcImage); int nYpos = rect.top; pImageList->Draw(pDC, tci.iImage, CPoint(rect.left, nYpos), ILD_TRANSPARENT); rect.left += ImageRect.Width(); } if (bSelected) { //pDC->SetTextColor(m_crSelColour); //pDC->SelectObject(&m_SelFont); rect.top -= ::GetSystemMetrics(SM_CYEDGE); pDC->DrawText(label, rect, DT_SINGLELINE|DT_VCENTER|DT_CENTER); } else { //pDC->SetTextColor(m_crUnselColour); //pDC->SelectObject(&m_UnselFont); pDC->DrawText(label, rect, DT_SINGLELINE|DT_BOTTOM|DT_CENTER); } pDC->RestoreDC(nSavedDC);}BOOL CColoredTabCtrl::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default CTabCtrl::OnEraseBkgnd(pDC); CRect Rect; GetClientRect(&Rect); pDC->FillSolidRect(Rect, m_bgColor); return TRUE;} 自绘是唯一的办法...但是某些地方需要特殊处理,特别是up-down VC中为什么用指向指针的指针来获得返回的接口指针 CRectTracker 在 Cscrollview 中画橡皮筋,拉动滚动条后,显示不正确问题 怎样判断recv的数据格式是否正确 请问如何立即发送鼠标点击消息 请问如何在IE中加入工具栏按钮(toolbar button) 求制作报表设计器的思路 哪位高手知道在VC中使用active控件比较全面的专门书籍 写了个托盘运行的程序,想用一个16位的图标,搞不定帮忙! 怎样更改文件图标? 初始化com失败 创建excel失败 急急急!!! 菜单加载的错误 关于字体设置问题??
参考第5种方案,不需要自绘!
在CTabCtrl的继承类里重写OnEraseBkgnd和DrawItem函数,(m_bgColor是背景颜色)如下:void CColoredTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CRect rect = lpDrawItemStruct->rcItem;
int nTabIndex = lpDrawItemStruct->itemID;
if (nTabIndex < 0) return;
BOOL bSelected = (nTabIndex == GetCurSel()); char label[64];
TC_ITEM tci;
tci.mask = TCIF_TEXT|TCIF_IMAGE;
tci.pszText = label;
tci.cchTextMax = 63;
if (!GetItem(nTabIndex, &tci )) return; CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
if (!pDC) return;
int nSavedDC = pDC->SaveDC(); // For some bizarre reason the rcItem you get extends above the actual
// drawing area. We have to workaround this "feature".
rect.top += ::GetSystemMetrics(SM_CYEDGE); pDC->SetBkMode(TRANSPARENT);
pDC->FillSolidRect(rect, m_bgColor); // Draw image
CImageList* pImageList = GetImageList();
if (pImageList && tci.iImage >= 0) { rect.left += pDC->GetTextExtent(_T(" ")).cx; // Margin // Get height of image so we
IMAGEINFO info;
pImageList->GetImageInfo(tci.iImage, &info);
CRect ImageRect(info.rcImage);
int nYpos = rect.top; pImageList->Draw(pDC, tci.iImage, CPoint(rect.left, nYpos), ILD_TRANSPARENT);
rect.left += ImageRect.Width();
} if (bSelected) {
//pDC->SetTextColor(m_crSelColour);
//pDC->SelectObject(&m_SelFont);
rect.top -= ::GetSystemMetrics(SM_CYEDGE);
pDC->DrawText(label, rect, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
} else {
//pDC->SetTextColor(m_crUnselColour);
//pDC->SelectObject(&m_UnselFont);
pDC->DrawText(label, rect, DT_SINGLELINE|DT_BOTTOM|DT_CENTER);
} pDC->RestoreDC(nSavedDC);
}
BOOL CColoredTabCtrl::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CTabCtrl::OnEraseBkgnd(pDC);
CRect Rect;
GetClientRect(&Rect);
pDC->FillSolidRect(Rect, m_bgColor); return TRUE;
}
但是某些地方需要特殊处理,特别是up-down