Jump to content

Vbs starter for the WinExplorer Tool, using AutoItX3


marc0v
 Share

Recommended Posts

WinExplorer is a very useful tool to get informations about windows/controls for further automation with AutoIt

(like AU3Info but with a different display of datas, a kind of global snapshot of all windows/controls)

WinExplorer is a freeware tool from http://www.nirsoft.net/utils/winexp.html

(I share no interest with them and the program is free)

Unhappily it has a non-resizeable window (too small) and a long treeview

So this starter vbs script resizes the window, and moves and resizes the controls inside it to make it more usable

This is a simple script, and could be applied to many dwarf-sized windows with long listview/treeview inside them

Most of the vbs code could then be reused just by changing :

Const winexp_class = "#32770"

Const winexp_title = "WinExplorer"

And the content of the SET_WINEXP_CONTROLS Sub (where the window controls are moved/resized)

This script has a lot of autoitx commands inside it, that's why it comes in this forum as an example...

Feel free to use it/change it/improve it.

Download link of the full zip with a help file and a pre-made shortcut

http://dl.free.fr/vtrq6QzXj

If the download link doesn't work any more, here is just the vbs code without the help file (not really needed anyway)

The script takes at least one (at most three) parameters :

the full path to WinExplorer executable with double quotes around, the width_ratio (defaults to 0.5), the height_ratio (defaults to 1)

Ratios are float numbers between 0 and 1, representing the size the WinExplorer window should be relatively to the desktop

And a typical command line to run it (folders could be different on your system !)

"C:\Windows\System32\WScript.exe" "C:\Program Files\WinExp\WinExpStarter.vbs" "C:\Program Files\WinExp\WinExp.exe" 0.5 0.75

Option Explicit
Public CURVBS
CURVBS = WScript.ScriptName

MAIN()

