1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Get The Handle Of A Window

Discussion in 'T&T - Scripting' started by Joe Caverly, Dec 8, 2012.

  1. Joe Caverly

    Joined:
    Aug 28, 2009
    Messages:
    680
    Likes Received:
    8
    Code:
    ::-----------------------------------------------------
    :: WinGetHandle.btm
    ::
    :: Demonstrates how to obtain the handle of a
    ::  window, using VBScript and the
    ::  AutoIt COM Interface
    ::
    :: The AutoItX COM DLL can be downloaded from;
    ::  http://www.autoitscript.com/site/autoit/downloads/
    ::
    :: Joe Caverly
    :: 2012-12-08
    ::
    ::-----------------------------------------------------
    @setlocal
    @echo off
    ::-----------------------------------------------------
    ::
    :: Create the WinGetHandle.vbs script
    ::
    ::-----------------------------------------------------
    text > WinGetHandle.vbs
    Set oAutoIt = WScript.CreateObject("AutoItX3.Control")
    
    handle = oAutoIt.WinGetHandle("[CLASS:Notepad]", "")
    WScript.Echo handle
    endtext
    ::-----------------------------------------------------
    ::
    :: Run the WinGetHandle.vbs Script
    ::
    ::-----------------------------------------------------
    set hWnd=%@execstr[cscript //nologo WinGetHandle.vbs]
    ::-----------------------------------------------------
    ::
    :: The result was returned as hexadecimal
    :: Convert it to decimal
    ::
    ::-----------------------------------------------------
    set hWnd=%@convert[16,10,%hWnd]
    ::-----------------------------------------------------
    ::
    :: Display the Window Handle
    ::
    ::-----------------------------------------------------
    echo %hWnd
    ::-----------------------------------------------------
    ::
    :: Delete the WinGetHandle.vbs script
    ::
    ::-----------------------------------------------------
    if exist WinGetHandle.vbs del WinGetHandle.vbs > nul
    endlocal
    ::-----------------------------------------------------
    ::
    :: Using the AutoIt v3 Window Info tool, here are the
    ::  CLASS names for some apps on my system
    ::
    :: Notepad++      CLASS:Notepad++
    :: KeyNote 1.6.5  CLASS:GFKeyNote10
    :: FireFox        CLASS:MozillaWindowClass
    :: Calculator     CLASS:SciCalc
    ::
    :: You can also use the partial or complete title of
    ::  a window, instead of the CLASS name;
    ::
    :: handle = oAutoIt.WinGetHandle("Take Com","")
    ::
    :: Refer to the AutoItX Help file for detailed info
    ::  on the WinGetHandle method
    ::
    :: Another method, if you know the CLASS name for the
    ::  app, is to use the @WINAPI Variable Function
    ::  as follows;
    ::
    :: findwindow=`%@winapi[user32,FindWindow,"%1",0]`
    ::
    :: then;
    ::
    :: echo %@findwindow[Notepad]
    ::
    ::-----------------------------------------------------
     

Share This Page