Draw While the Mouse Button Is Down

Between the time that the mouse button goes down and the time that it’s released, Scribble tracks the mouse and draws a trace of its movements in the view. OnMouseMove is called as the user moves the mouse while drawing the current stroke. The function connects the latest mouse location with its previous location and saves the new location as the previous point for the next time the function is called. To do the drawing, OnMouseMove constructs a local CClientDC object used to draw in the window’s client area.

To add code for OnMouseMove

  1. Using WizardBar, jump to CScribbleView’s member function OnMouseMove in ScribbleView.cpp.

  2. Replace the //TODO comments and code with the code shown here:
    // Mouse movement is interesting in the Scribble application
    // only if the user is currently drawing a new stroke by 
    // dragging the captured mouse.
    
    if( GetCapture( ) != this )
    return;        // If this window (view) didn't capture the 
    // mouse, the user isn't drawing in this window.
    
    CClientDC dc( this );
    
    m_pStrokeCur->m_pointArray.Add(point);
    
    // Draw a line from the previous detected point in the mouse
    // drag to the current point.
    CPen* pOldPen = 
    dc.SelectObject( GetDocument( )->GetCurrentPen( ) );
       dc.MoveTo( m_ptPrev );
       dc.LineTo( point );
       dc.SelectObject( pOldPen );
       m_ptPrev = point;
       return;
    

In the Scribble application, OnLButtonDown, OnMouseMove, and OnLButtonUp handle the three phases of mouse drawing: beginning to track the mouse, tracking the mouse and connecting points, and ending mouse tracking.