Using the Common Mouse Events

   

As with keyboard input, most controls support mouse input natively; you don't have to write code to deal with mouse input. However, at times you need more control than that offered by the native functionality of a control. C# supports six events that enable you to deal with mouse input directly. These events are listed in Table 18.5, in the order in which they occur.

Table 18.5. Events Used to Handle Mouse Input
Event Name Description
MouseEnter Occurs when the pointer enters a control.
MouseMove Occurs when the pointer moves over a control.
MouseHover Occurs when the pointer hovers over a control.
MouseDown Occurs when the pointer is over a control and a button is pressed.
MouseUp Occurs when the pointer is over a control and a button is released.
MouseLeave Occurs when the pointer leaves a control.

You're now going to build a project that illustrates interacting with the mouse, using the MouseMove event. This project will allow a user to draw on a form, much like you can draw in a paint program. Begin by creating a new Windows Application titled Mouse Paint. Change the name of the default form to fclsMousePaint, set its Text property to Paint with the Mouse, and set the Main() entry point of the project to reference fclsMousePaint instead of Form1.

Next, double-click the form to access its default event, the Load event. Enter the following statement into the load event:

 m_objGraphics = this.CreateGraphics(); 

You've already used a graphics object a few times. What you're doing here is setting a graphics object to the client area of the form; any drawing performed on the object will appear on the form. Because you're going to draw to this graphics object each time the mouse moves over the form, there's no point in creating a new graphics object each time you need to draw to it. Therefore, you're going to make m_objGraphics a module-level variable, which will be instantiated only once ”in the Load event of the form. Enter this statement below the opening curly- brace after the public class fclsMousePaint class declaration:

 private Graphics m_objGraphics; 

As I've said previously, you should always destroy objects when you're done with them. In this case, you want the object to remain in existence for the life of the form. Therefore, you'll destroy it in the Closed event of the form, which occurs when the form is unloaded. Return to the Form1.cs[Design] tab, open the events list in the Property window and double-click the Closed event to create and open the code window to the Closed event. Enter the following statement in the Closed event:

 m_objGraphics.Dispose(); 

Your form should now look like the one shown in Figure 18.10.

Figure 18.10. Code in many places often works together to achieve one goal.

graphics/18fig10.jpg


The last bit of code you need to add is the code that will draw on the form. You're going to place code in the MouseMove event of the form to do this. First, the code will make sure the left mouse button is held down. If it's not, no drawing will take place; the user must hold down the mouse button to draw. Next, a rectangle will be created. The coordinates of the mouse pointer will be used to create a very small rectangle that will be passed to the DrawEllipse method of the graphics object. This has the effect of drawing a tiny circle right where the mouse pointer is positioned. Again, Return to the Form1.cs[Design] tab, open the events list in the Property window, and double-click the MouseMove event to create and open the code window to the MouseMove event. Add the following code to this event:

 Rectangle rectEllipse = new Rectangle() ; if (e.Button != MouseButtons.Left) return; rectEllipse.X = e.X - 1; rectEllipse.Y = e.Y - 1; rectEllipse.Width = 2; rectEllipse.Height = 2; m_objGraphics.DrawEllipse(System.Drawing.Pens.Blue, rectEllipse); 

Like all events, the e object contains information related to the event. In this example, you are using the X and Y properties of the e object, which is the coordinate of the pointer when the event fires. In addition, you're checking the Button property of the object to make sure the user is pressing the left button.

Your project is now complete! Save your work by clicking Save All on the toolbar, and then press F5 to run the project. Move your mouse over the form ”nothing happens. Now, hold down the left mouse button and move the mouse. This time, you'll be drawing on the form (see Figure 18.11).

Figure 18.11. Capturing mouse events opens many exciting possibilities.

graphics/18fig11.jpg


Notice that the faster you move the mouse, the more space appears between circles. This shows you that the user is able to move the mouse faster than the MouseMove event can fire, so you can't get every single movement of the mouse. This is important to remember.


   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net