object is the most complex object in
the sample. The
View is responsible for all
interactions with the user, which means the
must collect the strokes as the user draws them, and also draw the
entire list of strokes whenever the window requires repainting.
The collection of the strokes is the most complex part. To collect effectively, you must trap the user pressing the mouse button in the window. Once this occurs, enter a drawing mode, and as the mouse is moved, draw a line to the current position. When the user releases the mouse button, they have completed the stroke, so add the stroke to the document. The key steps to coax this behavior are:
View must hook the relevant mouse messages: in
this case, the
LBUTTONDOWN message is received, remember
the start position and enter a drawing mode. Also capture the mouse,
to ensure that you get all future mouse messages, even when the mouse
leaves the window.
MOUSEMOVE message occurs when you are in
drawing mode, draw a line from the remembered start position to the
current mouse position. In addition, erase the previous line drawn by
this process. This gives a “rubber band” effect as you
move the mouse.
LBUTTONUP message is received, notify the
document of the new, completed stroke, release the mouse capture, and
leave drawing mode.
After adding this logic to the sample, it now looks like:
class ScribbleView(pywin.mfc.docview.ScrollView): ...