Microsoft KB Archive/192003

= BUG: Phantom MouseMove Events with a Transparent UserControl =

Article ID: 192003

Article Last Modified on 5/13/2003

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q192003



SYMPTOMS
A Visual Basic UserControl, whose BackStyle property is set to Transparent, may erroneously receive MouseMove messages when the mouse is over the control but is not currently moving.



CAUSE
The problem occurs because a property or method has been called on the control that causes the control to repaint itself. The repaint event results in a change in the window region for the control, which in turn fires the MouseMove event.

This behavior does not occur if the UserControl's BackStyle property is set to Opaque.



RESOLUTION
Setting the BackStyle property to Opaque offers one approach to working around this problem. However, if this property must be set to Transparent, another potential workaround is to use two static variables to monitor the values of the X and Y coordinates sent to the MouseMove event. If these values are the same as the previous MouseMove event, then you can treat this mouse message as a phantom message.



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.



Steps to Reproduce Behavior
 Create a new Standard EXE project. Form1 is created by default. From the File menu, add an ActiveX Control project. UserControl1 is created by default. Add a Shape control to UserControl1.  Paste the following code into UserControl1's code window: Option Explicit

Private Sub UserControl_Initialize UserControl.BackStyle = 0 'Transparent Shape1.BackStyle = 1 'Opaque End Sub

Private Sub UserControl_MouseMove(Button As Integer, Shift As _                                       Integer, X As Single, Y As Single) Debug.Print "UserControl: Mouse Move" End Sub

Public Sub Refresh Shape1.Refresh End Sub  Close the UserControl's code and design windows.</li> Add an instance of UserControl1 to Form1.</li> Add a Timer control to Form1 and set its Interval property to 2000.</li>  Paste the following code into Form1's code window: Private Sub Timer1_Timer Debug.Print "Timer: Is mouse moving?" UserControl11.Refresh End Sub </li> Run the project by pressing the F5 key.</li> Move the mouse over the shape and notice the MouseMove messages in the Immediate Window.</li> Position the mouse over the shape control and stop moving it. When the Timer event fires, a Timer message appears in the Immediate Window, followed immediately by another MouseMove message. The mouse does not need to be moving for this mouse message to appear; it is caused by the Refresh method.</li>  Stop the program and change the code in the UserControl_MouseMove event to monitor the values of X and Y and only respond to messages whose X or Y values have changed between calls: Private Sub UserControl_MouseMove(Button As Integer, Shift As _                                       Integer, X As Single, Y As Single) Static mX As Single, mY As Single If mX <> X Or mY <> Y Then Debug.Print "UserControl: Mouse Move" mX = X            mY = Y          End If       End Sub </li> Repeat steps 9 through 11. With the mouse stationary over the shape, you should see the Timer messages in the Immediate Window, but not the phantom mouse messages.</li></ol>

Additional query words: kbDSupport kbDSD extra kbVBp kbCtrl kbVBp600bug kbVBp500bug

Keywords: kbbug KB192003

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.