Sub MAIN()
    Dim arg_num, arg_list, cmdlinegeneric, cmdlinesample, syntax
    Dim arrwin, haserror, waitstart, mswaited, i
    Dim winexp_exefull, win_handle, winexp_procpid, winexp_winpid
    Dim win_ini_pos_x, win_ini_pos_y, win_ini_width, win_ini_height
    Dim win_set_width, win_set_height, width_ratio, height_ratio
    Dim winstate, minbit, maxbit, desktop_width, desktop_height
    Dim AutoItX3Obj
    Const noautoitx3 = "AutoItX3 activeX (AutoItX3.dll) does not seem to be registered on your system"
    Const mustinstall = "Please download from http://www.autoitscript.com/autoit3/ and install AutoIt3"
    Const syntaxerror = "Syntax error !"
    Const pathneeded = "Path to the WinExplorer program (WinExp.exe) is required as the first parameter (enclosed by double quotes)"
    Const toomanyparams = "Too many parameters have been passed to the script (maybe double quotes around WinExplorer path are missing and the arguments messed up)"
    Const maxparams = "Maximum number of parameters is 3 : a full path will double quotes, and 2 float numbers (between 0 and 1)"
    Const notnumericratio = "Not numeric ratio(s) have been passed to the script (maybe double quotes around WinExplorer path are missing and the arguments messed up)"
    Const yourarguments = "Your arguments :"
    Const procerror = "Unable to start process (provided path to WinExplorer program may be wrong)"
    Const winerror = "Unable to find the program window"
    Const desktoperror = "Unable to get desktop resolution"
    Const winrestoreerror = "Unable to restore the program window"
    Const winpossizeerror = "Unable to check the initial size or position of the program window"
    Const winexp_class = "#32770"
    Const winexp_title = "WinExplorer"
    Const opt_wintitlematchmode_left = 1
    Const opt_wintitlematchmode_exact = 3
    Const winminimized = 16
    Const winmaximized = 32
    Const stimeoutstart = 30
    Const mswaitloop = 500

    width_ratio = "0.5"
    height_ratio = "1"

    Set AutoItX3Obj = WS_CREATE_OBJ("AutoItX3.Control", False, False, False)
    If IS_NOTHING(AutoItX3Obj) Then
        MsgBox noautoitx3 & vbCr & mustinstall, vbCritical, CURVBS
        Exit Sub
    End If
    AutoItX3Obj.AutoItSetOption "WinWaitDelay", 0

    cmdlinegeneric = _
        Chr(34) & "{SystemDir}\WScript.exe" & Chr(34) & " " & _
        Chr(34) & "{PathToWinExpStarter}\WinExpStarter.vbs" & Chr(34) & " " & _
        Chr(34) & "{PathToWinExp}\WinExp.exe" & Chr(34) & " " & _
        "[DesktopWidthRatio] [DesktopHeightRatio]"
    cmdlinesample = _
        Chr(34) & "C:\Windows\System32\WScript.exe" & Chr(34) & " " & _
        Chr(34) & "C:\Program Files\WinExp\WinExpStarter.vbs" & Chr(34) & " " & _
        Chr(34) & "C:\Program Files\WinExp\WinExp.exe" & Chr(34) & " " & _
        CDbl(0.5) & " " & CDbl(0.75)

    syntax = "Full command line syntax :" & vbCr & cmdlinegeneric & vbCr & vbCr & _
        "eg : (this example will be COPIED TO YOUR CLIPBOARD when you close this dialog)" & vbCr & cmdlinesample & vbCr & vbCr & _
        "* " & Chr(34) & "{PathToWinExp}\WinExp.exe" & Chr(34) & _
        " is a MANDATORY parameter and is a fully qualified path to WinExplorer program (WinExp.exe)" & vbCr & _
        "eg : " & Chr(34) & "C:\Program Files\WinExp\WinExp.exe" & Chr(34) & vbCr & vbCr & _
        "* DesktopWidthRatio and DesktopHeightRatio are numbers between 0 and 1 : like 0.75" & vbCr & _
        "They are OPTIONAL and defaults to : [DesktopWidthRatio = " & width_ratio & "] [DesktopHeightRatio = " & height_ratio & "]"

    arg_num = WScript.Arguments.Count
    If arg_num < 1 Then
        MsgBox syntaxerror & vbCr & vbCr & pathneeded & vbCr & vbCr & syntax, vbCritical, CURVBS
        AutoItX3Obj.ClipPut cmdlinesample
        WScript.Quit
    End If

    arg_list = ""
    For i = 0 To (arg_num - 1)
        arg_list = arg_list & Chr(34) & WScript.Arguments.Item(i) & Chr(34) & " "
    Next

    If arg_num > 3 Then
        MsgBox syntaxerror & vbCr & vbCr & toomanyparams & vbCr & maxparams & vbCr & vbCr & syntax & vbCr & vbCr & yourarguments & vbCr & arg_list, _
            vbCritical, CURVBS
        AutoItX3Obj.ClipPut cmdlinesample
        WScript.Quit
    End If

    winexp_exefull = WScript.Arguments.Item(0)
    If arg_num > 1 Then
        width_ratio = WScript.Arguments.Item(1)
        If arg_num > 2 Then
            height_ratio = WScript.Arguments.Item(2)
        End If
    End If
    If (Not(IsNumeric(width_ratio)) Or Not(IsNumeric(height_ratio))) Then
        width_ratio = Replace(width_ratio, ",", ".")
        height_ratio = Replace(height_ratio, ",", ".")
    End If
    If (Not(IsNumeric(width_ratio)) Or Not(IsNumeric(height_ratio))) Then
        width_ratio = Replace(width_ratio, ".", ",")
        height_ratio = Replace(height_ratio, ".", ",")
    End If
    If (Not(IsNumeric(width_ratio)) Or Not(IsNumeric(height_ratio))) Then
        MsgBox syntaxerror & vbCr & vbCr & notnumericratio & vbCr & vbCr & syntax & vbCr & vbCr & yourarguments & vbCr & arg_list, _
            vbCritical, CURVBS
        AutoItX3Obj.ClipPut cmdlinesample
        WScript.Quit
    End If
    width_ratio = CDbl(width_ratio)
    If width_ratio < 0 Then
        width_ratio = 0
    ElseIf width_ratio > 1 Then
        width_ratio = 1
    End If
    height_ratio = CDbl(height_ratio)
    If height_ratio < 0 Then
        height_ratio = 0
    ElseIf height_ratio > 1 Then
        height_ratio = 1
    End If

    winexp_procpid = AutoItX3Obj.Run(winexp_exefull, "", AutoItX3Obj.SW_MINIMIZE)
    If (AutoItX3Obj.error <> 0 Or winexp_procpid <= 0) Then
        MsgBox procerror & vbCr & winexp_exefull, vbCritical, CURVBS
        Exit Sub
    End If
    winexp_procpid = CStr(winexp_procpid)

    AutoItX3Obj.AutoItSetOption "WinTitleMatchMode", opt_wintitlematchmode_exact
        waitstart = Now
        AutoItX3Obj.WinWait WINID_BY_TITLE_CLASS(winexp_title, winexp_class), "", stimeoutstart
        mswaited = DateDiff("s", waitstart, Now) * 1000

        winexp_winpid = "-1"
        Do While (winexp_winpid <> winexp_procpid And mswaited <= (stimeoutstart * 1000))
            arrwin = AutoItX3Obj.WinList(WINID_BY_TITLE_CLASS(winexp_title, winexp_class))

            For i = 1 To UBound(arrwin, 2)
                win_handle = arrwin(1, i)
                winexp_winpid = AutoItX3Obj.WinGetProcess(WINID_BY_HANDLE(win_handle))
                If winexp_winpid = "" Then winexp_winpid = "-1"
                If winexp_winpid = winexp_procpid Then Exit For
            Next

            WScript.Sleep mswaitloop
            mswaited = mswaited + mswaitloop
        Loop
    AutoItX3Obj.AutoItSetOption "WinTitleMatchMode", opt_wintitlematchmode_left

    If winexp_winpid <> winexp_procpid Then
        MsgBox winerror & vbCr & winexp_title, vbCritical, CURVBS
        Exit Sub
    End If

    GET_DESKTOP_SIZE desktop_width, desktop_height, AutoItX3Obj
    If (desktop_width <= 0 Or desktop_height <= 0) Then
        MsgBox desktoperror, vbCritical, CURVBS
        Exit Sub
    End If

    winstate = AutoItX3Obj.WinGetState(WINID_BY_HANDLE(win_handle))
    minbit = (winstate And winminimized) / winminimized
    If minbit = 1 Then
        AutoItX3Obj.WinSetState WINID_BY_HANDLE(win_handle), "", AutoItX3Obj.SW_RESTORE
    End If
    winstate = AutoItX3Obj.WinGetState(WINID_BY_HANDLE(win_handle))
    maxbit = (winstate And winmaximized) / winmaximized
    If maxbit = 1 Then
        AutoItX3Obj.WinSetState WINID_BY_HANDLE(win_handle), "", AutoItX3Obj.SW_RESTORE
    End If

    haserror = False
    winstate = AutoItX3Obj.WinGetState(WINID_BY_HANDLE(win_handle))
    If AutoItX3Obj.error <> 0 Then
        haserror = True
    Else
        minbit = (winstate And winminimized) / winminimized
        maxbit = (winstate And winmaximized) / winmaximized
        If (minbit = 1 Or maxbit = 1) Then haserror = True
    End If
    If haserror Then
        MsgBox winrestoreerror & vbCr & winexp_title, vbCritical, CURVBS
        Exit Sub
    End If

    haserror = False
    win_ini_pos_x = AutoItX3Obj.WinGetPosX(WINID_BY_HANDLE(win_handle))
    If AutoItX3Obj.error <> 0 Then haserror = True
    win_ini_pos_y = AutoItX3Obj.WinGetPosY(WINID_BY_HANDLE(win_handle))
    If AutoItX3Obj.error <> 0 Then haserror = True
    win_ini_width = AutoItX3Obj.WinGetPosWidth(WINID_BY_HANDLE(win_handle))
    If AutoItX3Obj.error <> 0 Then haserror = True
    win_ini_height = AutoItX3Obj.WinGetPosHeight(WINID_BY_HANDLE(win_handle))
    If AutoItX3Obj.error <> 0 Then haserror = True
    If (win_ini_width <= 0 Or win_ini_height <= 0) Then haserror = True
    If haserror Then
        MsgBox winpossizeerror & vbCr & winexp_title, vbCritical, CURVBS
        Exit Sub
    End If

    win_set_width = MAXOF(win_ini_width, Int(desktop_width * width_ratio))
    win_set_height = MAXOF(win_ini_height, Int(desktop_height * height_ratio))

    SET_WINEXP_WINDOW AutoItX3Obj, win_handle, win_ini_width, win_ini_height, _
        win_ini_pos_x, win_ini_pos_y, win_set_width, win_set_height
