TabCtrlEx.cpp
上传用户:xiulingzi
上传日期:2007-01-01
资源大小:3k
文件大小:4k
源码类别:

Tab控件

开发平台:

Visual C++

  1. // TabCtrlEx.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "TabCtrlEx.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CTabCtrlEx
  12. CTabCtrlEx::CTabCtrlEx()
  13. {
  14. m_crSelColour     = RGB(0,0,255);
  15. m_crUnselColour   = RGB(50,50,50);
  16. }
  17. CTabCtrlEx::~CTabCtrlEx()
  18. {
  19. m_SelFont.DeleteObject();
  20. m_UnselFont.DeleteObject();
  21. }
  22. BEGIN_MESSAGE_MAP(CTabCtrlEx, CTabCtrl)
  23. //{{AFX_MSG_MAP(CTabCtrlEx)
  24. ON_WM_CREATE()
  25. //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CTabCtrlEx message handlers
  29. int CTabCtrlEx::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  30. {
  31. if (CTabCtrl::OnCreate(lpCreateStruct) == -1) return -1;
  32. ModifyStyle(0, TCS_OWNERDRAWFIXED);
  33. return 0;
  34. }
  35. void CTabCtrlEx::PreSubclassWindow() 
  36. {
  37. CTabCtrl::PreSubclassWindow();
  38. ModifyStyle(0, TCS_OWNERDRAWFIXED);
  39. }
  40. void CTabCtrlEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  41. {
  42. CRect rect = lpDrawItemStruct->rcItem;
  43. int nTabIndex = lpDrawItemStruct->itemID;
  44. if (nTabIndex < 0) return;
  45. BOOL bSelected = (nTabIndex == GetCurSel());
  46. char label[64];
  47. TC_ITEM tci;
  48. tci.mask = TCIF_TEXT|TCIF_IMAGE;
  49. tci.pszText = label;     
  50. tci.cchTextMax = 63;    
  51. if (!GetItem(nTabIndex, &tci )) return;
  52. CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  53. if (!pDC) return;
  54. int nSavedDC = pDC->SaveDC();
  55. // For some bizarre reason the rcItem you get extends above the actual
  56. // drawing area. We have to workaround this "feature".
  57. rect.top += ::GetSystemMetrics(SM_CYEDGE);
  58. pDC->SetBkMode(TRANSPARENT);
  59. pDC->FillSolidRect(rect, ::GetSysColor(COLOR_BTNFACE));
  60. // Draw image
  61. CImageList* pImageList = GetImageList();
  62. if (pImageList && tci.iImage >= 0) {
  63. rect.left += pDC->GetTextExtent(_T(" ")).cx; // Margin
  64. // Get height of image so we 
  65. IMAGEINFO info;
  66. pImageList->GetImageInfo(tci.iImage, &info);
  67. CRect ImageRect(info.rcImage);
  68. int nYpos = rect.top;
  69. pImageList->Draw(pDC, tci.iImage, CPoint(rect.left, nYpos), ILD_TRANSPARENT);
  70. rect.left += ImageRect.Width();
  71. }
  72. if (bSelected) {
  73. pDC->SetTextColor(m_crSelColour);
  74. pDC->SelectObject(&m_SelFont);
  75. rect.top -= ::GetSystemMetrics(SM_CYEDGE);
  76. pDC->DrawText(label, rect, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
  77. } else {
  78. pDC->SetTextColor(m_crUnselColour);
  79. pDC->SelectObject(&m_UnselFont);
  80. pDC->DrawText(label, rect, DT_SINGLELINE|DT_BOTTOM|DT_CENTER);
  81. }
  82. pDC->RestoreDC(nSavedDC);
  83. }
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CTabCtrlEx operations
  86. void CTabCtrlEx::SetColours(COLORREF bSelColour, COLORREF bUnselColour)
  87. {
  88. m_crSelColour = bSelColour;
  89. m_crUnselColour = bUnselColour;
  90. Invalidate();
  91. }
  92. void CTabCtrlEx::SetFonts(CFont* pSelFont, CFont* pUnselFont)
  93. {
  94. ASSERT(pSelFont && pUnselFont);
  95. LOGFONT lFont;
  96. int nSelHeight, nUnselHeight;
  97. m_SelFont.DeleteObject();
  98. m_UnselFont.DeleteObject();
  99. pSelFont->GetLogFont(&lFont);
  100. m_SelFont.CreateFontIndirect(&lFont);
  101. nSelHeight = lFont.lfHeight;
  102. pUnselFont->GetLogFont(&lFont);
  103. m_UnselFont.CreateFontIndirect(&lFont);
  104. nUnselHeight = lFont.lfHeight;
  105. SetFont( (nSelHeight > nUnselHeight)? &m_SelFont : &m_UnselFont);
  106. }
  107. void CTabCtrlEx::SetFonts(int nSelWeight,   BOOL bSelItalic,   BOOL bSelUnderline,
  108.   int nUnselWeight, BOOL bUnselItalic, BOOL bUnselUnderline)
  109. {
  110. // Free any memory currently used by the fonts.
  111. m_SelFont.DeleteObject();
  112. m_UnselFont.DeleteObject();
  113. // Get the current font
  114. LOGFONT lFont;
  115. CFont *pFont = GetFont();
  116. if (pFont)
  117. pFont->GetLogFont(&lFont);
  118. else {
  119. NONCLIENTMETRICS ncm;
  120. ncm.cbSize = sizeof(NONCLIENTMETRICS);
  121. VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0));
  122. lFont = ncm.lfMessageFont; 
  123. }
  124. // Create the "Selected" font
  125. lFont.lfWeight = nSelWeight;
  126. lFont.lfItalic = bSelItalic;
  127. lFont.lfUnderline = bSelUnderline;
  128. m_SelFont.CreateFontIndirect(&lFont);
  129. // Create the "Unselected" font
  130. lFont.lfWeight = nUnselWeight;
  131. lFont.lfItalic = bUnselItalic;
  132. lFont.lfUnderline = bUnselUnderline;
  133. m_UnselFont.CreateFontIndirect(&lFont);
  134. SetFont( (nSelWeight > nUnselWeight)? &m_SelFont : &m_UnselFont);
  135. }