The OnLButtonUp
member function ends the current stroke when the user releases the left mouse button. The function draws a line to connect the last stroke, then releases the mouse for use by other windows. The test at the beginning calls the Windows GetCapture function to determine whether the current window has control of the mouse. If not, the user is not currently drawing in this view.
To add code for OnLButtonUp
CScribbleView
’s member function OnLButtonUp
in ScribbleView.cpp.// Mouse button up 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.
CScribbleDoc* pDoc = GetDocument();
CClientDC dc( this );
CPen* pOldPen = dc.SelectObject( pDoc->GetCurrentPen( ) );
dc.MoveTo( m_ptPrev );
dc.LineTo( point );
dc.SelectObject( pOldPen );
m_pStrokeCur->m_pointArray.Add(point);
ReleaseCapture( ); // Release the mouse capture established
// at the beginning of the mouse drag.
return;
In the code above, the view obtains a pointer to its document, using GetDocument
. Then the view makes sure it has the current pen. It draws the line, adding the points of the current stroke, m_pStrokeCur
, to the array, m_pointArray
, which contains the points that define the stroke. Finally it releases the mouse capture established at the beginning of the mouse drag.