Handling Windows Messages in the View

To implement mouse-driven drawing in Scribble, you must add code that handles several Windows messages related to mouse activity. You will use WizardBar to help add this message-handling code.

Suggested Reading

  • , Win32 SDK Reference

  • , Win32 SDK Reference

  • , Win32 SDK Reference

  • , Win32 SDK Reference

  • , Microsoft Foundation Class Reference

  • , Microsoft Foundation Class Reference

  • , Microsoft Foundation Class Reference

  • Overview, Microsoft Foundation Class Reference

  • Overview, Microsoft Foundation Class Reference

  • , Visual C++ Programmer’s Guide

  • , Visual C++ User’s Guide

In Scribble, Windows sends a user action message to a window, in this case the currently active view. The view uses its message map to determine whether it has a member function that can handle the message. If it does, the view calls the handler.

The following table correlates the user action to the message sent by Windows to Scribble’s view and the corresponding member function that handles the message:


User
Message sent from Windows to the window
Handler
Press left mouse button WM_LBUTTONDOWN CScribbleView::OnLButtonDown
Move mouse (whether or not mouse button is pressed) WM_MOUSEMOVE CScribbleView::OnMouseMove
Release left mouse button WM_LBUTTONUP CScribbleView::OnLButtonUp

The view handles mouse-drawing messages because it’s in the view that Scribble’s drawing takes place. The view represents that part of the document that can be seen at any one time.

The message handlers track mouse activity and draw in the view accordingly. They also call member functions of the document to update its data. As the user draws a stroke, the points that make up the stroke are stored in the document’s stroke list.