The MouseMove Event

The MouseMove event occurs when the mouse pointer is moved across the screen. Both forms and controls recognize the MouseMove event while the mouse pointer is within their borders.

Using MouseMove with the Line Method

Graphics methods can produce very different effects when used in a MouseMove procedure instead of in a MouseDown procedure. For example, in the topic "The MouseDown Event" earlier in this chapter, the Line method drew connected line segments. In the Scribble application described below, the same method is used in a Form_MouseMove procedure to produce a continuous curved line instead of connected segments.

In the Scribble application, the MouseMove event is recognized whenever the mouse pointer changes position. The following code draws a line between the current and previous location.

Private Sub Form_MouseMove (Button As Integer, _
      Shift As Integer, X As Single, Y As Single)
   Line -(X, Y)
End Sub

Like the MouseDown procedure, the line created by the MouseMove procedure starts at the upper-left corner, as shown in Figure 11.2.

Figure 11.2   The MouseMove event and the Line method create a simple sketch program

How MouseMove Works

How many times does the MouseMove event get called as the user moves the pointer across the screen? Or, to put it another way, when you move the pointer from the top of the screen to the bottom, how many locations are involved?

Visual Basic doesn't necessarily generate a MouseMove event for every pixel the mouse moves over. The operating environment generates a limited number of mouse messages per second. To see how often MouseMove events are actually recognized, you can enhance the Scribble application with the following code so that it draws a small circle at each location where a MouseMove event is recognized. The results are shown in Figure 11.3.

Private Sub Form_MouseMove (Button As Integer,_
      Shift As Integer, X As Single, Y As Single)
   Line -(X, Y)
   Circle (X, Y), 50
End Sub

Figure 11.3   A demonstration of where MouseMove events occur

Note that the faster the user moves the pointer, the fewer MouseMove events are recognized between any two points. Many circles close together indicate that the user moved the mouse slowly.

Your application can recognize many MouseMove events in quick succession. Therefore, a MouseMove event procedure shouldn't do anything that requires large amounts of computing time.

For More Information   See "MouseMove Event" in the Language Reference.