Mouse move event when mouse not over form

Benedict

New member
Joined
Dec 2, 2006
Messages
2
Programming Experience
Beginner
Hello All,

I'm new to programming and this forum so please excuse me if this is a stupid question.

I'm trying to resize a form when the mouse is 'tapped' twice against the edge of the screen. My idea is to use the mouse move event to start a timer when the mouse co-ordinates are equal to that of the screen edge. If the timer is already running then it stops the timer and resizes the form. The timer runs for .5 seconds and then disables itself so that the taps have to be close enough to be consider deliberate. The code for these events are listed below.


VB.NET:
    Dim ScreenWidth As Integer = Screen.PrimaryScreen.Bounds.Width - 1
        Dim ScreenHeight As Integer = Screen.PrimaryScreen.Bounds.Height
        Dim cX As Integer = Cursor.Position.X
        Dim cY As Integer = Cursor.Position.Y
        Dim TapSensitivity As Integer = 30

        If (cX = 0) Then
            'if at screen edge...
            If Not ScreenTapTimer.Enabled Then
                ScreenTapTimer.Enabled = True
                MovedAway = False
            Else
                If MovedAway Then
                    ScreenTapTimer.Enabled = False
                    'things to do when screen is tapped
                    If Me.WindowState = FormWindowState.Maximized Then
                        Me.WindowState = FormWindowState.Normal
                    Else
                        Me.WindowState = FormWindowState.Maximized
                    End If
                End If
            End If
        Else
            'if not at the screen edge.....
            If ((cX > TapSensitivity)) And ScreenTapTimer.Enabled Then
                MovedAway = True
            End If
        End If
    End Sub


    Private Sub ScreenTapTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ScreenTapTimer.Tick
        ScreenTapTimer.Enabled = False
    End Sub
End Class
The problem with this is that the mouse move event is only fired when the mouse is over the form. How can I get the mouse move event to fire even if it's not over the form? Or is there a better way to approach this problem? (This solution seems fairly process intesive as the mouse move event is constantly therefore running the sub all the time.)


Any help is greatly appreicated.

Cheers
Ben
 
Last edited:
Your application will only recieve windows messages that are ment for your application. That is to say, that if the cursor is not over any of your applications windows no messages will be sent and therefore no events will be raised. You need to use some Win32 API to get the cursor position from anywhere. In your timer tick event you need to use the GetCursorPos API, the declaration goes something like..

VB.NET:
<DllImport("User32.dll")> _
Friend Shared Function GetCursorPos(byref lpPoint as POINTAPI) as Integer
End Function
 
<StructLayout(LayoutKind.Sequential)>
Friend Structure POINTAPI
Dim x as Integer
Dim y as Integer
End Structure
 
Thanks for your reply.

My code uses the mouse move event, but the code you added just returns the position of the cursor (which I'm currently getting from using cursor.position). Is there anyway of getting the mouse move event to be raised when not over my form? (maybe from an external dll call?)

Cheers
Ben
 
I have written a library that contains a class for trapping Mouse Events and a class for trapping Keyboard events both of which you use as a normal .NET object inside your application if you are interested. Here is an example of how my .dll works.

VB.NET:
[FONT=Times New Roman][SIZE=3]Dim WithEvents MyMouse as new TripleG.GlobalHooks.MouseHook()[/SIZE][/FONT]
 
[FONT=Times New Roman][SIZE=3]Public Sub MouseDown (ByVal Sender as Object, ByVal e as TripleG_MouseDownEventArgs) Handles MyMouse.MouseDown[/SIZE][/FONT]
 
[FONT=Times New Roman][SIZE=3]If e.X = …..[/SIZE][/FONT]
 
[FONT=Times New Roman][SIZE=3]If e.Button = MouseButtons.Left ….[/SIZE][/FONT]
 
[FONT=Times New Roman][SIZE=3]Ect..[/SIZE][/FONT]
 
[FONT=Times New Roman][SIZE=3]End Sub[/SIZE][/FONT]
 
 
[FONT=Times New Roman][SIZE=3]Also for keyboards….[/SIZE][/FONT]
 
[FONT=Times New Roman][SIZE=3]Dim WithEvents MyKeyboard as new TripleG.GlobalHooks.KeyboardHook()[/SIZE][/FONT]
 
[FONT=Times New Roman][SIZE=3]Public Sub KeysFired (ByVal Sender as Object, ByVal e as TripleG_KeyboardEventArgs) Handles MyKeyboard.KeysFired[/SIZE][/FONT]
 
[FONT=Times New Roman][SIZE=3]If e.IsKeyDown(Keys.MediaBack) then …[/SIZE][/FONT]
 
[FONT=Times New Roman][SIZE=3]If e.LeftControlIsDown then …..[/SIZE][/FONT]
 
[FONT=Times New Roman][SIZE=3]‘CurrentKey is from a Keys Enum, the same one used in the windows forms.[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Select Case e.CurrentKey [/SIZE][/FONT]
[SIZE=3][FONT=Times New Roman]    Case A[/FONT][/SIZE]
 
[SIZE=3][FONT=Times New Roman]    Case ArrowUp[/FONT][/SIZE]
 
[SIZE=3][FONT=Times New Roman]    Case Space[/FONT][/SIZE]
 
[FONT=Times New Roman][SIZE=3]End Select[/SIZE][/FONT]
 
 
[FONT=Times New Roman][SIZE=3]If e.CurrentKey = Keys.X then[/SIZE][/FONT]
 
[SIZE=3][FONT=Times New Roman]    ‘this will now allow the X key to be delievered to any windows application. [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]     e.Cancel = True[/FONT][/SIZE]
 
[FONT=Times New Roman][SIZE=3]End If[/SIZE][/FONT]
 
 
[FONT=Times New Roman][SIZE=3]End Sub[/SIZE][/FONT]

I have spent a lot of time on this Library and it's still not what I want to claim it perfect but I had other developers in mind when I wrote it and it can be used in any .NET 2.0 application as an object. This makes hooking the mouse and keyboard very simple for the developer. I have also placed a lot of documention inside it so you shouldn't have any trouble just using it and glancing at the fields ect and letting intellisense tell you what they do.

I am building a website right now that will have a developers link that will include classses such as this for download. I have also written other nice classes that wrap API's and harness other things such as DirectX to make building Media features very easy.
 
Back
Top