Form - Mouse Hover

Cheetah

Well-known member
Joined
Oct 12, 2006
Messages
232
Programming Experience
Beginner
Hi there,

I want to create something so that if my mouse enters anywhere that is within the forms boundaries even if its onto an object which is on the form.

And another even when it leaves the boundaries.

Is this possible?

Thanks.
 
The one for when the mouse leaves is pretty straight forward, you can just handle the MouseLeave Event. The one for inside the form is slightly more problematic as each control will 'describe' the mouse position relative to it's client co-ordinates. You will probably need to use a bit of Win32.

Declare Function PtInRegion Lib "gdi32" Alias "PtInRegion" ( _ByVal hRgn As intptr, _ByVal x As int32, _ByVal y As int32_) As int32

So this function will return a value 1 or 0 i meaning the xy co-ordinates are inside the region given.
You get the region like this...

Dim hRgn as intptr = Mybase.Region.GetHrgn

Put this in a timer tick event and there you go.
 
The one for when the mouse leaves is pretty straight forward, you can just handle the MouseLeave Event. .

Thanks for your reply mate. Ill have a quick play around with it

In regards to what i quoted. When you hover on a form object, it thinks that it has left the form (well on my app it does). Just thought i'd say that.

I should be able to use the function you provided to cover both of the things i wanted.
 
You will probably need to use a bit of Win32.

Declare Function PtInRegion Lib "gdi32" Alias "PtInRegion" ( _ByVal hRgn As intptr, _ByVal x As int32, _ByVal y As int32_) As int32

So this function will return a value 1 or 0 i meaning the xy co-ordinates are inside the region given.
You get the region like this...

Dim hRgn as intptr = Mybase.Region.GetHrgn

Put this in a timer tick event and there you go.

Ok, bit confused over this already.

The Function you provided (i guess) is that I put in the Mybase.Region.GetHrgn and some coordinates (but what coordinates)? the Region.GetHrgn also requires something itself, a graphic, what is that?

The function then gives a 1 if the coordinates supplied and inside the given region and a 0 if not.

What I need to do is systematically check this, so I have to check whether its a one or a zero systematically, which means i would put it in a timer, so on tick it would check.

===

Sorry I am a beginner to this and this is the first time i have ever used the whole Win32 thing.
 
Ahh, yes it does require a graphics context to create the hrgn. Mmmm, maybe i over complicated this.

Add this somewhere in the class..

VB.NET:
Private Declare Function GetCursorPos Lib "user32.dll" (ByRef lpPoint As POINTAPI) As Int32
 
<StructLayout(LayoutKind.Sequential)> _ 
Private Structure POINTAPI
 Public x As Int32
 Public y As Int32
End Structure
 
Private Pnt as PointAPI

Drop a label on form and a button.

In the timer tick event put the following...

VB.NET:
GetCursorpos(pnt)
 
me.label1.text  = "Cursor x Pos : " & pnt.x.tostring & " Cursor y pos : " & Pnt.y.tostring

Then in the button click event put

VB.NET:
Me.Timer1.Enabled = true


From there the text in the label should change to reflect the co-ordinates of the mouse. If i've got it right let me know.
 
Yes that works lovely, thanks.

It gives the coordinates of the mouse in the working area as expected.

Now just to check im on the same wavelength:

I am guessing i now just check whether this is in the form, by checking whether its in between the forms location.X and the forms width+location.X and the same for the height and y location.
 
I agree using a Timer, the rest can be done with System.Windows.Forms.Cursor.Position and a defined Rectangle and PointToClient method. Example:
VB.NET:
Dim r As New Rectangle(100, 100, 100, 100) 'defined relative to form

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim pt As Point = Me.PointToClient(System.Windows.Forms.Cursor.Position)
    If r.Contains(pt) Then
        Me.Text = "within bounds"
    Else
        Me.Text = "outside bounds"
    End If
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    'draw some visual cue:
    e.Graphics.DrawRectangle(Pens.Red, r)
End Sub
 
Yep, Or you could just do


VB.NET:
If me.ClientRectangle.Contains(Pnt.X,Pnt.Y) then
 
'do what ever
 
else
 
'do something else
 
end if

EDIT : JohnH, that wasn't a reply to your post, we must have hit the button at the same time.
Cheetah, two examples of how to solve the same problem. Pick which ever floats your boat. JohnH's example uses native Framework methods mine uses Win32 neither wrong or right but the preferrred way would be JohnH's
 
Ah ok i see now.

Thanks for your help.

Much appreciated.

I decided to go with johns method as i already had a rectangle for another part of my program.
 
Back
Top