Jump to content

Get Window Title below Mouse Cursor


Recommended Posts

I am looking for a way to get the window title of the window I am hovering with my mouse. The last days turned out to be unsuccessful though.

I have found numerous ways people seemed to have succeeded with this but none of the code I tried worked out so far.

Example by PsaltyDS taken from here

HotKeySet("{ESC}", "_Quit")
Global $Struct = DllStructCreate("int x;int y")

While 1
    Sleep(100)
    ToolTip("")
    Pos()
    $hwnd = WindowFromPoint()
    ToolTip($hwnd)
WEnd

Func Pos()
    DllStructSetData($Struct, "x", MouseGetPos(0))
    DllStructSetData($Struct, "y", MouseGetPos(1))
EndFunc ;==>Pos

Func WindowFromPoint()
    Local $x = DllStructGetData($Struct, "X")
    Local $y = DllStructGetData($Struct, "Y")
    Local $aResult = DllCall("User32.dll", "hwnd", "WindowFromPoint", "int", $x, "int", $y)
    Return $aResult[0]
EndFunc ;==>WindowFromPoint

Func _Quit()
    Exit
EndFunc ;==>_Quit

Doesn't really work. If I have a Firefox window on parts of the screen for example the script correctly returns the ff window handle. If I hover parts of the desktop the script returns the filemanager handle (also correctly). However, if I hover another window while the ff window is there (active or not) the script returns the filemanager handle as if the other window wasn't even there.

Au3Info returns the correct handle (or title) all the time so why do you think the au3 implementations don't work for me ?

I am using Vista x64 if that's relevant.

It also doesn't matter which MouseCoordMode I chose.

Any hints or things I could try ?

Edited by level20peon
Link to comment
Share on other sites

Use WinAPI_GetWindowText(handle) to get the title

#include <WinAPI.au3>

While 1
    Sleep(100)
    test()
WEnd

Func test()
    ToolTip("")
    $pos = _WinAPI_GetMousePos()
    $hwnd = _WinAPI_WindowFromPoint($pos)
    $nm = _WinAPI_GetWindowText($hwnd)
    ToolTip($nm)
EndFunc
Link to comment
Share on other sites

#include <WinAPI.au3>

While 1
    Sleep(100)
    test()
WEnd

Func test()
    $pos = _WinAPI_GetMousePos()
    $hwnd = _WinAPI_WindowFromPoint($pos)
    While _WinAPI_GetParent($hwnd) <> 0
        $hwnd = _WinAPI_GetParent($hwnd)
    Wend
    ToolTip($hwnd & " : " & WinGetTitle($hwnd))
EndFunc

This?

Link to comment
Share on other sites

Thanks for your help guys but the "bug" isn't really on either your or my side.

As mentioned earlier I am on a x64 OS. Seems that the WindowFromPoint API function doesn't handle the tagPoint structure the same way as a x86 OS. I get the correct values if I run the script in (x86) mode. Maybe some Dev could look into this matter ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...