home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / progjour / 1991 / 06 / scribble.cpp < prev    next >
C/C++ Source or Header  |  1991-10-31  |  2KB  |  70 lines

  1. /*---------------------------------------------------------
  2.   Drawing with the mouse
  3. ---------------------------------------------------------*/
  4. #include <windows.h>
  5. #include "winclass.h"
  6. #include "initinst.h"
  7.  
  8. class SCRIBBLE : public MAINWINDOW
  9. {
  10.   protected:
  11.     HDC hDC;
  12.  
  13.     LPSTR GetClassName() {return "Scribble";};
  14.     void MouseMove ();
  15.     void LButtonDown ();
  16.     void LButtonUp ();
  17.  
  18.   public:
  19.     SCRIBBLE () { hDC = NULL; };
  20. };
  21.  
  22. /*---------------------------------------------------------
  23.  When the user pushes down the left mouse button,
  24.  get a device context and set the current position
  25. ---------------------------------------------------------*/
  26. void SCRIBBLE::LButtonDown ()
  27. {
  28.   hDC = GetDC (hWnd);
  29.   if (hDC)
  30.   {
  31.     MoveTo (hDC, getx(), gety());
  32.     ShowCursor (FALSE);  // Turn off the mouse arrow.
  33.     SetCapture (hWnd);   // Capture the mouse.
  34.   }
  35. }
  36.  
  37. /*---------------------------------------------------------
  38.  When the mouse moves from one pixel position to another,
  39.  draw a line if the device context is set
  40. ---------------------------------------------------------*/
  41. void SCRIBBLE::MouseMove ()
  42. {
  43.   if (hDC)
  44.     LineTo (hDC, getx(), gety());
  45. }
  46.  
  47. /*---------------------------------------------------------
  48.  When the user lifts up on the left mouse button,
  49.  release the device context
  50. ---------------------------------------------------------*/
  51. void SCRIBBLE::LButtonUp ()
  52. {
  53.   if (hDC)
  54.   {
  55.     ReleaseDC (hWnd, hDC);
  56.     hDC = NULL;
  57.     ShowCursor (TRUE);  // Turn the mouse arrow back on.
  58.     ReleaseCapture();   // Release the mouse.
  59.   }
  60. }
  61.  
  62. /*---------------------------------------------------------
  63.  Create a Scribble window
  64. ---------------------------------------------------------*/
  65. BOOL InitInstance()
  66. {
  67.   static SCRIBBLE scribble;
  68.   return scribble.Make();
  69. }
  70.