HDrawPView.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // HDrawPView.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "HDraw.h"
  5. #include "HDrawPView.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CHDrawPView
  13. IMPLEMENT_DYNCREATE(CHDrawPView, CScrollView)
  14. CHDrawPView::CHDrawPView()
  15. {
  16. }
  17. CHDrawPView::~CHDrawPView()
  18. {
  19. }
  20. BEGIN_MESSAGE_MAP(CHDrawPView, CScrollView)
  21. //{{AFX_MSG_MAP(CHDrawPView)
  22. ON_WM_CREATE()
  23. ON_WM_SETCURSOR()
  24. ON_WM_LBUTTONDOWN()
  25. //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CHDrawPView drawing
  29. void CHDrawPView::OnInitialUpdate()
  30. {
  31. CScrollView::OnInitialUpdate();
  32. CSize sizeTotal;
  33. // TODO: calculate the total size of this view
  34. sizeTotal.cx = sizeTotal.cy = 100;
  35. SetScrollSizes(MM_TEXT, sizeTotal);
  36. m_tracker.m_rect.SetRect(0,0,GetDocument()->m_cavasW, GetDocument()->m_cavasH);
  37. m_drawView->MoveWindow(m_tracker.m_rect);
  38. }
  39. void CHDrawPView::OnDraw(CDC* pDC)
  40. {
  41. CDocument* pDoc = GetDocument();
  42. // TODO: add draw code here
  43. m_tracker.Draw(pDC);
  44. }
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CHDrawPView diagnostics
  47. #ifdef _DEBUG
  48. void CHDrawPView::AssertValid() const
  49. {
  50. CScrollView::AssertValid();
  51. }
  52. void CHDrawPView::Dump(CDumpContext& dc) const
  53. {
  54. CScrollView::Dump(dc);
  55. }
  56. #endif //_DEBUG
  57. CHDrawDoc* CHDrawPView::GetDocument() // non-debug version is inline
  58. {
  59. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHDrawDoc)));
  60. return (CHDrawDoc*)m_pDocument;
  61. }
  62. /////////////////////////////////////////////////////////////////////////////
  63. // CHDrawPView message handlers
  64. int CHDrawPView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  65. {
  66. if (CScrollView::OnCreate(lpCreateStruct) == -1)
  67. return -1;
  68. m_tracker.m_rect.SetRect(0,0,GetDocument()->m_cavasW, GetDocument()->m_cavasH);
  69. m_tracker.m_nStyle=CRectTracker::resizeOutside;
  70. //*here must be pointer, if variable, crashed
  71. m_drawView = new CHDrawView();
  72. // TODO: Add your specialized creation code here
  73. if (!m_drawView->CreateEx(WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR,
  74. AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW,LoadCursor(NULL,IDC_CROSS),
  75. (HBRUSH)GetStockObject(WHITE_BRUSH),NULL),
  76. "",WS_CHILDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  77. m_tracker.m_rect.left,m_tracker.m_rect.top,
  78. m_tracker.m_rect.right-1,m_tracker.m_rect.bottom-1,
  79. this->m_hWnd,NULL))
  80. {
  81. TRACE0("Failed to create toolbar\n");
  82. return -1; // fail to create
  83. }
  84. m_drawView->SetDocument((CHDrawDoc*)m_pDocument);
  85. m_drawView->ShowWindow(SW_NORMAL);
  86. m_drawView->UpdateWindow();
  87. SetClassLong(m_hWnd,GCL_HBRBACKGROUND,(long)GetStockObject(GRAY_BRUSH));
  88. return 0;
  89. }
  90. void CHDrawPView::OnLButtonDown(UINT nFlags, CPoint point)
  91. {
  92. // TODO: Add your message handler code here and/or call default
  93. //in case tracker fail when scrool
  94. CDC* pDC = GetDC();//获得DC
  95. OnPrepareDC(pDC);//设置DC的滚动属性,与ScollView的滚动有关
  96. pDC->DPtoLP(&point);//转换当前点为逻辑位置坐标
  97. int hit = m_tracker.HitTest(point);
  98. switch(hit){
  99. case 2:
  100. case 5:
  101. case 6:
  102. if(m_tracker.Track(this,point)){
  103. //step1. cavas reset
  104. GetDocument()->m_cavasH = m_tracker.m_rect.bottom;
  105. GetDocument()->m_cavasW = m_tracker.m_rect.right;
  106. //step2. scroll or not
  107. CRect clientRect;
  108. GetClientRect(&clientRect);
  109. SetScrollSizes(MM_TEXT, CSize(m_tracker.m_rect.Width()+10, m_tracker.m_rect.Height()+10));
  110. m_drawView->MoveWindow(m_tracker.m_rect.left, m_tracker.m_rect.top,
  111. m_tracker.m_rect.right,m_tracker.m_rect.bottom);
  112. GetDocument()->BackUp();//备份
  113. Invalidate();
  114. }
  115. }
  116. ReleaseDC(pDC);
  117. CScrollView::OnLButtonDown(nFlags, point);
  118. }
  119. BOOL CHDrawPView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  120. {
  121. // TODO: Add your message handler code here and/or call default
  122. if(pWnd == this){
  123. CPoint point;
  124. //Step1. get cursor position
  125. GetCursorPos(&point);
  126. //Step2. convert point from screen to client
  127. ScreenToClient(&point);
  128. CDC* pDC = GetDC();//获得DC
  129. OnPrepareDC(pDC);//设置DC的滚动属性,与ScollView的滚动有关
  130. pDC->DPtoLP(&point);//转换当前点为逻辑位置坐标
  131. if(m_tracker.HitTest(point) >= 0){
  132. //Step3. set cursor, **notice, use nHitTest instead of return of tracker
  133. m_tracker.SetCursor(pWnd, nHitTest);
  134. return true;
  135. }
  136. ReleaseDC(pDC);
  137. }
  138. return CScrollView::OnSetCursor(pWnd, nHitTest, message);
  139. }
  140. void CHDrawPView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
  141. {
  142. // TODO: Add your specialized code here and/or call the base class
  143. ::PostMessage(((CMainFrame *)(AfxGetApp( )->m_pMainWnd))->m_hWnd,1234,(WPARAM)m_drawView,0);
  144. return ;
  145. }