Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get mouse position over controls?

Status
Not open for further replies.

tedsmith

Programmer
Nov 23, 2000
1,762
AU
I want to make a simple 'hover' function that shows a help window on any control on the form (like a much bigger tooltips thingy) without pressing a button.
Unfortunately the mousemove event of the form does not fire when you hover the mouse over a control on a form, only when over the blank form itself.
Is there some other way that will read the mouse cursor position no matter what it is currently over?
 
VB fires the MouseMove event of the control on which the mouse is being moved. You can place your code in the MouseMove event of controls of your interest if you want to be notified about it.

Alternatively, you can also query upon request a reference to the control which is currently under mouse. (even if the mouse is not moving.)

Add several controls on your form and paste the following code in the code section.
___
Code:
Option Explicit
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
    x As Long
    y As Long
End Type

Private Function ControlUnderMouse() As Control
    Dim H As Long, P As POINTAPI
    On Error GoTo ErrorNohWnd
    GetCursorPos P
    H = WindowFromPoint(P.x, P.y)
    For Each ControlUnderMouse In Controls
        If ControlUnderMouse.hWnd = H Then Exit Function
ErrorNohWnd:
    Next
End Function

Private Sub Form_Activate()
    Dim C As Control
    While DoEvents
        Set C = ControlUnderMouse
        If C Is Nothing Then Caption = "" Else Caption = C.Name
    Wend
End Sub
___
Run the program and move the mouse over the controls. The name will appear in forms title.

Note: This code will not retrieve any Label or Image controls if they are currently under mouse.
 
Thanks. Unfortunately a command button doesnt have a mousemove event otherwise it would be simple.
 

??????????Huh??????????

>>>>>>>>>>Thanks. Unfortunately a command button doesnt have a mousemove event otherwise it would be simple.

[tt]
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

End Sub
[tt]

That is,... a standard visual basic command button does have a mouse move event. What command button are you using????

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top