Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Formwide mousemove

Posted on 2008-10-08
7
Medium Priority
?
1,564 Views
Last Modified: 2013-11-05
Is there a mousemove event that is for the whole form.
Not that i have to capture all the different mousemove events from all the objects on the form.
0
Comment
Question by:dgb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 22669658
Sure...use the IMessageFilter Interface like this:
Public Class Form1
 
    Private WithEvents mf As New MyFilter
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Application.AddMessageFilter(mf)
    End Sub
 
    Private Sub mf_MouseMove() Handles mf.MouseMove
        If Me Is Form1.ActiveForm Then
            Dim clientPT As Point = Me.PointToClient(Cursor.Position)
            Debug.Print("WM_MOUSEMOVE: " & clientPT.ToString)
        Else
            ' ...WM_MOUSEMOVE is targeting a form other than this one...
        End If
    End Sub
 
    Private Class MyFilter
        Implements IMessageFilter
 
        Public Event MouseMove()
        Private Const WM_MOUSEMOVE As Integer = &H200
 
        Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
            Select Case m.Msg
                Case WM_MOUSEMOVE
                    RaiseEvent MouseMove()
 
            End Select
        End Function
 
    End Class
 
End Class

Open in new window

0
 
LVL 18

Expert Comment

by:mdougan
ID: 22669661
I don't believe so.  In earlier versions of VB they had a form property called Form KeyPreview and if you set that to true, then the form received keydown and keypress events before those events fired for the individual components, but they didn't have anything similar for mousemove events.

It is possible to do, but you have to "hook" the window's window_procedure.  All events go through the window's procedure first.  That's a lot of Win API coding.  You can find some examples at Karl Peterson's website:

http://vb.mvps.org/samples/

Look for the one called HookMe.  I see another one there called MouseEvent that I'm not familiar with, but, it might have something for you as well.
0
 
LVL 11

Expert Comment

by:ladarling
ID: 22669691
You need to create a handler sub and assign the controls that you need to monitor to the Handles list, for example:
   Private Sub MonitorMouse(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    Me.MouseMove, Button1.MouseMove, Label1.MouseMove 'Etc
        'Your code here.
    End Sub

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:mdougan
ID: 22669714
That's pretty cool Idle Mind, that sample's going in the file!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22670308
If you want to know what control you are over then you could do something like the below.

Note that it does have some quirks...for instance, it doesn't properly detect when you are over SubMenus in a MenuStrip.  Also, for MenuStrips and ToolStrips you don't what know what actual item you are over...just that you are over the Menu or ToolBar.  The code can be modified to handle these special case though using the GetItemAt() functions.
Public Class Form1
 
    Private WithEvents mf As New MyFilter
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Application.AddMessageFilter(mf)
    End Sub
 
    Private Sub mf_MouseMove() Handles mf.MouseMove
        If Form.ActiveForm Is Me Then
            Dim clientPT As Point
            Dim formPT As Point = Me.PointToClient(Cursor.Position)
            Dim ctlName As String
            Dim ctl As Control = FindControl(Me)
            If Not IsNothing(ctl) Then
                ctlName = ctl.Name
                clientPT = ctl.PointToClient(Cursor.Position)
            Else
                ctlName = Me.Name
                clientPT = Me.PointToClient(Cursor.Position)
            End If
            Debug.Print(ctlName & ": " & clientPT.ToString & "    Form: " & formPT.ToString)
        Else
            ' ...WM_MOUSEMOVE is targeting a form other than this one...
        End If
    End Sub
 
    Private Function FindControl(ByVal cont As Control) As Control
        Dim ctl As Control = cont.GetChildAtPoint(cont.PointToClient(Cursor.Position))
        If Not IsNothing(ctl) Then
            If ctl.HasChildren Then
                Dim subCtl As Control = FindControl(ctl)
                If Not IsNothing(subCtl) Then
                    Return subCtl
                Else
                    Return ctl
                End If
            Else
                Return ctl
            End If
        Else
            Return Nothing
        End If
    End Function
 
    Private Class MyFilter
        Implements IMessageFilter
 
        Public Event MouseMove()
        Private Const WM_MOUSEMOVE As Integer = &H200
 
        Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
            Select Case m.Msg
                Case WM_MOUSEMOVE
                    RaiseEvent MouseMove()
 
            End Select
        End Function
 
    End Class
 
End Class

Open in new window

0
 

Author Closing Comment

by:dgb
ID: 31504245
Thanks, only pitty that activeform doesn't work when in debugmode
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22678774
"only pitty that activeform doesn't work when in debugmode"

It worked fine for me in Debug mode!...   =\

When I had a different form active I didn't get messages in my Immediate window.  This was the case for you?  I was using VB.Net 2005 Express on a WinXP Pro system.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question