home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n08 / mfcpart3.exe / MOUSECAP.CPP next >
C/C++ Source or Header  |  1995-08-01  |  3KB  |  105 lines

  1. //***************************************************************************
  2. //
  3. //  MouseCap demonstrates mouse capturing.
  4. //
  5. //***************************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "mousecap.h"
  9.  
  10. CMyApp myApp;
  11.  
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CMyApp member functions
  14.  
  15. BOOL CMyApp::InitInstance ()
  16. {
  17.     m_pMainWnd = new CMainWindow ();
  18.     m_pMainWnd->ShowWindow (m_nCmdShow);
  19.     m_pMainWnd->UpdateWindow ();
  20.     return TRUE;
  21. }
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CMainWindow message map and member functions
  25.  
  26. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  27.     ON_WM_LBUTTONDOWN ()
  28.     ON_WM_LBUTTONUP ()
  29.     ON_WM_MOUSEMOVE ()
  30.     ON_WM_NCLBUTTONDOWN ()
  31. END_MESSAGE_MAP ()
  32.  
  33. CMainWindow::CMainWindow ()
  34. {
  35.     m_bTracking = FALSE;
  36.     m_bCaptureEnabled = TRUE;
  37.  
  38.     Create (NULL, "Mouse Capture Demo (Capture Enabled)");
  39.  
  40.     ::SetClassLong (m_hWnd, GCL_HCURSOR,
  41.         (LONG) myApp.LoadStandardCursor (IDC_CROSS));
  42. }
  43.  
  44. void CMainWindow::OnLButtonDown (UINT nFlags, CPoint point)
  45. {
  46.     m_pointFrom = point;
  47.     m_pointTo = point;
  48.     m_bTracking = TRUE;
  49.  
  50.     if (m_bCaptureEnabled)
  51.         SetCapture ();
  52. }
  53.  
  54. void CMainWindow::OnLButtonUp (UINT nFlags, CPoint point)
  55. {
  56.     if (m_bTracking) {
  57.         m_bTracking = FALSE;
  58.         if (GetCapture () == this)
  59.             ReleaseCapture ();
  60.  
  61.         CClientDC dc (this);
  62.         dc.SetROP2 (R2_XORPEN);
  63.         dc.SelectStockObject (WHITE_PEN);
  64.  
  65.         dc.MoveTo (m_pointFrom);
  66.         dc.LineTo (m_pointTo);
  67.  
  68.         dc.SetROP2 (R2_COPYPEN);
  69.         CPen pen (PS_SOLID, 16, RGB (255, 0, 0));
  70.         dc.SelectObject (&pen);
  71.  
  72.         dc.MoveTo (m_pointFrom);
  73.         dc.LineTo (point);
  74.     }
  75. }
  76.  
  77. void CMainWindow::OnMouseMove (UINT nFlags, CPoint point)
  78. {
  79.     if (m_bTracking) {
  80.         CClientDC dc (this);
  81.         dc.SetROP2 (R2_XORPEN);
  82.         dc.SelectStockObject (WHITE_PEN);
  83.  
  84.         dc.MoveTo (m_pointFrom);
  85.         dc.LineTo (m_pointTo);
  86.  
  87.         dc.MoveTo (m_pointFrom);
  88.         dc.LineTo (point);
  89.  
  90.         m_pointTo = point;
  91.     }
  92. }
  93.  
  94. void CMainWindow::OnNcLButtonDown (UINT nHitTest, CPoint point)
  95. {
  96.     if (nHitTest == HTCAPTION) {
  97.         m_bCaptureEnabled = m_bCaptureEnabled ? FALSE : TRUE;
  98.  
  99.         SetWindowText (m_bCaptureEnabled ?
  100.             "Mouse Capture Demo (Capture Enabled)" :
  101.             "Mouse Capture Demo (Capture Disabled)");
  102.     }
  103.     CFrameWnd::OnNcLButtonDown (nHitTest, point);
  104. }
  105.