[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

Is there a way to find the hWnd of a label control?

I want to detect mouse overs, and when the mouse is no longer over my label, I need to know that too. The problem is, the only way I know to do that is with the SetCapture and ReleaseCapture which binds the mouse to a given control using the hWnd. But a label has no exposed hWnd... So how can I get the hWnd if it isn't exposed?
0
schworak
Asked:
schworak
1 Solution
 
VbmasterCommented:
The GetCursorPos API will tell you where the mouse is..

Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (lpPoint As POINTAPI) As Long
Type POINTAPI
        x As Long
        y As Long
End Type
0
 
schworakAuthor Commented:
Yes, but it won't tell me when the mouse moves off of a control as an event. Using the capture call, the mouse move event is triggered even if I am off of the control.
0
 
AnswerTheManCommented:
it may be a little clumsy, but it's offhand, and if you'll give it a final touch it will work perfect :

suppose the Given Label is 'Label1'.
add a PictureBox with no picture, minimum size, UnVisible to the form.
Add another Label :'lblMsg'

add this code :

Private Sub Label1_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
    If State = 1 Then
        lblMsg = "Mouse left label1"
        Picture1.Drag 0
    End If
End Sub

Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    lblMsg = "Mouse is over label1"
    Picture1.Drag
End Sub


as i said, you only need a final touch to make it perfect.

''reject this if you expect more elegant solution
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
amit_panjwaniCommented:
use this API

 Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos"
             (lpPoint As POINTAPI) As Long
             Type POINTAPI
                     x As Long
                     y As Long
             End Type

Place a Timer on form,

Now Set interval to approx 1000

within Timer's Sub

Put Code for algorithm

Select

Case Mouse_Coordianates = Label Co ordinated

Do MyThing

It works out for me.

Timer keeps on polling for mouse co ordinates and will let u know what to do on mouseovers






0
 
schworakAuthor Commented:
I would still like to be able to find the hWnd of opjects that don't expose that value by default, but that code works well.

I found a slightly more complex method to solve my current problem but I like the simplicity of this answer.

I added a little code to clean it up a bit. Now it works great.

Private Sub Form_Load()
    Picture1.Height = 1
    Picture1.Width = 1
End Sub

Private Sub Label1_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
    If State = 1 Then
        lblmsg = "Mouse left label1"
        Picture1.Drag 0
    End If
End Sub

Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    lblmsg = "Mouse is over label1"
    Picture1.Left = Label1.Left + X
    Picture1.Top = Label1.Top + Y
    Picture1.Drag
End Sub




Thanks for putting me on the right track.
0
 
schworakAuthor Commented:
amit_panjwani, the timer idea is great if I wanted to keep polling like that. but I need this to be triggered by an event. The code above works well enough. Really well actually.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now