Milestone 相机选择器软件

Milestone Camera Selector Software

我正在尝试为工作创建一些 AutoIT 代码,以便更轻松地启动我们的相机系统。截至目前,我已经能够让我的代码启动一个图形用户界面,显示您可以添加到的摄像头服务器列表,并能够将新服务器添加到内部目录。我遇到的麻烦是程序应该切换到新启动的 Milestone 软件的部分,输入信息,然后单击 enter。我发现问题与 start() 函数的一部分有关,其中 WinWaitActive($handle) 要么没有使用正确的句柄,要么我使用的函数不正确?

我的最终目标是能够等待 4 秒,使用其句柄或 PID 将焦点从当前选择的任何 window 切换到新打开的里程碑 window,然后发送 2 TABs,ip地址,然后回车

#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <ComboConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>

#Region EDIT THE BELOW INFORMATION-------------------------------------------------------------------------------------------------------------------------------------------
Local Const $milestone = "C:\Program Files\Milestone\XProtect Smart Client\Client.exe"          ;Path to software
Local $Title = "Milestone Camera Selector"                                                         
#EndRegion-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#Region Script---------------------------------------------------------------------------------------------------------------------------------------------------------------
Local $iFileExists = FileExists($milestone)                                                 ;Check if the file exists, returns a 1 or 0
Global $list = IniReadSection(@TempDir & "\" & $Title & "\config.ini","Server")             ;Read the ini file, this builds a 2D Array [X][0] is the key [X][1] is the value
HotKeySet("{F1}", "help")

If $iFileExists Then                                                                        ;If FileExists = 1 (exists) then
    If Not FileExists(@TempDir & "\" & $Title) Then                                         ;If config directory does not exists then
        Do
        DirCreate(@TempDir & "\" & $Title)                                                  ;Create a directory in TempDir for the config.ini file to be stored locally
        Until FileExists(@TempDir & "\" & $Title)
        IniWrite(@TempDir & "\" & $Title & "\config.ini", "Server", "", "")
    EndIf
    Local $GUI = GUICreate($Title, 267, 115)                                                ;Create the GUI
    $start = GUICtrlCreateButton("Start", 40, 72, 65, 25)                                   ;Create a button
    $create = GUICtrlCreateButton("Add Server", 160, 72, 65, 25)
    $server = GUICtrlCreateCombo("Select Server", 8, 8, 249, 25, $CBS_DROPDOWN)             ;Create the combo box
    For $i = 1 To UBound($list) - 1                                                         ;Populate combo box with first value (name) from array
        If $list[$i][0] <> "" Then GUICtrlSetData($server, $list[$i][0])                    ;Ensure array is not empty and fill combox with KEYS
    Next
    $servername = GUICtrlCreateInput("Server Name", 8, 40, 121, 21)
    GUICtrlSetState(-1, $GUI_HIDE)
    $serverip = GUICtrlCreateInput("IP Address", 136, 40, 121, 21)
    GUICtrlSetState(-1, $GUI_HIDE)

    GUISetState(@SW_SHOW)

    While 1                                                                                 ;Enter loop until user closes or presses button
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE                                                           ;Exit when closed
                Exit
            Case $start                                                                     ;Store selection into variable, delete the GUI, and run the start function
                $selection = GUICtrlRead($server)
                If $selection <> "Select Server" Then
                    GUIDelete()
                    start()
                EndIf
            Case $create
                If GUICtrlRead($create) = "Add Server" Then
                    GUICtrlSetState($servername, $GUI_SHOW)
                    GUICtrlSetState($serverip, $GUI_SHOW)
                    GUICtrlSetData($create, "Create")
                Else
                    If (GUICtrlRead($servername) <> "" And GUICtrlRead($servername) <> "Server Name" And GUICtrlRead($serverip) <> "" And GUICtrlRead($serverip) <> "IP Address") Then
                        IniWrite(@TempDir & "\" & $Title & "\config.ini", "Server", GUICtrlRead($servername), GUICtrlRead($serverip))
                        GUICtrlSetState($servername, $GUI_HIDE)
                        GUICtrlSetState($serverip, $GUI_HIDE)
                        GUICtrlSetData($create, "Add Server")
                    Else
                        MsgBox($MB_ICONINFORMATION, $Title, "Invalid Server Name and IP Address.")
                    EndIf
                    For $i = UBound($list) - 1 To 0 Step -1
                        _GUICtrlComboBox_DeleteString($server, $i)
                    Next
                    Local $list = IniReadSection(@TempDir & "\" & $Title & "\config.ini","Server")
                    For $i = 1 To UBound($list) - 1
                        If $list[$i][0] <> "" Then GUICtrlSetData($server, $list[$i][0])
                    Next
                EndIf
        EndSwitch
    WEnd
Else                                                                                        ;otherwise, message
    MsgBox($MB_SYSTEMMODAL, "", "Milestone XProtect wasn't found on this computer" & @CRLF)
EndIf

Func start()
    $iPID = Run($milestone,"", @SW_SHOWNOACTIVATE)                                          ;Runs the software and returns the Process ID
    Sleep(4000)                                                                             ;sleep for 4 seconds
    If @error Then MsgBox(0, $Title, "The program could not launch.")                       ;If Run returns a 0 or error, there was a problem
    $handle = _GetHandleFromPID($iPID)                                                      ;Retrieve the handle of the program ran, jump to the function below
    If $handle = 0 Then MsgBox(0, $Title, "The handle could not be found.")                 ;If the Handle returned is 0, there was no match
    Sleep(1000)
    WinWaitActive($handle)                                                                  ;Wait for the program to be activated 
    Send("{TAB}")                                                                           ;send keystrokes to active window
    Send("{TAB}")
    For $i = 0 to UBound($list) - 1                                                         ;Find the IP address that matches the selection and send it
        If $list[$i][0] = $selection Then Send($list[$i][1])
    Next
    Send("{ENTER}")
    Exit                                                                                    ;Exit for now until you can capture a succesful run
EndFunc

func _GetHandleFromPID($PID)                                                                ;Call function with the PID returned from Run function
    $WinList = WinList()                                                                    ;Assign WinList to a variable
    for $i = 1 to $WinList[0][0]                                                            ;Run through each Window in WinList, 2D array [titles][handles]
        If WinGetProcess($WinList[$i][1]) = $PID then                                       ;Look for a Window with the correct PID
            Return $WinList[$i][1]                                                          ;Assign the matching Windows handle to the variable
        EndIf
    Next
    Return 0                                                                                ;Return 0 if no matches were found
EndFunc

Func help()
    ShellExecute(@TempDir & "\" & $Title & "\config.ini")
EndFunc
#EndRegion--------------------------------------------------------------------------------------------------------------------------------------------------------------------

再一次,如果能帮助我完成几个月前开始的这个项目,我们将不胜感激。此外,如果您认为使用 python 或 AutoHotkey 等不同的编程语言更容易实现这一点,请告诉我! :)

我很抱歉去 MIA。我真的很忙,没有时间研究软件的细节。

本质上发生的事情是返回的 PID 与名为 CiceroUIWndFrame 的进程(后台的父进程 运行ning)相关联,并且它返回该进程的句柄。由于该进程永远不会被激活,脚本只是保持暂停状态,等待它弹出。不幸的是,由于 Milestone 软件没有自己的 PID(查看任务管理器中的“详细信息”选项卡,您会注意到 Milestone 软件仅被称为 client.exe),我们无法使用好的方法来获取正确的 PID手柄。

我知道的唯一解决方案是获取活动的句柄window。因此,此修改后的脚本将 运行 Milestone X Protect Software,等待 1 秒(希望足够长以启动)并获取任何活动程序的句柄。然后再休眠 4 秒,等待您再次激活客户端。因此,如果您在另一个 window 并等待几分钟,当您再次激活客户端登录时,它将发送击键。

此方法的缺点是您依赖于启动后的活动 window 成为里程碑(如果加载时间更长,您会得到错误的句柄,如果您在 1 之前单击失焦其次你得到了错误的句柄)。

#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <ComboConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>
#include <AutoItConstants.au3>

#Region EDIT THE BELOW INFORMATION-------------------------------------------------------------------------------------------------------------------------------------------
Local Const $milestone = "C:\Program Files\Milestone\XProtect Smart Client\Client.exe"  ;Path to software
Local $Title = "Program Title"                                                          ;Give me a name
Local $icon = "Full\Path\To\Icon.ico"
#EndRegion-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#Region Script---------------------------------------------------------------------------------------------------------------------------------------------------------------
Local $iFileExists = FileExists($milestone)                                     ;Check if the file exists, returns a 1 or 0
Global $list = IniReadSection(@TempDir & "\" & $Title & "\config.ini","Server") ;Read the ini file, this builds a 2D Array [X][0] is the key [X][1] is the value
HotKeySet("{F1}", "help")

If $iFileExists Then                                                            ;If FileExists = 1 (exists) then
    If Not FileExists(@TempDir & "\" & $Title) Then                             ;If config directory does not exists then
        Do
        DirCreate(@TempDir & "\" & $Title)                                      ;Create a directory in TempDir for the config.ini file to be stored locally
        Until FileExists(@TempDir & "\" & $Title)
        IniWrite(@TempDir & "\" & $Title & "\config.ini", "Server", "", "")
    EndIf
    Local $GUI = GUICreate($Title, 267, 115)                                    ;Create the GUI
    GUISetIcon($icon, -1)                                                       ;Create icon
    $start = GUICtrlCreateButton("Start", 40, 72, 65, 25)                       ;Create a button
    $create = GUICtrlCreateButton("Add Server", 160, 72, 65, 25)
    $server = GUICtrlCreateCombo("Select Server", 8, 8, 249, 25, $CBS_DROPDOWN) ;Create the combo box
    For $i = 1 To UBound($list) - 1                                             ;Populate combo box with first value (name) from array
        If $list[$i][0] <> "" Then GUICtrlSetData($server, $list[$i][0])        ;Ensure array is not empty and fill combox with KEYS
    Next
    $servername = GUICtrlCreateInput("Server Name", 8, 40, 121, 21)
    GUICtrlSetState(-1, $GUI_HIDE)
    $serverip = GUICtrlCreateInput("IP Address", 136, 40, 121, 21)
    GUICtrlSetState(-1, $GUI_HIDE)

    GUISetState(@SW_SHOW)

    While 1                                                                     ;Enter loop until user closes or presses button
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE                                               ;Exit when closed
                Exit
            Case $start                                                         ;Store selection into variable, delete the GUI, and run the start function
                $selection = GUICtrlRead($server)
                If $selection <> "Select Server" Then
                    GUIDelete()
                    start()
                EndIf
            Case $create
                If GUICtrlRead($create) = "Add Server" Then
                    GUICtrlSetState($servername, $GUI_SHOW)
                    GUICtrlSetState($serverip, $GUI_SHOW)
                    GUICtrlSetData($create, "Create")
                Else
                    If (GUICtrlRead($servername) <> "" And GUICtrlRead($servername) <> "Server Name" And GUICtrlRead($serverip) <> "" And GUICtrlRead($serverip) <> "IP Address") Then
                        IniWrite(@TempDir & "\" & $Title & "\config.ini", "Server", GUICtrlRead($servername), GUICtrlRead($serverip))
                        GUICtrlSetState($servername, $GUI_HIDE)
                        GUICtrlSetState($serverip, $GUI_HIDE)
                        GUICtrlSetData($create, "Add Server")
                    Else
                        MsgBox($MB_ICONINFORMATION, $Title, "Invalid Server Name and IP Address.")
                    EndIf
                    For $i = UBound($list) - 1 To 0 Step -1
                        _GUICtrlComboBox_DeleteString($server, $i)
                    Next
                    Local $list = IniReadSection(@TempDir & "\" & $Title & "\config.ini","Server")
                    For $i = 1 To UBound($list) - 1
                        If $list[$i][0] <> "" Then GUICtrlSetData($server, $list[$i][0])
                    Next
                EndIf
        EndSwitch
    WEnd
Else                                                                            ;otherwise, message
    MsgBox($MB_SYSTEMMODAL, "", "Milestone XProtect wasn't found on this computer" & @CRLF)
EndIf

Func start()
    $iPID = Run($milestone)                                                     ;Runs the software and returns the Process ID
    BlockInput(1)
    Sleep(1000)                                                                 ;sleep for 1 second
    BlockInput(0)
    If @error Then MsgBox(0, $Title, "The program could not launch.") Exit      ;If Run returns a 0 or error, there was a problem
    $handle = WinGetHandle("[ACTIVE]")                                          ;Get the handle of the active window, should be Milestone software as it was just ran 1 second ago.
    If $handle = 0 Then MsgBox(0, $Title, "The handle could not be found.") Exit;If the Handle returned is 0, there was no match
    Sleep(4000)
    WinWaitActive($handle)                                                      ;Wait for the program to be activated
    Send("{TAB}")                                                               ;send keystrokes to active window
    Send("{TAB}")
    For $i = 0 to UBound($list) - 1                                             ;Find the IP address that matches the selection and send it
        If $list[$i][0] = $selection Then Send($list[$i][1])
    Next
    Send("{ENTER}")
    Exit                                                                        ;Exit for now until you can capture a succesful run
EndFunc

Func help()
    ShellExecute(@TempDir & "\" & $Title & "\config.ini")
EndFunc
#EndRegion--------------------------------------------------------------------------------------------------------------------------------------------------------------------

added/modified行如下,顺序为:

#include <AutoItConstants.au3>
$iPID = Run($milestone) ;Removed the #shownoactivate parameter, we want it to be the active window. Very important.
BlockInput(1) ;suggestion from comment, disables all input briefly
Sleep(1000) ;wait 1 second for program to launch
BlockInput(0) ;enables all user input again
If @error Then MsgBox(0, $Title, "The program could not launch.") Exit      
$handle = WinGetHandle("[ACTIVE]") ;Get the handle of the active window, should be Milestone software as it was just ran 1 second ago.
If $handle = 0 Then MsgBox(0, $Title, "The handle could not be found.") Exit
Sleep(4000)

我用我这边的软件测试了这个并且它有效。