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

  1. #if !defined(AFX_TABCTRLST_H__032460F5_8D49_4AB4_A7BF_1350F7EBE8C2__INCLUDED_)
  2. #define AFX_TABCTRLST_H__032460F5_8D49_4AB4_A7BF_1350F7EBE8C2__INCLUDED_
  3. #if _MSC_VER > 1000
  4. #pragma once
  5. #endif // _MSC_VER > 1000
  6. // TabCtrlST.h : header file
  7. //
  8. /
  9. // CTabCtrlST window
  10. class CTabCtrlST : public CTabCtrl
  11. {
  12. // Construction
  13. public:
  14.     CTabCtrlST();
  15. // Attributes
  16. public:
  17.     //改变背景色
  18.     void SetTabBk(COLORREF crColor);
  19. // Operations
  20. public:
  21. // Overrides
  22.     // ClassWizard generated virtual function overrides
  23.     //{{AFX_VIRTUAL(CTabCtrlST)
  24.     virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
  25.     afx_msg BOOL OnEraseBkgnd(CDC* pDC);
  26.     //}}AFX_VIRTUAL
  27. // Implementation
  28. public:
  29.     virtual ~CTabCtrlST();
  30.     // Generated message map functions
  31. protected:
  32.     //{{AFX_MSG(CTabCtrlST)
  33.     afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
  34.     //}}AFX_MSG
  35.     COLORREF m_colorBK;
  36.     DECLARE_MESSAGE_MAP()
  37. };
  38. /
  39. //{{AFX_INSERT_LOCATION}}
  40. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  41. #endif // !defined(AFX_TABCTRLST_H__032460F5_8D49_4AB4_A7BF_1350F7EBE8C2__INCLUDED_)

 

  1. // TabCtrlST.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "TabCtrlST.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. /
  11. // CTabCtrlST
  12. CTabCtrlST::CTabCtrlST()
  13. {
  14.     m_colorBK=RGB(240,240,255);
  15. }
  16. CTabCtrlST::~CTabCtrlST()
  17. {
  18. }
  19. BEGIN_MESSAGE_MAP(CTabCtrlST, CTabCtrl)
  20.     //{{AFX_MSG_MAP(CTabCtrlST)
  21.     ON_WM_DRAWITEM()
  22.     ON_WM_ERASEBKGND()
  23.     //}}AFX_MSG_MAP
  24. END_MESSAGE_MAP()
  25. /
  26. // CTabCtrlST message handlers
  27. void CTabCtrlST::SetTabBk(COLORREF crColor)
  28. {
  29.     m_colorBK = crColor;
  30.     
  31.     this->Invalidate();
  32.     
  33. }
  34. void CTabCtrlST::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
  35. {
  36.     // TODO: Add your message handler code here and/or call default
  37.     TRACE("nIDCtl:%d/n",nIDCtl);
  38.     
  39.     CTabCtrl::OnDrawItem(nIDCtl, lpDrawItemStruct);
  40. }
  41. void CTabCtrlST::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  42.     CBrush brushBK(m_colorBK);
  43.     
  44.     char     szTabText[100];     
  45.     UINT     bkColor;     
  46.     CBrush   *cbr;     
  47.     TC_ITEM  tci;     
  48.     LOGBRUSH m_LogBrush;     
  49.   
  50.     cbr   =   &brushBK;     
  51.     cbr->GetLogBrush(&m_LogBrush);     
  52.     bkColor   =   m_LogBrush.lbColor;  
  53. //  TRACE("item:%d/n",lpDrawItemStruct->itemID);      
  54.     memset(szTabText,   '/0',   sizeof(szTabText));     
  55.     tci.mask                 =   TCIF_TEXT;     
  56.     tci.pszText         =   szTabText;     
  57.     tci.cchTextMax     =   sizeof(szTabText)-1;     
  58.     
  59.     GetItem(lpDrawItemStruct->itemID,   &tci);     
  60.     FillRect(lpDrawItemStruct->hDC,&lpDrawItemStruct->rcItem,*cbr);   
  61.     ::SetBkColor(lpDrawItemStruct->hDC,bkColor);   
  62.     TextOut(lpDrawItemStruct->hDC,     
  63.         lpDrawItemStruct->rcItem.left+5,     
  64.         lpDrawItemStruct->rcItem.top+5,     
  65.         tci.pszText,     
  66.         lstrlen(tci.pszText));     
  67.     
  68. }
  69. BOOL CTabCtrlST::OnEraseBkgnd(CDC* pDC)
  70. {
  71.     CBrush brushBK(m_colorBK);
  72.     CRect rect;
  73.     this->GetClientRect(rect);
  74.     pDC->FillRect(rect,&brushBK);
  75.     TRACE("%d,%d,%d,%d/n",rect.left,rect.top,rect.right,rect.bottom);
  76.     return TRUE;
  77. }
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值