Jump to content

how to modify this script a little further, so when i call it , it will return the window handle only?


Go to solution Solved by SOLVE-SMART,

Recommended Posts

i tried to get rid of the gui which i dont need , but many errors ocurred, everything seems interlaced together, please anyone help me to simply this code into a callable single function that will return the handle of the window which is under the mouse 

#include <GDIPlus.au3>
 
#include <WinAPISysWin.au3>
#include <WindowsConstants.au3>
 
HotKeySet("{ESC}""_Quit"; <===================
Local $hWnd_old = 0
 
; Create a structure that defines any point to be checked.
$tPoint = DllStructCreate($tagPOINT)
 
; Create a transparent GUI covering the desktop
$hGUI = GUICreate("Window viewer", @DesktopWidth, @DesktopHeight, 00$WS_POPUPBitOR($WS_EX_LAYERED$WS_EX_TOPMOST))
GUISetBkColor(0xABCDEF)
_WinAPI_SetLayeredWindowAttributes($hGUI0xABCDEF)
 
_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hPen = _GDIPlus_PenCreate(0xFFFF00004; alpha channel = 0xFF (opaque), red = 0xFF0000, width = 4 (pixels)
 
GUISetState(@SW_SHOW, $hGUI)
 
While 1
    Local $aMPos = MouseGetPos() ; screen coords
    DllStructSetData($tPoint"x"$aMPos[0])
    DllStructSetData($tPoint"y"$aMPos[1])
 
    ; $hParent = _WinAPI_GetParent(_WinAPI_WindowFromPoint($tPoint)) ; BAD (returns the handle of hidden ClassName RunDLL on some disabled controls)
    $hParent = _WinAPI_GetAncestor(_WinAPI_WindowFromPoint($tPoint), $GA_PARENT; ok
 
    $hWnd = 0
    $aChild = _WinAPI_EnumChildWindows($hParent)
 
    If Not @error Then
        For $i = 1 To $aChild[0][0]
            $hParent = _WinAPI_GetParent($aChild[$i][0])
            $tPoint = _WinAPI_GetMousePos(True$hParent; mouse coords relative to parent client area
 
            ; $hWnd = _WinAPI_WindowFromPoint($tPoint) ; BAD (returns the group control when mouse hovered over a control inside it...
            ;                                            ...also a disabled window handle isn't retrieved with this function)
            ; $hWnd = _WinAPI_ChildWindowFromPoint($hParent, $tPoint)   ; BAD (returns the group control)
            ; $hWnd = _WinAPI_ChildWindowFromPointEx($hParent, $tPoint) ; BAD (returns the group control)
            $hWnd = _WinAPI_RealChildWindowFromPoint($hParent$tPoint; ok (even on disabled controls) . Function not found in AutoIt
 
            If $hWnd = $aChild[$i][0Then
                $tRect = _WinAPI_GetWindowRect($hWnd)
                ExitLoop
            EndIf
        Next
    EndIf
 
    If $hWnd <> $hWnd_old Then
        _GDIPlus_GraphicsClear($hGraphic0xFFABCDEF)
        If $hWnd Then
            _GDIPlus_GraphicsDrawRect($hGraphic$tRect.Left, $tRect.Top, _
            $tRect.Right - $tRect.Left, $tRect.Bottom - $tRect.Top, $hPen)
            Local $ToolTipText = "Handle" & @TAB & $hWnd & @crlf & "Class" & @TAB & _WinAPI_GetClassName($hWnd)
 
            ; Ctrl ID in ToolTip shouldn't be used at all for top-level windows, then :
            If BitAND(_WinAPI_GetWindowLong($hWnd$GWL_STYLE), $WS_CHILD) = $WS_CHILD Then
                $ToolTipText &= @crlf & "Ctrl ID" & @TAB & _WinAPI_GetDlgCtrlID($hWnd)
            EndIf
 
            ToolTip($ToolTipText)
            ; ToolTip($ToolTipText, $tPoint.x + 10, $tPoint.y + 10, "", 0, 4) ; 4 = Force tooltip always visible
        EndIf
        $hWnd_old = $hWnd
    EndIf
    Sleep(10)
WEnd
 
Func _Quit()
    ; Clean up resources
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ShutDown()
    GUIDelete($hGUI)
    Exit
EndFunc   ;==>_Quit
 
; #FUNCTION# ====================================================================================================================
; Author.........: pixelsearch
; Modified.......:
; ===============================================================================================================================
Func _WinAPI_RealChildWindowFromPoint($hWnd$tPOINT)
    Local $aRet = DllCall('user32.dll''hwnd''RealChildWindowFromPoint''hwnd'$hWnd'struct'$tPOINT)
    If @error Then Return SetError(@error, @extended, 0)
 
    Return $aRet[0]
EndFunc   ;==>_WinAPI_RealChildWindowFromPoint
Edited by jacky998877
Link to comment
Share on other sites

Why are you opening a new thread when the old one isn't fulfilled yet?
@Nine and I already mentioned what you can do and you decide to post this arbitrary code to get this adjusted by one of us?

Please try it on your own at least 😒 .

Best regards
Sven

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Link to comment
Share on other sites

8 minutes ago, SOLVE-SMART said:

Why are you opening a new thread when the old one isn't fulfilled yet?
@Nine and I already mentioned what you can do and you decide to post this arbitrary code to get this adjusted by one of us?

Please try it on your own at least 😒 .

Best regards
Sven

it's fulfilled. i marked the solution already. this post is different. it's about modifying the code. the previous one is about finding the code

Link to comment
Share on other sites

58 minutes ago, jacky998877 said:

it's fulfilled. i marked the solution already. this post is different. it's about modifying the code. the previous one is about finding the code

Okay, fair enough.

Out of the aspects that you described in the other thread, this is a simple approach (a example) to know about the cursor enters/hovers the target window.

  • open your "Downloads" folder
  • resize the window (not maximize)
  • bring it to the background (unfocus this window)
  • do some mouse moves, to see no message box appears
  • bring the target window to the front (give focus)
  • hover the target window
    • => the message box will show you the handle (only in case the window is the active one)
    • => done
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y
#AutoIt3Wrapper_Run_Au3Stripper=y
#AutoIt3Wrapper_UseUpx=n
#Au3Stripper_Parameters=/sf /sv /mo /rm /rsln

Global $sTargetWindowTitle = 'Downloads'

While True
    Sleep(100)

    If Not _IsWindowActive($sTargetWindowTitle) Then
        ContinueLoop
    EndIf

    If _IsMouseOnWindow($sTargetWindowTitle) Then
        MsgBox('', 'Mouse on window', 'Handle: ' & WinGetHandle($sTargetWindowTitle))
        ExitLoop
    EndIf
WEnd

Func _IsWindowActive($sWindowTitle)
    Return (WinGetTitle('[ACTIVE]') == $sWindowTitle)
EndFunc

Func _IsMouseOnWindow($sWindowTitle)
    Local $aMouse  = MouseGetPos()
    Local $aWindow = WinGetPos($sWindowTitle)

    If $aMouse[0] >= $aWindow[0] And _
       $aMouse[1] >= $aWindow[1] And _
       $aMouse[0] <= $aWindow[0] + $aWindow[2] And _
       $aMouse[1] <= $aWindow[1] + $aWindow[3] Then
       Return True
    EndIf

    Return False
EndFunc

💡 I see no need to adjust your code above. I guess you can adjust my example to your following requirements.

Best regards
Sven

Edited by SOLVE-SMART

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Link to comment
Share on other sites

3 hours ago, SOLVE-SMART said:

Okay, fair enough.

Out of the aspects that you described in the other thread, this is a simple approach (a example) to know about the cursor enters/hovers the target window.

  • open your "Downloads" folder
  • resize the window (not maximize)
  • bring it to the background (unfocus this window)
  • do some mouse moves, to see no message box appears
  • bring the target window to the front (give focus)
  • hover the target window
    • => the message box will show you the handle (only in case the window is the active one)
    • => done
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y
#AutoIt3Wrapper_Run_Au3Stripper=y
#AutoIt3Wrapper_UseUpx=n
#Au3Stripper_Parameters=/sf /sv /mo /rm /rsln

Global $sTargetWindowTitle = 'Downloads'

While True
    Sleep(100)

    If Not _IsWindowActive($sTargetWindowTitle) Then
        ContinueLoop
    EndIf

    If _IsMouseOnWindow($sTargetWindowTitle) Then
        MsgBox('', 'Mouse on window', 'Handle: ' & WinGetHandle($sTargetWindowTitle))
        ExitLoop
    EndIf
WEnd

Func _IsWindowActive($sWindowTitle)
    Return (WinGetTitle('[ACTIVE]') == $sWindowTitle)
EndFunc

Func _IsMouseOnWindow($sWindowTitle)
    Local $aMouse  = MouseGetPos()
    Local $aWindow = WinGetPos($sWindowTitle)

    If $aMouse[0] >= $aWindow[0] And _
       $aMouse[1] >= $aWindow[1] And _
       $aMouse[0] <= $aWindow[0] + $aWindow[2] And _
       $aMouse[1] <= $aWindow[1] + $aWindow[3] Then
       Return True
    EndIf

    Return False
EndFunc

💡 I see no need to adjust your code above. I guess you can adjust my example to your following requirements.

Best regards
Sven

i am sorry man, you didnt get what i say, i tried your script, but i have to activate the "download" folder by clicking on it , so that  your script can detect the active window, but my request  is  something like this "without clicking on it by mouse, to know its handle just by hovering over it ", but thanks for the effort

Edited by jacky998877
Link to comment
Share on other sites

  • Solution

I know, @jacky998877, the purpose of the code was to show "a example" and I thought you will be able to adjust the rest on your own.
Just remove _IsWindowActive() and this should be it 🤞 .

#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y
#AutoIt3Wrapper_Run_Au3Stripper=y
#AutoIt3Wrapper_UseUpx=n
#Au3Stripper_Parameters=/sf /sv /mo /rm /rsln

Global $sTargetWindowTitle = 'Downloads'

While True
    Sleep(100)

    If _IsMouseOnWindow($sTargetWindowTitle) Then
        MsgBox('', 'Mouse on window', 'Handle: ' & WinGetHandle($sTargetWindowTitle))
        ExitLoop
    EndIf
WEnd

Func _IsMouseOnWindow($sWindowTitle)
    Local $aMouse  = MouseGetPos()
    Local $aWindow = WinGetPos($sWindowTitle)

    If $aMouse[0] >= $aWindow[0] And _
       $aMouse[1] >= $aWindow[1] And _
       $aMouse[0] <= $aWindow[0] + $aWindow[2] And _
       $aMouse[1] <= $aWindow[1] + $aWindow[3] Then
       Return True
    EndIf

    Return False
EndFunc

Best regards
Sven

Edited by SOLVE-SMART

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

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...