13.11. Mouse-Event HandlingThis section explains how to handle mouse events, such as clicks, presses and moves, which are generated when the user interacts with a control via the mouse. Mouse events can be handled for any control that derives from class System.Windows.Forms.Control. For most mouse events, information about the event is passed to the event-handling method through an object of class MouseEventArgs, and the delegate used to create the mouse-event handlers is MouseEventHandler. Each mouse-event-handling method for these events requires an Object and a MouseEventArgs object as arguments. Class MouseEventArgs contains information related to the mouse event, such as the mouse pointer's x- and y-coordinates, the mouse button pressed (Right, Left or Middle) and the number of times the mouse was clicked. Note that the x- and y-coordinates of the MouseEventArgs object are relative to the control that generated the eventthat is point (0,0) represents the upper-left corner of the control where the mouse event occurred. Several common mouse events are described in Figure 13.37.
Figure 13.38 uses mouse events to draw on a Form. Whenever the user drags the mouse (i.e., moves the mouse while a mouse button is pressed), small circles appear on the Form at the position where each mouse event occurs during the drag operation. Figure 13.38. Using the mouse to draw on a Form.
Line 4 declares variable shouldPaint, which determines whether to draw on the Form. We want to draw only while the mouse button is pressed (i.e., held down). Thus, when the user clicks or holds down a mouse button, the system generates a MouseDown event, and the event handler FrmPainter_MouseDown (lines 711) sets shouldPaint to true. When the user releases the mouse button, the system generates a MouseUp event, shouldPaint is set to False in the FrmPainter_MouseUp event handler (lines 1418) and the program stops drawing. Unlike MouseMove events, which occur continuously as the user moves the mouse, the system generates a MouseDown event only when a mouse button is first pressed and generates a MouseUp event only when a mouse button is released. Whenever the mouse moves over a control, the MouseMove event for that control occurs. Inside the FrmPainter_MouseMove event handler (lines 2132), the program draws only if shouldPaint is TRue (i.e., a mouse button is pressed). Line 27 calls inherited Form method CreateGraphics to create a Graphics object that allows the program to draw on the Form. Class Graphics provides methods that draw various shapes. For example, lines 2829 use method FillEllipse to draw a circle. The first parameter to method FillEllipse in this case is an object of class SolidBrush, which specifies the solid color that will fill the shape. The color is provided as an argument to class SolidBrush's constructor. Type Color contains many predefined color constantswe selected Color.BlueViolet. FillEllipse draws an oval in a bounding rectangle that is specified by the x- and y-coordinates of its upper-left corner, its width and its heightthe final four arguments to the method. The x- and y-coordinates represent the location of the mouse event and can be taken from the mouse-event arguments (e.X and e.Y). To draw a circle, we set the width and height of the bounding rectangle so that they are equalin this example, both are 4 pixels. Line 30 invokes the Graphics object's Dispose method to return the Graphics object's resources back to the system. Graphics, SolidBrush and Color are all part of the System.Drawing namespace. We discuss class Graphics and its methods in depth in Chapter 17, Graphics and Multimedia. |