Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleFlush mouse events
Keywordsmouse events, ignore events, events, disable
CategoriesTips and Tricks
 
Use PeekMessage to remove the mouse events from the queue.

The point is sometimes mouse events get queued when you don't want them. Click the Show Popup button. Then click on the PictureBox while the popup is visible. The mouse click falls through to the PictureBox and it draws a circle.

This can be annoying. In many programs, the user will click like this to get rid of the popup without wanting the click to hit the PictureBox. For example, in a drawing program, you do not want to start drawing.

Now check the Flush Events box and try again. This time the program flushes the pending mouse events after dismissing the popup, so the PictureBox never receives that click.

 
' Discard any pending mouse events for this window.
Private Sub FlushMouseEvents(ByVal hwnd As Long)
Dim msg_info As MSG

    Do While PeekMessage(msg_info, hwnd, WM_MOUSEFIRST, _
        WM_MOUSELAST, PM_REMOVE) <> 0
            ' Fetch messages until there are no more.
    Loop
End Sub

Private Sub cmdShowpopup_Click()
    Picture1.Cls
    PopupMenu mnuPopup

    If chkFlush.Value = vbChecked Then
        FlushMouseEvents Picture1.hwnd
    End If
End Sub

' Draw a circle where the mouse clicks.
Private Sub Picture1_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    Picture1.Circle (X, Y), 300, vbRed
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated