• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

what Control is the mouse over?

what is the best way to find out which control the mouse is over (only one form)
Without having to fill the mousemove from every control
0
Mark_FreeSoftware
Asked:
Mark_FreeSoftware
1 Solution
 
iHadiCommented:
Hi Mark_FreeSoftware

To get the handle of the control the mouse is over, add a timer and any other controls on your form and past the following code:

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
Private Declare Function ChildWindowFromPoint Lib "user32" (ByVal hwnd As Long, ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long

Private Sub Timer1_Timer()
Dim CursorPos As POINTAPI
Dim pt As POINTAPI
Dim ChildHandle As Long
Dim ClientRect As RECT
Dim Res As Long

Res = GetCursorPos(CursorPos)
ClientToScreen Me.hwnd, pt
ChildHandle = ChildWindowFromPoint(Me.hwnd, CursorPos.X - pt.X, CursorPos.Y - pt.Y)
Label1 = pt.X & " " & pt.Y
End Sub
0
 
Mark_FreeSoftwareAuthor Commented:
sorry for my late reaction, was quite busy.


it works!
thank you very much






for future reference, here is the full code i used:


Option Explicit

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Type POINTAPI
        x As Long
        y As Long
End Type

Private Declare Function ChildWindowFromPoint Lib "user32" (ByVal hwnd As Long, ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long

Private Sub Timer1_Timer()
Dim CursorPos As POINTAPI
Dim pt As POINTAPI
Dim ChildHandle As Long
Dim ClientRect As RECT
Dim Res As Long

Res = GetCursorPos(CursorPos)
ClientToScreen Me.hwnd, pt
ChildHandle = ChildWindowFromPoint(Me.hwnd, CursorPos.x - pt.x, CursorPos.y - pt.y)
Select Case ChildHandle
Case Command1.hwnd
   Label1 = Command1.Name
Case Command2.hwnd
   Label1 = Command2.Name
Case Command3.hwnd
   Label1 = Command3.Name
Case Command4.hwnd
   Label1 = Command4.Name
End Select
End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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