自繪CTabCtrl類

  1. // TabCtrlST.h : header file///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. #if !defined(AFX_TABCTRLST_H__032460F5_8D49_4AB4_A7BF_1350F7EBE8C2__INCLUDED_)
  3. #define AFX_TABCTRLST_H__032460F5_8D49_4AB4_A7BF_1350F7EBE8C2__INCLUDED_

  4. #if _MSC_VER > 1000
  5. #pragma once
  6. #endif // _MSC_VER > 1000
  7. // TabCtrlST.h : header file
  8. //

  9. /////////////////////////////////////////////////////////////////////////////
  10. // CTabCtrlST window

  11. class CTabCtrlST : public CTabCtrl
  12. {
  13. // Construction
  14. public:
  15.     CTabCtrlST();

  16. // Attributes
  17. public:
  18.     //改變背景色
  19.     void SetTabBk(COLORREF crColor);

  20. // Operations
  21. public:

  22. // Overrides
  23.     // ClassWizard generated virtual function overrides
  24.     //{{AFX_VIRTUAL(CTabCtrlST)
  25.     virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
  26.     afx_msg BOOL OnEraseBkgnd(CDC* pDC);
  27.     //}}AFX_VIRTUAL

  28. // Implementation
  29. public:
  30.     virtual ~CTabCtrlST();

  31.     // Generated message map functions
  32. protected:
  33.     //{{AFX_MSG(CTabCtrlST)
  34.     afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
  35.     //}}AFX_MSG
  36.     COLORREF m_colorBK;
       virtual void PreSubclassWindow();

  37.     DECLARE_MESSAGE_MAP()
  38. };

  39. /////////////////////////////////////////////////////////////////////////////

  40. //{{AFX_INSERT_LOCATION}}
  41. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.

  42. #endif // !defined(AFX_TABCTRLST_H__032460F5_8D49_4AB4_A7BF_1350F7EBE8C2__INCLUDED_)



  1. // TabCtrlST.cpp : implementation file///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    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. }
    78. void CTabCtrlST::PreSubclassWindow() 
      {
          // TODO: Add your specialized code here and/or call the base class
          ModifyStyle(BS_TYPEMASK, TCS_OWNERDRAWFIXED, SWP_FRAMECHANGED);
          CTabCtrl::PreSubclassWindow();
      }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章