End Sub

Function MAXOF(ByVal num1, ByVal num2)
    If num1 > num2 Then
        MAXOF = num1
    Else
        MAXOF = num2
    End If
End Function

' Resize/Move **************************************************************************************
Sub SET_WINEXP_WINDOW(ByVal AutoItX3Obj, ByVal win_handle, ByVal win_last_width, ByVal win_last_height, _
        ByVal win_set_pos_x, ByVal win_set_pos_y, ByVal win_set_width, ByVal win_set_height)
    Dim x_diff, y_diff

    x_diff = win_set_width - win_last_width
    y_diff = win_set_height - win_last_height

    SET_WINEXP_CONTROLS win_handle, x_diff, y_diff, AutoItX3Obj
    AutoItX3Obj.WinMove WINID_BY_HANDLE(win_handle), "", win_set_pos_x, win_set_pos_y, win_set_width, win_set_height
End Sub

Sub SET_WINEXP_CONTROLS(ByVal win_handle, ByVal x_diff, ByVal y_diff, ByVal AutoItX3Obj)
    Const tree_controlnn = "SysTreeView321"
    Const tab_controlnn = "SysTabControl321"
    Const close_controlnn = "Button1"
    Const refresh_controlnn = "Button2"
    Const options_controlnn = "Button78"
    Const find_controlnn = "Button79"
    Const about_controlnn = "Button80"

    CONTROL_REL_SIZE win_handle, tree_controlnn     , x_diff    , y_diff    , AutoItX3Obj ' DOCK LEFT + RIGHT + TOP + BOTTOM
    CONTROL_REL_SIZE win_handle, tab_controlnn      , x_diff    , 0         , AutoItX3Obj ' DOCK LEFT + RIGHT + BOTTOM
    CONTROL_REL_MOVE win_handle, tab_controlnn      , 0         , y_diff    , AutoItX3Obj
    CONTROL_REL_MOVE win_handle, close_controlnn    , x_diff    , 0         , AutoItX3Obj ' DOCK RIGHT + TOP
    CONTROL_REL_MOVE win_handle, refresh_controlnn  , x_diff    , 0         , AutoItX3Obj ' DOCK RIGHT + TOP
    CONTROL_REL_MOVE win_handle, options_controlnn  , x_diff    , 0         , AutoItX3Obj ' DOCK RIGHT + TOP
    CONTROL_REL_MOVE win_handle, find_controlnn     , x_diff    , 0         , AutoItX3Obj ' DOCK RIGHT + TOP
    CONTROL_REL_MOVE win_handle, about_controlnn    , x_diff    , 0         , AutoItX3Obj ' DOCK RIGHT + TOP

    AutoItX3Obj.ControlHide WINID_BY_HANDLE(win_handle), "", close_controlnn ' small display bug workaround
    AutoItX3Obj.ControlShow WINID_BY_HANDLE(win_handle), "", close_controlnn

    AutoItX3Obj.ControlFocus WINID_BY_HANDLE(win_handle), "", tree_controlnn
    AutoItX3Obj.ControlSend WINID_BY_HANDLE(win_handle), "", tree_controlnn, "{END}" 'focus window list and get to end
