OAMTABCTRL.CPP
上传用户:lvjun8202
上传日期:2013-04-30
资源大小:797k
文件大小:6k
源码类别:

SNMP编程

开发平台:

C/C++

  1. // OAMTabCtrl.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "OAM.h"
  5. #include "OAMTabCtrl.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // COAMTabCtrl
  13. extern HBITMAP hBkBitmap;
  14. extern COLORREF _clrText;
  15. COAMTabCtrl::COAMTabCtrl()
  16. {
  17. m_crSelColour     = RGB(0,0,255);
  18. m_crUnselColour   = _clrText;
  19. }
  20. COAMTabCtrl::~COAMTabCtrl()
  21. {
  22. if(m_SelFont.GetSafeHandle())
  23. m_SelFont.DeleteObject();
  24. if(m_UnselFont.GetSafeHandle())
  25. m_UnselFont.DeleteObject();
  26. }
  27. BEGIN_MESSAGE_MAP(COAMTabCtrl, CTabCtrl)
  28. //{{AFX_MSG_MAP(COAMTabCtrl)
  29. ON_WM_ERASEBKGND()
  30. ON_WM_SIZE()
  31. //}}AFX_MSG_MAP
  32. END_MESSAGE_MAP()
  33. /////////////////////////////////////////////////////////////////////////////
  34. // COAMTabCtrl message handlers
  35. BOOL COAMTabCtrl::OnEraseBkgnd(CDC* pDC) 
  36. {
  37. // TODO: Add your message handler code here and/or call default
  38. CRect rcChild;
  39. GetClientRect(&rcChild);
  40. rcChild.DeflateRect(3,3);
  41. rcChild.bottom -= 21;
  42. pDC->ExcludeClipRect(rcChild);
  43. int result;// = CControlBar::OnEraseBkgnd(pDC);
  44. if(hBkBitmap)
  45. {
  46. CRect rectClient;
  47. GetClientRect(rectClient);
  48. rectClient.top = rectClient.bottom - 21;
  49. CBitmap * bp = CBitmap::FromHandle(hBkBitmap);
  50. pDC->FillRect(rectClient,&CBrush(bp));
  51. result = TRUE;
  52. }
  53. else
  54. {
  55. result = CTabCtrl::OnEraseBkgnd(pDC);
  56. }
  57. return result;//CTabCtrl::OnEraseBkgnd(pDC);
  58. }
  59. void COAMTabCtrl::PreSubclassWindow() 
  60. {
  61. // TODO: Add your specialized code here and/or call the base class
  62. CTabCtrl::PreSubclassWindow();
  63. // ModifyStyle(0, TCS_OWNERDRAWFIXED);
  64. }
  65. BOOL COAMTabCtrl::PreCreateWindow(CREATESTRUCT& cs) 
  66. {
  67. // TODO: Add your specialized code here and/or call the base class
  68. // cs.style |= TCS_OWNERDRAWFIXED;
  69. return CTabCtrl::PreCreateWindow(cs);
  70. }
  71. void COAMTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  72. {
  73. CRect rect = lpDrawItemStruct->rcItem;
  74. int nTabIndex = lpDrawItemStruct->itemID;
  75. if (nTabIndex < 0) return;
  76. BOOL bSelected = (nTabIndex == GetCurSel());
  77. char label[64];
  78. TC_ITEM tci;
  79. tci.mask = TCIF_TEXT|TCIF_IMAGE;
  80. tci.pszText = label;     
  81. tci.cchTextMax = 63;    
  82. if (!GetItem(nTabIndex, &tci )) return;
  83. CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  84. if (!pDC) return;
  85. int nSavedDC = pDC->SaveDC();
  86. // For some bizarre reason the rcItem you get extends above the actual
  87. // drawing area. We have to workaround this "feature".
  88. int nTopOffset;
  89. if(GetStyle() & TCS_BOTTOM)
  90. nTopOffset = ::GetSystemMetrics(SM_CYEDGE);
  91. else
  92. nTopOffset = -::GetSystemMetrics(SM_CYEDGE);
  93. rect.top -= nTopOffset;
  94. pDC->SetBkMode(TRANSPARENT);
  95. if(hBkBitmap)
  96. {
  97. CBitmap * bp = CBitmap::FromHandle(hBkBitmap);
  98. pDC->FillRect(rect,&CBrush(bp));
  99. }
  100. else
  101. pDC->FillSolidRect(rect, ::GetSysColor(COLOR_BTNFACE));
  102. // Draw image
  103. CImageList* pImageList = GetImageList();
  104. if (pImageList && tci.iImage >= 0) {
  105. rect.left += pDC->GetTextExtent(_T(" ")).cx; // Margin
  106. // Get height of image so we 
  107. IMAGEINFO info;
  108. pImageList->GetImageInfo(tci.iImage, &info);
  109. CRect ImageRect(info.rcImage);
  110. int nYpos = rect.top+nTopOffset;
  111. pImageList->Draw(pDC, tci.iImage, CPoint(rect.left, nYpos), ILD_TRANSPARENT);
  112. rect.left += ImageRect.Width();
  113. }
  114. if (bSelected) {
  115. pDC->SetTextColor(m_crSelColour);
  116. pDC->SelectObject(&m_SelFont);
  117. rect.top += nTopOffset;
  118. pDC->DrawText(label, rect, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
  119. } else {
  120. pDC->SetTextColor(m_crUnselColour);
  121. pDC->SelectObject(&m_UnselFont);
  122. pDC->DrawText(label, rect, DT_SINGLELINE|DT_BOTTOM|DT_CENTER);
  123. }
  124. pDC->RestoreDC(nSavedDC);
  125. }
  126. void COAMTabCtrl::SetColours(COLORREF bSelColour, COLORREF bUnselColour)
  127. {
  128. m_crSelColour = bSelColour;
  129. m_crUnselColour = bUnselColour;
  130. Invalidate();
  131. }
  132. void COAMTabCtrl::SetFonts(CFont* pSelFont, CFont* pUnselFont)
  133. {
  134. // ASSERT(pSelFont && pUnselFont);
  135. LOGFONT lFont;
  136. int nSelHeight, nUnselHeight;
  137. if(m_SelFont.GetSafeHandle())
  138. m_SelFont.DeleteObject();
  139. if(m_UnselFont.GetSafeHandle())
  140. m_UnselFont.DeleteObject();
  141. pSelFont->GetLogFont(&lFont);
  142. m_SelFont.CreateFontIndirect(&lFont);
  143. nSelHeight = lFont.lfHeight;
  144. pUnselFont->GetLogFont(&lFont);
  145. m_UnselFont.CreateFontIndirect(&lFont);
  146. nUnselHeight = lFont.lfHeight;
  147. SetFont( (nSelHeight > nUnselHeight)? &m_SelFont : &m_UnselFont);
  148. }
  149. void COAMTabCtrl::SetFonts(int nSelWeight,   BOOL bSelItalic,   BOOL bSelUnderline,
  150.   int nUnselWeight, BOOL bUnselItalic, BOOL bUnselUnderline)
  151. {
  152. // Free any memory currently used by the fonts.
  153. if(m_SelFont.GetSafeHandle())
  154. m_SelFont.DeleteObject();
  155. if(m_UnselFont.GetSafeHandle())
  156. m_UnselFont.DeleteObject();
  157. // Get the current font
  158. LOGFONT lFont;
  159. CFont *pFont = GetFont();
  160. if (pFont)
  161. pFont->GetLogFont(&lFont);
  162. else {
  163. NONCLIENTMETRICS ncm;
  164. ncm.cbSize = sizeof(NONCLIENTMETRICS);
  165. VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0));
  166. lFont = ncm.lfMessageFont; 
  167. }
  168. // Create the "Selected" font
  169. lFont.lfWeight = nSelWeight;
  170. lFont.lfItalic = bSelItalic;
  171. lFont.lfUnderline = bSelUnderline;
  172. m_SelFont.CreateFontIndirect(&lFont);
  173. // Create the "Unselected" font
  174. lFont.lfWeight = nUnselWeight;
  175. lFont.lfItalic = bUnselItalic;
  176. lFont.lfUnderline = bUnselUnderline;
  177. m_UnselFont.CreateFontIndirect(&lFont);
  178. SetFont( (nSelWeight > nUnselWeight)? &m_SelFont : &m_UnselFont);
  179. }
  180. void COAMTabCtrl::OnSize(UINT nType, int cx, int cy) 
  181. {
  182. CTabCtrl::OnSize(nType, cx, cy);
  183. // TODO: Add your message handler code here
  184. // ResizeTabs();
  185. }
  186. void COAMTabCtrl::ResizeTabs()
  187. {
  188. CRect rc;
  189. GetClientRect(rc);
  190. int iNumWin = GetItemCount();
  191. int x = 140; // def item width
  192. if ((x+4) * iNumWin > rc.Width()) x = (rc.Width()) / iNumWin - 4;
  193. CRect rcEx;
  194. GetItemRect(0, rcEx); 
  195. SetItemSize(CSize(x,rcEx.Height()));
  196. }