自绘CTabCtrl类可以改变背景色(VC)


class CTabCtrlST : public CTabCtrl
{
// Construction
public:
CTabCtrlST();
// Attributes
public:
//改变背景色
void SetTabBk(COLORREF crColor);
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTabCtrlST)
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CTabCtrlST();
// Generated message map functions
protected:
//{{AFX_MSG(CTabCtrlST)
afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
//}}AFX_MSG
COLORREF m_colorBK;
DECLARE_MESSAGE_MAP()
};

 

 

// TabCtrlST.cpp : 实现文件
//

#include "stdafx.h"
#include "TabCtrlST.h"


// CTabCtrlST

//IMPLEMENT_DYNAMIC(CTabCtrlST, CTabCtrl)

CTabCtrlST::CTabCtrlST()
{
m_colorBK=RGB(32,65,94);
}

CTabCtrlST::~CTabCtrlST()
{
}


BEGIN_MESSAGE_MAP(CTabCtrlST, CTabCtrl)
ON_WM_DRAWITEM()
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

 

// CTabCtrlST 消息处理程序


void CTabCtrlST::SetTabBk(COLORREF crColor)
{
m_colorBK = crColor;
this->Invalidate();
}

void CTabCtrlST::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your message handler code here and/or call default
TRACE("nIDCtl:%d/n",nIDCtl);
CTabCtrl::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

void CTabCtrlST::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CBrush brushBK(m_colorBK);
char szTabText[100];
UINT bkColor;

CBrush *cbr;
TC_ITEM tci;
LOGBRUSH m_LogBrush;

cbr = &brushBK;
cbr->GetLogBrush(&m_LogBrush);
bkColor = m_LogBrush.lbColor;
//  TRACE("item:%d/n",lpDrawItemStruct->itemID);
memset(szTabText, '/0', sizeof(szTabText));
tci.mask = TCIF_TEXT;
tci.pszText = szTabText;
tci.cchTextMax = sizeof(szTabText)-1;

GetItem(lpDrawItemStruct->itemID, &tci);
FillRect(lpDrawItemStruct->hDC,&lpDrawItemStruct->rcItem,*cbr);
::SetBkColor(lpDrawItemStruct->hDC,bkColor);
TextOut(lpDrawItemStruct->hDC,
lpDrawItemStruct->rcItem.left+5,
lpDrawItemStruct->rcItem.top+5,
tci.pszText,
lstrlen(tci.pszText));
}


BOOL CTabCtrlST::OnEraseBkgnd(CDC* pDC)
{
CBrush brushBK(m_colorBK);
CRect rect;
this->GetClientRect(rect);
pDC->FillRect(rect,&brushBK);
TRACE("%d,%d,%d,%d/n",rect.left,rect.top,rect.right,rect.bottom);
return TRUE;
}

 来自 https://blog.csdn.net/luo_sen/article/details/2752000?utm_source=blogxgwz6

posted @ 2019-07-15 21:27  宇宙之外  阅读(680)  评论(0编辑  收藏  举报