End Sub

Sub CONTROL_REL_SIZE(ByVal win_handle, ByVal id_controlnn, ByVal x_diff, ByVal y_diff, ByVal AutoItX3Obj)
    Dim ctrl_pos_x, ctrl_pos_y, ctrl_width, ctrl_height, mswaited, haserror
    Const mswaitcontrol = 5000
    Const mswaitloop = 10

    mswaited = 0
    Do
        haserror = False
        ctrl_pos_x = AutoItX3Obj.ControlGetPosX(WINID_BY_HANDLE(win_handle), "", id_controlnn)
        If AutoItX3Obj.error <> 0 Then haserror = True
        ctrl_pos_y = AutoItX3Obj.ControlGetPosY(WINID_BY_HANDLE(win_handle), "", id_controlnn)
        If AutoItX3Obj.error <> 0 Then haserror = True
        ctrl_width = AutoItX3Obj.ControlGetPosWidth(WINID_BY_HANDLE(win_handle), "", id_controlnn)
        If AutoItX3Obj.error <> 0 Then haserror = True
        ctrl_height = AutoItX3Obj.ControlGetPosHeight(WINID_BY_HANDLE(win_handle), "", id_controlnn)
        If AutoItX3Obj.error <> 0 Then haserror = True
        If (ctrl_width <= 0 Or ctrl_height <= 0) Then haserror = True

        WScript.Sleep mswaitloop
        mswaited = mswaited + mswaitloop
    Loop Until (Not(haserror) Or mswaited > mswaitcontrol)

    If haserror Then Exit Sub

    AutoItX3Obj.ControlMove WINID_BY_HANDLE(win_handle), "", id_controlnn, ctrl_pos_x, ctrl_pos_y, ctrl_width + x_diff, ctrl_height + y_diff
End Sub

