How to find the window Title of Active(foreground) window using WindowScript Host

后端 未结 4 597
温柔的废话
温柔的废话 2020-11-30 11:56

I want to find the title of the window which is currently active(having focus) using Window Script Host(WSH) because I want my WSH script to Sendkeys only If the desired win

4条回答
  •  攒了一身酷
    2020-11-30 12:22

    You can make a COM object with GetForegroundWindow and GetWindowText.

    Put following lines into wso.cls and store is a folder called wso on your desktop.

    Imports System
    Imports System.Runtime.InteropServices
    Imports Microsoft.Win32
    
    Namespace WindowScriptingObject
    
         _
       Public Interface _WindowScriptingObject
             Function ActiveWindow() As Integer
             Function WindowText(ByVal hWnd As Integer) As String
        End Interface
    
         Public Class WindowScriptingObject
            Implements _WindowScriptingObject
    
            Public WindowScriptingObject()
    
            Public Declare Auto Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow"() As Integer
            Public Declare Auto Function GetWindowText Lib "user32.dll" (ByVal hwnd As Int32,  ByVal lpString As System.Text.StringBuilder, ByVal cch As Int32) As Int32
            Public Function ActiveWindow() As Integer Implements _WindowScriptingObject.ActiveWindow
        ActiveWindow=GetForegroundWindow()
    
            End Function
    
            Public Function WindowText(hwnd as Integer) As String Implements _WindowScriptingObject.WindowText
        on error resume next
        Dim b As New System.Text.StringBuilder(ChrW(0), 512)
                    Dim ret = GetWindowText(hWnd, b, b.Capacity)
        WindowText = b.tostring
            End Function
    
    
        End Class
    
    End Namespace
    

    Then create a bat file in same folder called wso.bat.

    "C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:library /out:"%userprofile%\desktop\wso\wso.dll" "%userprofile%\desktop\wso\wso.cls" /verbose
    
    "C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm" /codebase "%userprofile%\desktop\wso\wso.dll" /tlb:"%userprofile%\desktop\wso\wso.tlb" /v
    
    If /i "%cmdcmdline:~0,6%"=="cmd /c" pause
    

    To use in vbs after running bat file.

    Set wso=CreateObject("WindowScriptingObject")
    x = wso.ActiveWindow
    msgbox x, , "vbs"
    msgbox wso.windowtext(x), , "vbs"
    

    The GUIDs used here are specific to this project. Do not use them for other purposes.

    More info on what we are doing

    http://social.msdn.microsoft.com/Forums/en-US/adcae113-4758-481a-a367-60d5d14d97d6/this-is-how-to-turn-vbs-and-js-files-into-exe-files-from-the-command-line-without-third-party-tools?forum=scripting

    If you must do a per user install, use regasm to make a regfile instead of registering it. Then change all references to HKCR to HKCU\Software\Classes. Then merge with regedit /s regfile.reg.

    To move the file you need to run Regasm on it in it's new location. See command in bat file.

    Will be posed at MS site of course for accurate historical purposes.

提交回复
热议问题