WaveEditCtrl.cpp
上传用户:dthg120
上传日期:2007-01-01
资源大小:50k
文件大小:3k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. // WaveEditCtrl.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "WaveEdit.h"
  5. #include "WaveEditCtrl.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CWaveEditCtrl
  13. CWaveEditCtrl::CWaveEditCtrl()
  14. {
  15. m_bStopped = FALSE;
  16. m_hCursorArrow = AfxGetApp()->LoadStandardCursor (IDC_ARROW);
  17.     m_hCursorIBeam = AfxGetApp()->LoadStandardCursor (IDC_IBEAM);
  18. }
  19. CWaveEditCtrl::~CWaveEditCtrl()
  20. {
  21. }
  22. BEGIN_MESSAGE_MAP(CWaveEditCtrl, CWnd)
  23. //{{AFX_MSG_MAP(CWaveEditCtrl)
  24. ON_WM_PAINT()
  25. ON_WM_SETCURSOR()
  26. ON_WM_CREATE()
  27. ON_WM_LBUTTONDOWN()
  28. //}}AFX_MSG_MAP
  29. END_MESSAGE_MAP()
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CWaveEditCtrl message handlers
  32. int CWaveEditCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  33. {
  34. if (CWnd::OnCreate(lpCreateStruct) == -1)
  35. return -1;
  36. CRect rcClient;
  37. GetClientRect(rcClient);
  38. m_ptOrigin.x = rcClient.left;
  39.     m_ptOrigin.y = rcClient.top;
  40.     m_ptCaretPos = m_ptOrigin;
  41. return 0;
  42. }
  43. BOOL CWaveEditCtrl::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
  44. {
  45. pContext = NULL;
  46. static CString className = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW);
  47. return CWnd::CreateEx(WS_EX_CLIENTEDGE | WS_EX_STATICEDGE,
  48. className, NULL, dwStyle, 
  49. rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
  50. pParentWnd->GetSafeHwnd(), (HMENU) nID);
  51. }
  52. void CWaveEditCtrl::OnPaint() 
  53. {
  54. int amp;
  55. CPaintDC dc(this); // device context for painting
  56. CPen* pOldPen;
  57. CPen pen (PS_SOLID, 0,  RGB(0,255,0));
  58. pOldPen = dc.SelectObject(&pen);
  59. CRect rcClient;
  60. GetClientRect(rcClient);
  61. for(int i = 0; i < rcClient.Width(); ++i){
  62. amp = (rand()% 15)% (rcClient.Height() / 2);
  63. dc.MoveTo (rcClient.left + i, rcClient.top + (rcClient.Height()/2) - amp);
  64. dc.LineTo (rcClient.left + i, rcClient.top + (rcClient.Height()/2) + amp);
  65. }
  66. dc.SelectObject(pOldPen);
  67. // Do not call CWnd::OnPaint() for painting messages
  68. }
  69. void CWaveEditCtrl::OnLButtonDown(UINT nFlags, CPoint point)
  70. {
  71. nFlags = NULL;
  72. m_bStopped = TRUE;
  73. MoveCaret();
  74. m_ptCaretPos.x = point .x;
  75. }
  76. BOOL CWaveEditCtrl::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
  77. {
  78. CRect rcClient;
  79. GetClientRect(rcClient);
  80. if (nHitTest == HTCLIENT) {
  81.         DWORD dwPos = ::GetMessagePos ();
  82.         CPoint point (LOWORD (dwPos), HIWORD (dwPos));
  83.         ScreenToClient (&point);
  84.         ::SetCursor (rcClient.PtInRect (point) ?
  85.             m_hCursorIBeam : m_hCursorArrow);
  86.         return TRUE;
  87.     }
  88. return CWnd::OnSetCursor(pWnd, nHitTest, message);
  89. }
  90. void CWaveEditCtrl::MoveCaret()
  91. {
  92. //Invalidate();
  93. CRect rcClient;
  94. GetClientRect(rcClient);
  95. m_ptCaretPos.x = m_ptCaretPos.x + 1;
  96. if (m_ptCaretPos.x  > rcClient.Width()){
  97. m_bStopped = TRUE;
  98. m_ptCaretPos.x = 0;
  99. }
  100. SetCaretPos(m_ptCaretPos);
  101. }
  102. void CWaveEditCtrl::SetCaret()
  103. {
  104. CRect rcClient;
  105. GetClientRect(rcClient);
  106. CreateSolidCaret (max (1, ::GetSystemMetrics (SM_CXBORDER)),
  107.         rcClient.bottom - rcClient.top);
  108.     SetCaretPos (m_ptCaretPos);
  109.     ShowCaret ();
  110. }
  111. void CWaveEditCtrl::KillCaret()
  112. {
  113. HideCaret ();
  114.     m_ptCaretPos = GetCaretPos ();
  115.     ::DestroyCaret ();
  116. }