Sub CONTROL_REL_MOVE(ByVal win_handle, ByVal id_controlnn, ByVal x_diff, ByVal y_diff, ByVal AutoItX3Obj)
    Dim ctrl_pos_x, ctrl_pos_y, mswaited, haserror
    Const mswaitcontrol = 5000
    Const mswaitloop = 10

    mswaited = 0
    Do
        haserror = False
        ctrl_pos_x = AutoItX3Obj.ControlGetPosX(WINID_BY_HANDLE(win_handle), "", id_controlnn)
        If AutoItX3Obj.error <> 0 Then haserror = True
        ctrl_pos_y = AutoItX3Obj.ControlGetPosY(WINID_BY_HANDLE(win_handle), "", id_controlnn)
        If AutoItX3Obj.error <> 0 Then haserror = True

        WScript.Sleep mswaitloop
        mswaited = mswaited + mswaitloop
    Loop Until (Not(haserror) Or mswaited > mswaitcontrol)

    If haserror Then Exit Sub

    AutoItX3Obj.ControlMove WINID_BY_HANDLE(win_handle), "", id_controlnn, ctrl_pos_x + x_diff, ctrl_pos_y + y_diff
End Sub

' Get Sizes/Position *******************************************************************************
Sub GET_DESKTOP_SIZE(ByRef desktop_width, ByRef desktop_height, ByVal AutoItX3Obj)
    Dim explorer_short_u_proc, explorer_procpid, explorer_winpid, progman_handle, win_width, win_height
    Const progman_class = "Progman"

    explorer_short_u_proc = "EXPLORER.EXE"

    desktop_width = 0
    desktop_height = 0

    If IS_NOTHING(AutoItX3Obj) Then Exit Sub

    progman_handle = AutoItX3Obj.WinGetHandle(WINID_BY_CLASS(progman_class))
    If progman_handle = "" Then Exit Sub

    explorer_procpid = CStr(AutoItX3Obj.ProcessExists(explorer_short_u_proc))
    explorer_winpid = AutoItX3Obj.WinGetProcess(WINID_BY_HANDLE(progman_handle))
    If (explorer_winpid = "") Then explorer_winpid = "-1"
    If explorer_winpid <> explorer_procpid Then Exit Sub

    win_width = AutoItX3Obj.WinGetPosWidth(WINID_BY_HANDLE(progman_handle))
    If AutoItX3Obj.error <> 0 Then Exit Sub
    win_height = AutoItX3Obj.WinGetPosHeight(WINID_BY_HANDLE(progman_handle))
    If AutoItX3Obj.error <> 0 Then Exit Sub
    If (win_width <= 0 Or win_height <= 0) Then Exit Sub

    desktop_width = win_width
    desktop_height = win_height
End Sub

' AutoIt *******************************************************************************************
Function WINID_BY_HANDLE(ByVal win_handle)
    WINID_BY_HANDLE = "[HANDLE:" & Replace(win_handle, ";", ";;") & "]"
End Function
Function WINID_BY_CLASS(ByVal win_class)
    WINID_BY_CLASS = "[CLASS:" & Replace(win_class, ";", ";;") & "]"
End Function
Function WINID_BY_TITLE_CLASS(ByVal win_title, ByVal win_class)
    WINID_BY_TITLE_CLASS = "[TITLE:" & Replace(win_title, ";", ";;") & "; CLASS:" & Replace(win_class, ";", ";;") & "]"
End Function

' Standard *****************************************************************************************
Function IS_NOTHING(ByVal anobject)
    If TypeName(anobject) = TypeName(Nothing) Then
        IS_NOTHING = True
    Else
        IS_NOTHING = False
    End If
End Function

Function WS_CREATE_OBJ(ByVal strprogid, ByVal isquiet, ByVal isfatal, ByVal sysmodalerror)
    Dim msgtext, dlgico, msgtype, errnum, errdes
    Const fatalerrcreateobj = "FATAL error 'CreateObject' with"
    Const scriptclose = "The script will be CLOSED"
    Const errcreateobj = "Error 'CreateObject' with"
    Const unknownerr = "Unknown error"

    On Error Resume Next
        Set WS_CREATE_OBJ = WScript.CreateObject(strprogid)
        errnum = Err.Number
        errdes = Err.Description
    On Error GoTo 0
    If errnum <> 0 Then
        If errdes = "" Then errdes = unknownerr
        If Not(isquiet) Then
            msgtext = "[" & strprogid & "]" & vbCr & vbCr & errdes
            If isfatal Then
                msgtext = fatalerrcreateobj & vbCr & msgtext & vbCr & vbCr & scriptclose
                dlgico = vbCritical
            Else
                msgtext = errcreateobj & vbCr & msgtext
                dlgico = vbExclamation
            End If
            If sysmodalerror Then msgtype = vbSystemModal Else msgtype = 0
            MsgBox msgtext, dlgico + msgtype, CURVBS
        End If
        Set WS_CREATE_OBJ = Nothing
        If isfatal Then WScript.Quit
    End If
End Function
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...