SaveAnimation.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "stdafx.h"
  2. #include ".\saveanimation.h"
  3. #include "PerfTimer.h"
  4. CSaveAnimation::CSaveAnimation(void)
  5. {
  6. m_dLeftPercent = 0;
  7. m_dTopPercent = 0;
  8. m_dRightPercent = 0;
  9. m_dBottomPercent = 0;
  10. m_dSpeed = 40;
  11. }
  12. CSaveAnimation::~CSaveAnimation(void)
  13. {
  14. }
  15. void CSaveAnimation::DoAnimation(CRect crStart, CRect crEnd, CWnd *pWnd)
  16. {
  17. m_crStart = crStart;
  18. m_crEnd = crEnd;
  19. long lMaxDist = GetMaxDistance();
  20. GetPercentages(lMaxDist);
  21. CDC* pDC = pWnd->GetDC();
  22. CRect crCur(m_crStart);
  23. CRect crPrev(-1, -1, -1, -1);
  24. MSG msg;
  25. double dCurLeft = crCur.left;
  26. double dCurTop = crCur.top;
  27. double dCurRight = crCur.right;
  28. double dCurBottom = crCur.bottom;
  29. CPerfTimer Timer;
  30. for(int i = 0; i < lMaxDist/m_dSpeed; i++)
  31. {
  32. //don't do the first time
  33. if(i > 0)
  34. {
  35. //wait 20ms between paints
  36. while(Timer.Elapsedms() < 10)
  37. {
  38. Sleep(1);
  39. }
  40. //Remove the old focus rect
  41. pDC->DrawFocusRect(crPrev);
  42. }
  43. Timer.Start(TRUE);
  44. pDC->DrawFocusRect(crCur);
  45. crPrev = crCur;
  46. dCurLeft -= m_dLeftPercent * m_dSpeed;
  47. dCurTop -= m_dTopPercent * m_dSpeed;
  48. dCurRight -= m_dRightPercent * m_dSpeed;
  49. dCurBottom -= m_dBottomPercent * m_dSpeed;
  50. crCur.left = (int)dCurLeft;
  51. crCur.top = (int)dCurTop;
  52. crCur.right = (int)dCurRight;
  53. crCur.bottom = (int)dCurBottom;
  54. while (PeekMessage(&msg, pWnd->m_hWnd, 0, 0, PM_REMOVE))
  55. {
  56. TranslateMessage(&msg);
  57. DispatchMessage(&msg);
  58. }
  59. }
  60. //Draw one more time to remove the last focus rect
  61. pDC->DrawFocusRect(crPrev);
  62. while (PeekMessage(&msg, pWnd->m_hWnd, 0, 0, PM_REMOVE))
  63. {
  64. TranslateMessage(&msg);
  65. DispatchMessage(&msg);
  66. }
  67. pWnd->ReleaseDC(pDC);
  68. }
  69. long CSaveAnimation::GetMaxDistance()
  70. {
  71. long lMax = 0;
  72. if(abs(m_crStart.left - m_crEnd.left) > lMax)
  73. lMax = abs(m_crStart.left - m_crEnd.left);
  74. if(abs(m_crStart.top - m_crEnd.top) > lMax)
  75. lMax = abs(m_crStart.top - m_crEnd.top);
  76. if(abs(m_crStart.right - m_crEnd.right) > lMax)
  77. lMax = abs(m_crStart.right - m_crEnd.right);
  78. if(abs(m_crStart.bottom - m_crEnd.bottom) > lMax)
  79. lMax = abs(m_crStart.bottom - m_crEnd.bottom);
  80. return lMax;
  81. }
  82. void CSaveAnimation::GetPercentages(long lMaxDist)
  83. {
  84. if(lMaxDist > 0)
  85. {
  86. m_dLeftPercent = (m_crStart.left - m_crEnd.left) / (double)lMaxDist;
  87. m_dTopPercent = (m_crStart.top - m_crEnd.top) / (double)lMaxDist;
  88. m_dRightPercent = (m_crStart.right - m_crEnd.right) / (double)lMaxDist;
  89. m_dBottomPercent = (m_crStart.bottom - m_crEnd.bottom) / (double)lMaxDist;
  90. }
  91. }