HorizRuler.cpp
上传用户:wujian85
上传日期:2010-04-08
资源大小:227k
文件大小:3k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // HorizRuler.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "printformeditor.h"
  5. #include "HorizRuler.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CHorizRuler
  13. CHorizRuler::CHorizRuler()
  14. {
  15. iFrom = 0;
  16. iTo = 2000;
  17. iDiv = 2;
  18. iPos = 0;
  19. iTextStep = 100;
  20. iGreatStep = 50;
  21. iSmallStep = 25;
  22. iTextDiv = 100;
  23. ft.CreatePointFont(90, "Arial");
  24. crBack = RGB(255,255,255);
  25. crText = RGB(0,0,0);
  26. crLines = RGB(0,0,0);
  27. xMouse = -10;
  28. }
  29. CHorizRuler::~CHorizRuler()
  30. {
  31. }
  32. BEGIN_MESSAGE_MAP(CHorizRuler, CButton)
  33. //{{AFX_MSG_MAP(CHorizRuler)
  34. ON_WM_ERASEBKGND()
  35. //}}AFX_MSG_MAP
  36. END_MESSAGE_MAP()
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CHorizRuler message handlers
  39. void CHorizRuler::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
  40. {
  41. int oxm = xMouse;
  42. CDC * pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
  43. CRect rc(lpDrawItemStruct->rcItem);
  44. pDC->FillSolidRect(rc, crBack);
  45. int t;
  46. int iMin = iFrom;// + iPos;
  47. int iMax = iTo; //iMin + rc.Width() / iDiv;
  48. if (iMax > iTo) iMax = iTo;
  49. int iRange = iMax - iMin;
  50. COLORREF crt = pDC->SetTextColor(crText);
  51. CFont * of   = pDC->SelectObject(&ft);
  52. UINT oa = pDC->SetTextAlign(TA_CENTER|TA_TOP);
  53. CString cs;
  54. CPen pn(PS_SOLID, 1, crLines);
  55. CPen * op = pDC->SelectObject(&pn);
  56. int r1 = rc.Height() / 3;
  57. int r2 = rc.Height() / 4;
  58. for (t = iMin; t <= iMax / iDiv; t += iGreatStep / iDiv)
  59. {
  60. if (rc.left + t - iPos > 0 && rc.top + t - iPos <= rc.right)
  61. {
  62. pDC->MoveTo(rc.left + t - iPos, rc.top + r2);
  63. pDC->LineTo(rc.left + t- iPos, rc.bottom - r2);
  64. }
  65. if (rc.top + t - iPos > rc.right) break;
  66. }
  67. /* for (t = iMin; t < iMax; t += iSmallStep / iDiv)
  68. {
  69. if (rc.left + t - iPos > 0 && rc.top + t - iPos < rc.right)
  70. {
  71. pDC->MoveTo(rc.left + t - iPos, rc.top + r1);
  72. pDC->LineTo(rc.left + t - iPos, rc.bottom - r1);
  73. }
  74. if (rc.top + t - iPos > rc.right) break;
  75. }
  76. */
  77. for (t = iMin; t <= iMax / iDiv; t += iTextStep / iDiv)
  78. {
  79. if (rc.left + t - iPos > 0 && rc.top + t - iPos <= rc.right)
  80. {
  81. cs.Format("%d", t *iDiv / iTextDiv);
  82. if (t <= iTo / iDiv)
  83. pDC->TextOut(rc.left + t - iPos, rc.top, cs);
  84. }
  85. if (rc.top + t - iPos > rc.right) break;
  86. }
  87. pDC->SetTextColor(crt);
  88. pDC->SelectObject(of);
  89. pDC->SetTextAlign(oa);
  90. pDC->SelectObject(op);
  91. xMouse = -1;
  92. DrawMouse(oxm);
  93. }
  94. void CHorizRuler::SetPos(const int pos)
  95. {
  96. iPos = pos;
  97. if (iPos < iFrom) iPos = iFrom;
  98. if (GetSafeHwnd()) Invalidate();
  99. }
  100. void CHorizRuler::SetRange(const int r1, const int r2)
  101. {
  102. iFrom = r1;
  103. iTo = r2;
  104. if (iPos < iFrom) iPos = iFrom;
  105. if (GetSafeHwnd()) Invalidate();
  106. }
  107. BOOL CHorizRuler::OnEraseBkgnd(CDC* pDC) 
  108. {
  109. return true;
  110. // return CButton::OnEraseBkgnd(pDC);
  111. }
  112. void CHorizRuler::DrawMouse(const int x)
  113. {
  114. if (x == xMouse) return;
  115. CRect rc;
  116. GetClientRect(rc);
  117. CClientDC dc(this);
  118. int orop = dc.SetROP2(R2_NOT);
  119. CPen pn(PS_DASHDOT, 1, RGB(0,0,0));
  120. CPen * op = dc.SelectObject(&pn);
  121. if (xMouse >= 0)
  122. {
  123. dc.MoveTo(xMouse, rc.top);
  124. dc.LineTo(xMouse, rc.bottom);
  125. }
  126. xMouse = x;
  127. if (xMouse >= 0)
  128. {
  129. dc.MoveTo(xMouse, rc.top);
  130. dc.LineTo(xMouse, rc.bottom);
  131. }
  132. dc.SetROP2(orop);
  133. dc.SelectObject(op);
  134. }