GridCellCheck.cpp
上传用户:jllc68
上传日期:2009-01-21
资源大小:402k
文件大小:5k
源码类别:

界面编程

开发平台:

Visual C++

  1. // GridCellCheck.cpp : implementation file
  2. //
  3. // MFC Grid Control - Main grid cell class
  4. //
  5. // Provides the implementation for a combobox cell type of the
  6. // grid control.
  7. //
  8. // Written by Chris Maunder <cmaunder@mail.com>
  9. // Copyright (c) 1998-2002. All Rights Reserved.
  10. //
  11. // Parts of the code contained in this file are based on the original
  12. // CInPlaceList from http://www.codeguru.com/listview
  13. //
  14. // This code may be used in compiled form in any way you desire. This
  15. // file may be redistributed unmodified by any means PROVIDING it is 
  16. // not sold for profit without the authors written consent, and 
  17. // providing that this notice and the authors name and all copyright 
  18. // notices remains intact. 
  19. //
  20. // An email letting me know how you are using it would be nice as well. 
  21. //
  22. // This file is provided "as is" with no expressed or implied warranty.
  23. // The author accepts no liability for any damage/loss of business that
  24. // this product may cause.
  25. //
  26. // For use with CGridCtrl v2.22+
  27. //
  28. // History:
  29. // 23 Jul 2001 - Complete rewrite
  30. // 13 Mar 2004 - GetCellExtent and GetCheckPlacement fixed by Yogurt
  31. //             - Read-only now honoured - Yogurt
  32. //
  33. /////////////////////////////////////////////////////////////////////////////
  34. #include "stdafx.h"
  35. #include "GridCell.h"
  36. #include "GridCtrl.h"
  37. #include "GridCellCheck.h"
  38. CGridCellCheck::CGridCellCheck() : CGridCell()
  39. {
  40.     m_bChecked = FALSE;
  41.     //m_Rect.IsRectNull();
  42. }
  43. CSize CGridCellCheck::GetCellExtent(CDC* pDC)
  44. {
  45.     // Using SM_CXHSCROLL as a guide to the size of the checkbox
  46.     int nWidth = GetSystemMetrics(SM_CXHSCROLL) + 2*GetMargin();
  47.     CSize cellSize = CGridCell::GetCellExtent(pDC);
  48.     cellSize.cx += nWidth;
  49.     cellSize.cy = max (cellSize.cy, nWidth);
  50.     return  cellSize;
  51. }
  52. // i/o:  i=dims of cell rect; o=dims of text rect
  53. BOOL CGridCellCheck::GetTextRect( LPRECT pRect)
  54. {
  55.     BOOL bResult = CGridCell::GetTextRect(pRect);
  56.     if (bResult)
  57.     {
  58.         int nWidth = GetSystemMetrics(SM_CXHSCROLL) + 2*GetMargin();
  59.         pRect->left += nWidth;
  60.         if (pRect->left > pRect->right)
  61.             pRect->left = pRect->right;
  62.     }
  63.     return bResult;
  64. }
  65. // Override draw so that when the cell is selected, a drop arrow is shown in the RHS.
  66. BOOL CGridCellCheck::Draw(CDC* pDC, int nRow, int nCol, CRect rect,  BOOL bEraseBkgnd /*=TRUE*/)
  67. {
  68.     BOOL bResult = CGridCell::Draw(pDC, nRow, nCol, rect,  bEraseBkgnd);
  69. #ifndef _WIN32_WCE
  70.     // Store the cell's dimensions for later
  71.     m_Rect = rect;
  72.     CRect CheckRect = GetCheckPlacement();
  73.     rect.left = CheckRect.right;
  74.     // enough room to draw?
  75.     // if (CheckRect.Width() < rect.Width() && CheckRect.Height() < rect.Height()) {
  76.     // Do the draw 
  77.     pDC->DrawFrameControl(GetCheckPlacement(), DFC_BUTTON, 
  78.     (m_bChecked)? DFCS_BUTTONCHECK | DFCS_CHECKED : DFCS_BUTTONCHECK);
  79.     // }
  80. #endif
  81.     return bResult;
  82. }
  83. void CGridCellCheck::OnClick(CPoint PointCellRelative)
  84. {
  85. // PointCellRelative is relative to the topleft of the cell. Convert to client coords
  86. PointCellRelative += m_Rect.TopLeft();
  87.     // Bail if cell is read-only
  88.     CCellID cell = GetGrid()->GetCellFromPt(PointCellRelative);
  89.     if (!GetGrid()->IsCellEditable(cell))
  90.         return;
  91. // GetCheckPlacement returns the checkbox dimensions in client coords. Only check/
  92. // uncheck if the user clicked in the box
  93. if (GetCheckPlacement().PtInRect(PointCellRelative))
  94. {
  95. m_bChecked = !m_bChecked;
  96. GetGrid()->InvalidateRect(m_Rect);
  97. }
  98. }
  99. //////////////////////////////////////////////////////////////////////
  100. // Operations
  101. //////////////////////////////////////////////////////////////////////
  102. BOOL CGridCellCheck::SetCheck(BOOL bChecked /*=TRUE*/)
  103. {
  104. BOOL bTemp = m_bChecked;
  105. m_bChecked = bChecked;
  106. if (!m_Rect.IsRectEmpty())
  107. GetGrid()->InvalidateRect(m_Rect);
  108. return bTemp;
  109. }
  110. BOOL CGridCellCheck::GetCheck()
  111. {
  112. return m_bChecked;
  113. }
  114. //////////////////////////////////////////////////////////////////////
  115. // Protected implementation
  116. //////////////////////////////////////////////////////////////////////
  117. // Returns the dimensions and placement of the checkbox in client coords.
  118. CRect CGridCellCheck::GetCheckPlacement()
  119. {
  120. int nWidth = GetSystemMetrics(SM_CXHSCROLL);
  121. CRect place = m_Rect + CSize(GetMargin(), GetMargin());
  122.     place.right = place.left + nWidth;
  123.     place.bottom = place.top + nWidth;
  124. /* for centering
  125. int nDiff = (place.Width() - nWidth)/2;
  126. if (nDiff > 0)
  127. {
  128. place.left += nDiff;
  129. place.right = place.left + nWidth;
  130. }
  131. nDiff = (place.Height() - nWidth)/2;
  132. if (nDiff > 0)
  133. {
  134. place.top += nDiff;
  135. place.bottom = place.top + nWidth;
  136. }
  137.     */
  138.     if (m_Rect.Height() < nWidth + 2 * static_cast<int> (GetMargin())) 
  139.     {
  140.         place.top = m_Rect.top + (m_Rect.Height() - nWidth) / 2;     
  141.         place.bottom = place.top + nWidth;
  142.     }
  143. return place;
  144. }