How to get the tooltip value when hovering the mouse anywhere on the desktop

Mansour_Dalir 1,876 Reputation points
2024-09-29T06:03:39.5933333+00:00

I'm working on a VB project and I need to capture the tooltip value when hovering over the desktop. Can someone help me figure out how to do this? Is this reversible?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,628 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,728 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 85,371 Reputation points
    2024-09-29T07:24:15.4866667+00:00

    You can use SetWinEventHook :

        Dim procDelegate As WinEventProcDelegate
        Dim hEventHook As IntPtr
    

    In Form1_Load :

        procDelegate = New WinEventProcDelegate(AddressOf WinEventProc)
        hEventHook = SetWinEventHook(EVENT_MIN, EVENT_MAX, IntPtr.Zero, procDelegate, 0, 0, WINEVENT_OUTOFCONTEXT)
    

    Then :

    Private Sub WinEventProc(hWinEventHook As IntPtr, eventConst As UInteger, hWnd As IntPtr, idObject As Integer, idChild As Integer, dwEventThread As UInteger, dwmsEventTime As UInteger)
        If (eventConst = EVENT_OBJECT_SHOW) Then
            Dim sb As New StringBuilder(260)
            GetClassName(hWnd, sb, sb.Capacity)
            'Console.WriteLine(sb.ToString())
            If (sb.ToString() = "tooltips_class32") Then
                'Console.Beep(8000, 10)
                Dim sbTooltipText As New StringBuilder(260)
                SendMessage(hWnd, WM_GETTEXT, New IntPtr(260), sbTooltipText)
                Console.WriteLine("Tooltip Text: " & sbTooltipText.ToString())
            End If
        End If   
    End Sub
    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        If (hEventHook <> IntPtr.Zero) Then
            UnhookWinEvent(hEventHook)
        End If
    End Sub
    

    With declarations :

        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Private Shared Function SetWinEventHook(eventMin As UInteger, eventMax As UInteger, hmodWinEventProc As IntPtr, pfnWinEventProc As WinEventProcDelegate, idProcess As UInteger, idThread As UInteger, dwFlags As UInteger) As IntPtr
        End Function
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function UnhookWinEvent(hWinEventHook As IntPtr) As Boolean
        End Function
        Delegate Sub WinEventProcDelegate(hWinEventHook As IntPtr, eventType As UInteger, hwnd As IntPtr, idObject As Integer, idChild As Integer, dwEventThread As UInteger, dwmsEventTime As UInteger)
        Public Const WINEVENT_OUTOFCONTEXT = &H0  ' Events are ASYNC
        Public Const WINEVENT_SKIPOWNTHREAD = &H1  ' Don't call back for events on installer's thread
        Public Const WINEVENT_SKIPOWNPROCESS = &H2  ' Don't call back for events on installer's process
        Public Const WINEVENT_INCONTEXT = &H4  ' Events are SYNC, this causes your dll To be injected into every process
        Public Const EVENT_MIN = &H1
        Public Const EVENT_MAX = &H7FFFFFFF
        ' Does not work on Windows 10
        Public Const EVENT_SYSTEM_SWITCHSTART = &H14
        Public Const EVENT_SYSTEM_SWITCHEND = &H15
        Public Const EVENT_SYSTEM_FOREGROUND = &H3
        Public Const EVENT_OBJECT_CREATE = &H8000  ' hwnd + ID + idChild is created item
        Public Const EVENT_OBJECT_DESTROY = &H8001  ' hwnd + ID + idChild is destroyed item
        Public Const EVENT_OBJECT_SHOW = &H8002  ' hwnd + ID + idChild is shown item
        Public Const EVENT_OBJECT_HIDE = &H8003  ' hwnd + ID + idChild is hidden item
        Public Const EVENT_OBJECT_REORDER = &H8004  ' hwnd + ID + idChild is parent of zordering children
        Public Const EVENT_OBJECT_FOCUS = &H8005  ' hwnd + ID + idChild is focused item
        Public Const EVENT_OBJECT_SELECTION = &H8006  ' hwnd + ID + idChild is selected item (if only one), or idChild is OBJID_WINDOW if complex
        Public Const EVENT_OBJECT_SELECTIONADD = &H8007  ' hwnd + ID + idChild is item added
        Public Const EVENT_OBJECT_SELECTIONREMOVE = &H8008  ' hwnd + ID + idChild is item removed
        Public Const EVENT_OBJECT_SELECTIONWITHIN = &H8009  ' hwnd + ID + idChild is parent of changed selected items
        Public Const EVENT_OBJECT_STATECHANGE = &H800A  ' hwnd + ID + idChild is item w/ state change
        Public Const EVENT_OBJECT_LOCATIONCHANGE = &H800B  ' hwnd + ID + idChild is moved/sized item
        Public Const EVENT_OBJECT_NAMECHANGE = &H800C  ' hwnd + ID + idChild is item w/ name change
        Public Const EVENT_OBJECT_DESCRIPTIONCHANGE = &H800D  ' hwnd + ID + idChild is item w/ desc change
        Public Const EVENT_OBJECT_VALUECHANGE = &H800E  ' hwnd + ID + idChild is item w/ value change
        Public Const EVENT_OBJECT_PARENTCHANGE = &H800F  ' hwnd + ID + idChild is item w/ new parent
        Public Const EVENT_OBJECT_HELPCHANGE = &H8010  ' hwnd + ID + idChild is item w/ help change
        Public Const EVENT_OBJECT_DEFACTIONCHANGE = &H8011  ' hwnd + ID + idChild is item w/ def action change
        Public Const EVENT_OBJECT_ACCELERATORCHANGE = &H8012  ' hwnd + ID + idChild is item w/ keybd accel change
        Public Const EVENT_OBJECT_INVOKED = &H8013  ' hwnd + ID + idChild is item invoked
        Public Const EVENT_OBJECT_TEXTSELECTIONCHANGED = &H8014  ' hwnd + ID + idChild is item w? test selection change
        Public Const EVENT_OBJECT_CONTENTSCROLLED = &H8015
        Public Const EVENT_SYSTEM_ARRANGMENTPREVIEW = &H8016
        Public Const EVENT_OBJECT_CLOAKED = &H8017
        Public Const EVENT_OBJECT_UNCLOAKED = &H8018
        Public Const EVENT_OBJECT_LIVEREGIONCHANGED = &H8019
        Public Const EVENT_OBJECT_HOSTEDOBJECTSINVALIDATED = &H8020
        Public Const EVENT_OBJECT_DRAGSTART = &H8021
        Public Const EVENT_OBJECT_DRAGCANCEL = &H8022
        Public Const EVENT_OBJECT_DRAGCOMPLETE = &H8023
        Public Const EVENT_OBJECT_DRAGENTER = &H8024
        Public Const EVENT_OBJECT_DRAGLEAVE = &H8025
        Public Const EVENT_OBJECT_DRAGDROPPED = &H8026
        Public Const EVENT_OBJECT_IME_SHOW = &H8027
        Public Const EVENT_OBJECT_IME_HIDE = &H8028
        Public Const EVENT_OBJECT_IME_CHANGE = &H8029
        Public Const EVENT_OBJECT_TEXTEDIT_CONVERSIONTARGETCHANGED = &H8030
        Public Const EVENT_OBJECT_END = &H80FF
        Public Const OBJID_WINDOW = &H0
        Public Const OBJID_SYSMENU = &HFFFFFFFF
        Public Const OBJID_TITLEBAR = &HFFFFFFFE
        Public Const OBJID_MENU = &HFFFFFFFD
        Public Const OBJID_CLIENT = &HFFFFFFFC
        Public Const OBJID_VSCROLL = &HFFFFFFFB
        Public Const OBJID_HSCROLL = &HFFFFFFFA
        Public Const OBJID_SIZEGRIP = &HFFFFFFF9
        Public Const OBJID_CARET = &HFFFFFFF8
        Public Const OBJID_CURSOR = &HFFFFFFF7
        Public Const OBJID_ALERT = &HFFFFFFF6
        Public Const OBJID_SOUND = &HFFFFFFF5
        Public Const OBJID_QUERYCLASSNAMEIDX = &HFFFFFFF4
        Public Const OBJID_NATIVEOM = &HFFFFFFF0
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function GetClassName(hWnd As IntPtr, lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer
        End Function
        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As IntPtr, lParam As StringBuilder) As IntPtr
        End Function
        Public Const WM_GETTEXT As Integer = &HD
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.