SplitterBar.cpp
上传用户:wymy58
上传日期:2007-01-07
资源大小:2086k
文件大小:6k
源码类别:

DirextX编程

开发平台:

Visual C++

  1. // CSplitterBar.cpp : implementation file
  2. //
  3. /////////////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "PFM.h"
  6. #include "SplitterBar.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CSplitterBar
  14. IMPLEMENT_DYNAMIC(CSplitterBar, CWnd)
  15. BEGIN_MESSAGE_MAP(CSplitterBar, CWnd)
  16. //{{AFX_MSG_MAP(CSplitterBar)
  17. ON_WM_PAINT()
  18. ON_WM_NCHITTEST()
  19. ON_WM_CREATE()
  20. ON_WM_SETCURSOR()
  21. ON_WM_MOUSEMOVE()
  22. ON_WM_LBUTTONDOWN()
  23. ON_WM_LBUTTONUP()
  24. //}}AFX_MSG_MAP
  25. END_MESSAGE_MAP()
  26. CSplitterBar::CSplitterBar()
  27. {
  28. m_bDragging=FALSE;
  29. m_pwndLeftPane=m_pwndRightPane=NULL;
  30. }
  31. CSplitterBar::~CSplitterBar()
  32. {
  33. }
  34. BOOL CSplitterBar::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
  35. {
  36. CWnd* pWnd = this;
  37. return pWnd->Create(NULL, "", dwStyle, rect, pParentWnd, nID);
  38. }
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CSplitterBar message handlers
  41. void CSplitterBar::OnPaint() 
  42. {
  43. RECT rc;
  44. if (!GetUpdateRect(&rc)) return;
  45. PAINTSTRUCT paint;
  46. CDC *pDC = BeginPaint(&paint);
  47. CRect rect;
  48. GetClientRect(rect);
  49. pDC->Draw3dRect(&rect,
  50.   ::GetSysColor(COLOR_BTNHIGHLIGHT),
  51.   ::GetSysColor(COLOR_BTNSHADOW));
  52. EndPaint(&paint);
  53. }
  54. UINT CSplitterBar::OnNcHitTest(CPoint point) 
  55. {
  56. return HTCLIENT;
  57. }
  58. int CSplitterBar::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  59. {
  60. if (CWnd::OnCreate(lpCreateStruct) == -1)
  61. return -1;
  62. GetWindowRect(&m_rectSplitter);
  63. SetWindowPos(&CWnd::wndTop,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
  64. //Initialize left most and right most coordinator
  65. CRect rectParent;
  66. GetParent()->GetClientRect(rectParent);
  67. m_rectPosition = rectParent;
  68. return 0;
  69. }
  70. BOOL CSplitterBar::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
  71. {
  72. CPoint ptCursor=GetMessagePos();
  73. if(IsCursorOverSplitter(ptCursor))
  74. {
  75. ::SetCursor(::LoadCursor(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_CURWE)));
  76. return TRUE;
  77. }
  78. return CWnd::OnSetCursor(pWnd, nHitTest, message);
  79. }
  80. BOOL CSplitterBar::IsCursorOverSplitter( const CPoint& ptCursor )
  81. {
  82. CRect rectSplitter;
  83. GetWindowRect(rectSplitter);
  84. return rectSplitter.PtInRect( ptCursor );
  85. }
  86. void CSplitterBar::OnMouseMove(UINT nFlags, CPoint point) 
  87. {
  88. if(nFlags & MK_LBUTTON && m_bDragging)
  89. {
  90. DrawDraggingBar(point);
  91. return;
  92. }
  93. CWnd::OnMouseMove(nFlags, point);
  94. }
  95. void CSplitterBar::OnLButtonDown(UINT nFlags, CPoint point) 
  96. {
  97. ClientToScreen(&point);
  98. if(IsCursorOverSplitter(point))
  99. {
  100. SetCapture();
  101. m_bDragging=TRUE;
  102. GetWindowRect(&m_rectSplitter);
  103. ScreenToClient(&point);
  104. DrawDraggingBar(point,DRAG_ENTER);
  105. return;
  106. }
  107. CWnd::OnLButtonDown(nFlags, point);
  108. }
  109. void CSplitterBar::OnLButtonUp(UINT nFlags, CPoint point) 
  110. {
  111. if(m_bDragging)
  112. {
  113. DrawDraggingBar(point,DRAG_EXIT);
  114. //Move the splitter here
  115. ClientToScreen(&point);
  116. //with in client area?
  117. CPoint pointLeftMost;
  118. pointLeftMost.x=m_rectPosition.left;
  119. GetParent()->ClientToScreen(&pointLeftMost);
  120. CPoint pointRightMost;
  121. pointRightMost.x=m_rectPosition.right;
  122. GetParent()->ClientToScreen(&pointRightMost);
  123. if(point.x < pointLeftMost.x)
  124. point.x=pointLeftMost.x;
  125. if(point.x > pointRightMost.x)
  126. point.x=pointRightMost.x;
  127. m_rectDragCurt=m_rectSplitter;
  128. m_rectDragCurt.left=point.x;
  129. m_rectDragCurt.right=point.x+m_rectSplitter.Width();
  130. GetParent()->ScreenToClient(m_rectDragCurt);
  131. MoveWindow(m_rectDragCurt,TRUE);
  132. OnPaint();
  133. ReleaseCapture();
  134. m_bDragging=FALSE;
  135. MovePanes();
  136. GetParent()->SendMessage(WM_SPLITTER_MOVED,0,0L);
  137. }
  138. CWnd::OnLButtonUp(nFlags, point);
  139. }
  140. void CSplitterBar::DrawDraggingBar(CPoint point,DRAGFLAG df)
  141. {
  142. ClientToScreen(&point);
  143. m_rectDragCurt=m_rectSplitter;
  144. m_rectDragCurt.left=point.x;
  145. m_rectDragCurt.right=point.x+m_rectSplitter.Width();
  146. CSize size(m_rectDragCurt.Width(),m_rectDragCurt.Height());
  147. CWnd *pParentWnd=GetParent();
  148. ASSERT(pParentWnd);
  149. CDC *pDC=pParentWnd->GetDC();
  150. pParentWnd->ScreenToClient(m_rectDragCurt);
  151. switch(df)
  152. {
  153. case DRAG_ENTER:
  154.  pDC->DrawDragRect(m_rectDragCurt,size,NULL,size);
  155.  break;
  156. case DRAG_EXIT: //fall through
  157. default:
  158.  pDC->DrawDragRect(m_rectDragCurt,size,m_rectDragPrev,size);
  159.  break;
  160. }
  161. pParentWnd->ReleaseDC(pDC);
  162. m_rectDragPrev=m_rectDragCurt;
  163. }
  164. void CSplitterBar::SetPanes(CWnd *pwndLeftPane,CWnd *pwndRightPane)
  165. {
  166. ASSERT(pwndLeftPane);
  167. ASSERT(pwndRightPane);
  168. m_pwndLeftPane=pwndLeftPane;
  169. m_pwndRightPane=pwndRightPane;
  170. //Initialize splitter bar position & size
  171. CRect rectBar = m_rectPosition;
  172. rectBar.right = rectBar.right>>1;
  173. rectBar.left = rectBar.right;
  174. int nBarWidth=GetSystemMetrics(SM_CYFRAME);
  175. rectBar.left-=nBarWidth;
  176. rectBar.right+=nBarWidth;
  177. MoveWindow(rectBar);
  178. //repostion left & rigth panes
  179. MovePanes();
  180. }
  181. void CSplitterBar::MovePanes()
  182. {
  183. ASSERT(m_pwndLeftPane);
  184. ASSERT(m_pwndRightPane);
  185. //Get position of the splitter bar
  186. CRect rectBar;
  187. GetWindowRect(rectBar);
  188. GetParent()->ScreenToClient(rectBar);
  189. //reposition left pane
  190. CRect rectLeft(m_rectPosition.left, rectBar.top, rectBar.left, rectBar.bottom);
  191. m_pwndLeftPane->MoveWindow(rectLeft);
  192. //reposition right pane
  193. CRect rectRight(rectBar.right, rectBar.top, m_rectPosition.right, rectBar.bottom);
  194. m_pwndRightPane->MoveWindow(rectRight);
  195. //repaint client area
  196. GetParent()->Invalidate();
  197. }
  198. VOID CSplitterBar::SetPosition(LPCRECT lpRect)
  199. {
  200. m_rectPosition = *lpRect;
  201. CRect rectBar = m_rectPosition;
  202. rectBar.right = rectBar.right>>1;
  203. rectBar.left = rectBar.right;
  204. int nBarWidth=GetSystemMetrics(SM_CYFRAME);
  205. rectBar.left-=nBarWidth/2;
  206. rectBar.right+=nBarWidth/2;
  207. MoveWindow(rectBar);
  208. // m_rectPosition = rectBar;
  209. MovePanes();
  210. }