EGCtrlBar.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 6k
Category:

Graph program

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include "EGctrlbar.h"
  3. CEGControlBar::CEGControlBar(void)
  4. {
  5. m_curHorzDrag = AfxGetApp()->LoadCursor(AFX_IDC_HSPLITBAR); // sometime fails .. 
  6. m_curVertDrag = AfxGetApp()->LoadCursor(AFX_IDC_VSPLITBAR); // sometime fails .. 
  7. m_bDragging = FALSE;
  8. }
  9. CEGControlBar::~CEGControlBar(void)
  10. {
  11. if( NULL != m_curHorzDrag )
  12. DestroyCursor( m_curHorzDrag );
  13. if( NULL != m_curVertDrag )
  14. DestroyCursor( m_curVertDrag );
  15. }
  16. BEGIN_MESSAGE_MAP(CEGControlBar, CControlBar)
  17. ON_WM_CREATE()
  18. ON_WM_LBUTTONDOWN()
  19. ON_WM_LBUTTONUP()
  20. ON_WM_MOUSEMOVE()
  21. ON_WM_SIZE()
  22. ON_WM_PAINT()
  23. ON_WM_SETCURSOR()
  24. ON_WM_SETFOCUS()
  25. END_MESSAGE_MAP()
  26. BOOL CEGControlBar::IsVertical() {
  27. DWORD dwStyle = GetBarStyle();
  28. return ( dwStyle & CBRS_TOP) == CBRS_TOP || ( dwStyle & CBRS_BOTTOM ) == CBRS_BOTTOM;
  29. }
  30. void CEGControlBar::SetDragCursor() {
  31. if ( IsVertical() ) {
  32. SetCursor( m_curVertDrag);
  33. } else {
  34. SetCursor( m_curHorzDrag);
  35. }
  36. }
  37. void CEGControlBar::GetInsideRect( CRect& rc ) {
  38. GetClientRect(rc);
  39. DWORD dwStyle = GetBarStyle();
  40. if ( (dwStyle & CBRS_LEFT ) == CBRS_LEFT ) {
  41. rc.right -= SPLITTER_SIZE;
  42. } else if ( (dwStyle & CBRS_RIGHT ) == CBRS_RIGHT ) {
  43. rc.left += SPLITTER_SIZE;
  44. } else if ( (dwStyle & CBRS_TOP ) == CBRS_TOP ) {
  45. rc.bottom -= SPLITTER_SIZE;
  46. } else {
  47. rc.top += SPLITTER_SIZE;
  48. }
  49. }
  50. BOOL CEGControlBar::Create( CWnd * pParent, int nSize, int iId )
  51. {
  52. if ( !CControlBar::Create(NULL, "", WS_VISIBLE|WS_CHILD, CRect(0,0,0,0), pParent, iId))
  53. return FALSE;
  54. SetOwner(pParent);
  55. m_iSize = nSize;
  56. return TRUE;
  57. }
  58. int CEGControlBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
  59. {
  60. if (CControlBar::OnCreate(lpCreateStruct) == -1) 
  61. return -1;
  62. SetAlign ( CBRS_BOTTOM );
  63. return 0;
  64. }
  65. void CEGControlBar::SetAlign( DWORD dwAlign ) {
  66. SetBarStyle ( dwAlign | CBRS_HIDE_INPLACE & ~(CBRS_BORDER_ANY | CBRS_GRIPPER));
  67. }
  68. void CEGControlBar::HidePane() {
  69. GetParentFrame()->ShowControlBar( this, FALSE, FALSE );
  70. }
  71. void CEGControlBar::ShowPane() {
  72. GetParentFrame()->ShowControlBar( this, TRUE, FALSE );
  73. }
  74. void CEGControlBar::ToggleVisible() {
  75. if ( IsVisible() ) {
  76. HidePane();
  77. } else {
  78. ShowPane();
  79. }
  80. }
  81. void CEGControlBar::OnPaint()
  82. {
  83. CPaintDC dc(this);
  84. CRect rc;
  85. dc.GetClipBox( &rc );
  86. dc.FillSolidRect(rc, GetSysColor(COLOR_3DFACE));
  87. GetInsideRect(rc);
  88. OnDraw( &dc, rc );
  89. }
  90. void CEGControlBar::OnDraw( CDC * /* pDC */, CRect& /* rc */ ) {
  91. }
  92. CSize CEGControlBar::CalcFixedLayout (BOOL /*bStretch*/, BOOL bHorz )
  93. {
  94. return CSize ( bHorz ? 32767 : m_iSize, bHorz ? m_iSize: 32767);
  95. }
  96. void CEGControlBar::OnLButtonDown(UINT nFlags, CPoint point)
  97. {
  98. if ( m_iSize <= 4 || m_dragRect.PtInRect(point) )
  99. {
  100. m_bDragging = TRUE;
  101. SetDragCursor();
  102. SetCapture();
  103. SetFocus();
  104. CRect rc;
  105. GetClientRect(rc);
  106. OnInvertTracker(m_dragRect);
  107. return;
  108. }
  109. CControlBar::OnLButtonDown(nFlags, point);
  110. }
  111. void CEGControlBar::OnLButtonUp(UINT nFlags, CPoint point)
  112. {
  113. if ( m_bDragging )
  114. {
  115. ReleaseCapture();
  116. OnInvertTracker(m_dragRect);
  117. DWORD dwStyle = GetBarStyle();
  118. int nMinHeight = GetSystemMetrics( SM_CYCAPTION ) + SPLITTER_SIZE*2;
  119. int nMinWidth = nMinHeight*4;
  120. if( ( dwStyle & CBRS_TOP ) == CBRS_TOP ) {
  121. m_iSize = point.y > nMinHeight ? point.y : nMinHeight;
  122. } else if( ( dwStyle & CBRS_BOTTOM ) == CBRS_BOTTOM ) {
  123. m_iSize =  m_iSize - point.y;
  124. if ( m_iSize < nMinHeight )
  125. m_iSize = nMinHeight;
  126. } else if( ( dwStyle & CBRS_RIGHT ) == CBRS_RIGHT ) {
  127. m_iSize = m_iSize - point.x;
  128. if ( m_iSize < nMinWidth )
  129. m_iSize = nMinWidth;
  130. } else {
  131. m_iSize = point.x > nMinWidth ? point.x : nMinWidth;
  132. }
  133. GetParentFrame()->RecalcLayout();
  134. }
  135. m_bDragging = FALSE;
  136. CControlBar::OnLButtonUp(nFlags, point);
  137. }
  138. void CEGControlBar::OnMouseMove(UINT nFlags, CPoint point)
  139. {
  140. if ( m_bDragging ) {
  141. CRect rc1( m_dragRect );
  142. if ( IsVertical() ) {
  143. m_dragRect.SetRect( rc1.left, point.y - 5, rc1.right, point.y );
  144. } else {
  145. m_dragRect.SetRect( point.x - 5, rc1.top, point.x, rc1.bottom);
  146. }
  147. if (rc1 != m_dragRect)
  148. {
  149. OnInvertTracker(rc1);
  150. OnInvertTracker(m_dragRect);
  151. }
  152. }
  153. CControlBar::OnMouseMove(nFlags, point);
  154. }
  155. BOOL CEGControlBar::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  156. {
  157. if (nHitTest == HTCLIENT) {
  158. CPoint point;
  159. ::GetCursorPos (&point);
  160. ScreenToClient (&point);
  161. if ( m_bDragging || m_dragRect.PtInRect(point) ) {
  162. SetDragCursor();
  163. return TRUE;
  164. }
  165. }
  166. return CControlBar::OnSetCursor(pWnd, nHitTest, message);
  167. }
  168. void CEGControlBar::OnInvertTracker(const CRect& rc)
  169. {
  170. CFrameWnd* pParentFrame = GetParentFrame ();
  171. CDC* pDC = pParentFrame->GetDC();
  172. CRect rect(rc);
  173.     ClientToScreen(rect);
  174. pParentFrame->ScreenToClient(rect);
  175. CBrush br;
  176. // br.CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
  177. // br.CreateSolidBrush( GetSysColor( COLOR_APPWORKSPACE ) );
  178. br.CreateHatchBrush( HS_DIAGCROSS, GetSysColor( COLOR_APPWORKSPACE ) );
  179. HBRUSH hOldBrush = NULL;
  180. hOldBrush = (HBRUSH)SelectObject(pDC->m_hDC, br.m_hObject);
  181. pDC->PatBlt( rect.left, rect.top, rect.Width(), rect.Height(), DSTINVERT );
  182. if (hOldBrush != NULL) SelectObject(pDC->m_hDC, hOldBrush);
  183. ReleaseDC(pDC);
  184. }
  185. void CEGControlBar::OnResize( CRect& /* rc */ ) {
  186. }
  187. void CEGControlBar::OnSize(UINT nType, int cx, int cy)
  188. {
  189. CControlBar::OnSize(nType, cx, cy);
  190. DWORD dwStyle = GetBarStyle();
  191. if ( (dwStyle & CBRS_LEFT ) == CBRS_LEFT ) {
  192. m_dragRect.SetRect( cx - SPLITTER_SIZE, 0, cx, cy);
  193. } else if ( (dwStyle & CBRS_RIGHT ) == CBRS_RIGHT ) {
  194. m_dragRect.SetRect( 0, 0, SPLITTER_SIZE, cy);
  195. } else if ( (dwStyle & CBRS_TOP ) == CBRS_TOP ) {
  196. m_dragRect.SetRect( 0, cy - SPLITTER_SIZE, cx, cy);
  197. } else {
  198. m_dragRect.SetRect( 0, 0, cx, SPLITTER_SIZE);
  199. }
  200. CRect rc;
  201. GetInsideRect( rc );
  202. OnResize ( rc );
  203. }