tabcontrol 自绘

来源:互联网 发布:淘宝用卷怎么用 编辑:程序博客网 时间:2024/06/06 01:08

//头文件

[cpp] view plain copy
  1. #pragma once  
  2. // OwnerdrawTabCtrl.h : header file  
  3.   
  4. /////////////////////////////////////////////////////////////////////////////  
  5. // COwnerdrawTabCtrl window  
  6.   
  7. class COwnerdrawTabCtrl : public CTabCtrl  
  8. {  
  9. // Construction  
  10. public:  
  11.     COwnerdrawTabCtrl();  
  12.   
  13. // Attributes  
  14. public:  
  15.   
  16. // Operations  
  17. public:  
  18.   
  19. // Overrides  
  20.     // ClassWizard generated virtual function overrides  
  21.     //{{AFX_VIRTUAL(COwnerdrawTabCtrl)  
  22.     public:  
  23.     virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);  
  24.     protected:  
  25.     virtual void PreSubclassWindow();  
  26.     //}}AFX_VIRTUAL  
  27.   
  28. // Implementation  
  29. public:  
  30.     virtual ~COwnerdrawTabCtrl();  
  31.   
  32.     // Generated message map functions  
  33. protected:  
  34.     //{{AFX_MSG(COwnerdrawTabCtrl)  
  35.     //}}AFX_MSG  
  36.   
  37.   void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);  
  38.   
  39.     DECLARE_MESSAGE_MAP()  
  40. };  

 

//CPP实现

[cpp] view plain copy
  1. // OwnerdrawTabCtrl.h : implementation file  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "OwnerdrawTabCtrl.h"  
  6.   
  7. #ifdef _DEBUG  
  8. #define new DEBUG_NEW  
  9. #undef THIS_FILE  
  10. static char THIS_FILE[] = __FILE__;  
  11. #endif  
  12.   
  13. /////////////////////////////////////////////////////////////////////////////  
  14. // COwnerdrawTabCtrl  
  15.   
  16. COwnerdrawTabCtrl::COwnerdrawTabCtrl()  
  17. {  
  18. }  
  19.   
  20. COwnerdrawTabCtrl::~COwnerdrawTabCtrl()  
  21. {  
  22. }  
  23.   
  24.   
  25. BEGIN_MESSAGE_MAP(COwnerdrawTabCtrl, CTabCtrl)  
  26.     //{{AFX_MSG_MAP(COwnerdrawTabCtrl)  
  27.     //}}AFX_MSG_MAP  
  28. END_MESSAGE_MAP()  
  29.   
  30. /////////////////////////////////////////////////////////////////////////////  
  31. // COwnerdrawTabCtrl message handlers  
  32.   
  33. void COwnerdrawTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)   
  34. {  
  35.     // TODO: Add your message handler code here and/or call default   
  36.     
  37.   if(lpDrawItemStruct->CtlType == ODT_TAB)  
  38.   {   
  39.     CRect rect = lpDrawItemStruct->rcItem;  
  40.     INT nTabIndex = lpDrawItemStruct->itemID;  
  41.     if (nTabIndex < 0) return;  
  42.       
  43.     TCHAR label[64];  
  44.     TC_ITEM tci;  
  45.     tci.mask = TCIF_TEXT|TCIF_IMAGE;  
  46.     tci.pszText = label;       
  47.     tci.cchTextMax = 63;          
  48.     GetItem(nTabIndex, &tci );  
  49.       
  50.     CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);  
  51.     if (!pDC) return;  
  52.     int nSavedDC = pDC->SaveDC();  
  53.       
  54.     //填充背景色  
  55.     COLORREF rcBack;  
  56.     if (lpDrawItemStruct->itemState & CDIS_SELECTED  )   
  57.     {  
  58.       rcBack = RGB(255, 0, 0);  
  59.     }  
  60.     else if(lpDrawItemStruct->itemState & (CDIS_DISABLED | CDIS_GRAYED) )   
  61.     {  
  62.       rcBack = RGB(0, 255, 0);  
  63.     }  
  64.     else  
  65.     {  
  66.       rcBack = GetSysColor(COLOR_BTNFACE);  
  67.     }      
  68.     pDC->FillSolidRect(rect, rcBack);  
  69.        
  70.     rect.top += ::GetSystemMetrics(SM_CYEDGE);  
  71.       
  72.     pDC->SetBkMode(TRANSPARENT);  
  73.       
  74.     //绘制图片  
  75.     CImageList* pImageList = GetImageList();  
  76.     if (pImageList && tci.iImage >= 0)   
  77.     {  
  78.       rect.left += pDC->GetTextExtent(_T(" ")).cx;       // Margin  
  79.         
  80.       // Get height of image so we   
  81.       IMAGEINFO info;  
  82.       pImageList->GetImageInfo(tci.iImage, &info);  
  83.       CRect ImageRect(info.rcImage);  
  84.       INT nYpos = rect.top;  
  85.         
  86.       pImageList->Draw(pDC, tci.iImage, CPoint(rect.left, nYpos), ILD_TRANSPARENT);  
  87.       rect.left += ImageRect.Width();  
  88.     }  
  89.       
  90.     //绘制字体  
  91.     COLORREF txtColor;  
  92.     if (lpDrawItemStruct->itemState & CDIS_SELECTED  )   
  93.     {  
  94.       rect.top -= ::GetSystemMetrics(SM_CYEDGE);  
  95.         
  96.       txtColor = RGB(0,255,0);  
  97.     }  
  98.     else if(lpDrawItemStruct->itemState & (CDIS_DISABLED | CDIS_GRAYED) )   
  99.     {  
  100.       txtColor = RGB(128, 128, 128);        
  101.     }  
  102.     else  
  103.     {  
  104.       txtColor = GetSysColor(COLOR_WINDOWTEXT);  
  105.     }      
  106.     pDC->SetTextColor(txtColor);  
  107.     pDC->DrawText(label, rect, DT_SINGLELINE|DT_VCENTER|DT_CENTER);  
  108.       
  109.     pDC->RestoreDC(nSavedDC);  
  110.       
  111.   }  
  112.   
  113. }  
  114.   
  115. void COwnerdrawTabCtrl::PreSubclassWindow()   
  116. {  
  117.     ModifyStyle(0, TCS_OWNERDRAWFIXED);  
  118.     CTabCtrl::PreSubclassWindow();  
  119. }  
  120.   
  121. BOOL COwnerdrawTabCtrl::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)   
  122. {  
  123.     // TODO: Add your specialized code here and/or call the base class  
  124.     dwStyle |= TCS_OWNERDRAWFIXED;  
  125.     return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);  
  126. }  
0 0