AutoIT:界面与自动化操作结合来简化日常劳动: .Net Reactor验证License,设置License,创建License,截图AutoIt自动化实现。(二)

为了提高以前的代码的工作效率,以及页面的美观程度,对代码进行优化,优化后的代码如下:

#include <ScreenCapture.au3>
#include <GuiConstantsEx.au3>
Opt("WinTitleMatchMode")

;区域截图
Func screen_capture($path)
    if FileExists($path)= 0 or StringInStr($path,"")=0 Then
        MsgBox(0, "Path Error", "Please input correct Path info!")
    Else
        $handle = WinGetHandle(".NET Reactor v4.5.9.8 - Full Version")
        ConsoleWrite("Screen capture win handle: " & $handle & @CRLF)
        WinActivate($handle)
        $control = ControlGetHandle($handle,"","[Name:groupBox6]")
        ConsoleWrite("Screen capture Control handle: " & $control & @CRLF)
        ;_ScreenCapture_Capture("D:abc.jpg", 350,290,900,420)
        _ScreenCapture_CaptureWnd($path & "AutoIt.jpg", $control)
    EndIf
EndFunc

;设置 Hardware ID
Func set_hardwareID($hardware_id, $date)
    if StringLen($hardware_id) <> 24 or StringInStr($date,"201")=0 or StringLen($date)<8 Then
        MsgBox(0, "Create license error", "Please input correct License create info!")
    Else
        $handle = WinGetHandle(".NET Reactor v4.5.9.8 - Full Version")
        ConsoleWrite("Set hardware ID Window handle: " & $handle & @CRLF)
        WinActivate($handle)
        ControlFocus($handle,"","[Name:button7]")
        SendKeepActive($handle)
        ControlSend($handle,"","", "{tab}")
        $text = ControlGetText($handle,"","[class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1]")
        While $text <> "False"
            ConsoleWrite($text & @CRLF)
            ControlSend($handle,"","", "{UP}")
            $text = ControlGetText($handle,"","[class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1]")
        WEnd
        ControlSend($handle,"","", "{Down}{Down}{Enter}{Down}{Down}")
        ControlSetText($handle,"", "[Class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1; Text:False]",$date)
        ControlSend($handle,"","", "{up}{up}{enter}{down}{enter}{down}{down}")
        ControlSetText($handle,"", "[Class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1; Text:False]",$hardware_id)
        ControlSend($handle,"","", "{up}{up}{enter}")
    EndIf
EndFunc

;保存 License
Func save_license($path)
    if FileExists($path)= 0 or StringInStr($path,"")=0 Then
        MsgBox(0, "Path Error", "Please input correct Path info!")
    Else
        $handle = WinGetHandle(".NET Reactor v4.5.9.8 - Full Version")
        ConsoleWrite("Save License win handle: " & $handle & @CRLF)
        WinActivate($handle)

        $control_handle = ControlGetHandle($handle,"","[Name:button1]")
        ConsoleWrite("Save License Create License button: " & $handle & @CRLF)
        ControlClick($handle,"",$control_handle)

        WinWaitActive("Save License As..")
        $save_window_handle = WinGetHandle("Save License As..")
        ConsoleWrite("Save License popup window handle: " & $save_window_handle & @CRLF)

        $save_input_control = ControlGetHandle($save_window_handle,"","Edit1")
        ConsoleWrite("Save License input field handle: " & $save_input_control & @CRLF)

        ControlSetText($save_window_handle,"",$save_input_control,$path & "SageTouch.License")
        $save_button_handle = ControlGetHandle($save_window_handle,"","Button1")
        ConsoleWrite("Save License Save button: " & $save_button_handle & @CRLF)

        ControlClick($save_window_handle,"",$save_button_handle)
        if WinWait("确认另存为","",1)<>0 Then
            $popup_handle = WinGetHandle("确认另存为")
            WinWaitActive($popup_handle)
            ControlClick($popup_handle,"", "Button1")
        EndIf
    EndIf
    ;获取窗口句柄
EndFunc

;检查 License 是否是正确的
Func exam_license($path)
    If FileExists($path & "SageTouch.License")=0 or StringInStr($path,"")=0 Then
        MsgBox(0, "Exam license","Can not find license to exam")
    Else
        $handle = WinGetHandle(".NET Reactor v4.5.9.8 - Full Version")
        WinActivate($handle)
        $control_handle = ControlGetHandle($handle,"","[Name:menuStrip1]")
        ConsoleWrite("Exam License control handle: " & $control_handle & @LF)

        ControlClick($handle,"",$control_handle,"left", 1, 130, 20)
        $exam_window_handle = WinGetHandle("[class:WindowsForms10.Window.20808.app.0.1f550a4_r15_ad1]")
        ConsoleWrite("Exam License Popup window handle: " & $control_handle & @LF)
        ControlClick($exam_window_handle,"","","left",1,100,40)

        WinWaitActive("License Examiner")
        $license_exam_handle = WinGetHandle("License Examiner")
        ConsoleWrite("License examiner window handle: " & $license_exam_handle & @CRLF)

        $open_license_handle = ControlGetHandle($license_exam_handle,"","[Name:button2]")
        ConsoleWrite("Open License button handle: "&$open_license_handle & @CRLF)
        ControlClick($license_exam_handle,"",$open_license_handle)

        WinWaitActive("Please Select License File")
        $select_license_handle = WinGetHandle("Please Select License File")
        ConsoleWrite("Select License File window handle: " & $select_license_handle & @CRLF)

        $select_license_input_handle = ControlGetHandle($select_license_handle,"","Edit1")
        $open_button_handle = ControlGetHandle($select_license_handle,"","Button1")
        ControlSetText($select_license_handle,"",$select_license_input_handle,$path & "SageTouch.License")
        ControlClick($select_license_handle,"",$open_button_handle)
    EndIf
EndFunc

;Create a new GUI
GUICreate("Create License", 700, 300)

GUICtrlCreateLabel("Hardware ID", 30, 30, 250, 20)
$hardware_id = GUICtrlCreateInput("33DD-8F20-FA26-7E2A-CDE6", 30, 50, 250, 20)

GUICtrlCreateLabel("Expire Date", 300, 30, 200, 20)
$date = GUICtrlCreateInput("2013-10-1", 300, 50, 200, 20)


GUICtrlCreateLabel("License File Path", 30, 100, 120, 20)
$path = GUICtrlCreateInput("", 30, 120, 470, 20)

$exam_License = GUICtrlCreateButton("Exam License", 30, 170, 120, 20)
$Create_License = GUICtrlCreateButton("Create License", 180, 170, 120, 20)
$Save_License = GUICtrlCreateButton("Save License", 330, 170, 120, 20)
$Screen_Capture = GUICtrlCreateButton("Screen Capture", 480, 170, 120, 20)

GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $Create_License
            set_hardwareID(GUICtrlRead($hardware_id), GUICtrlRead($date))
        Case $msg = $Screen_Capture
            screen_capture(GUICtrlRead($path))
        Case $msg = $Save_License
            save_license(GUICtrlRead($path))
        Case $msg = $exam_License
            exam_license(GUICtrlRead($path))
    EndSelect
Wend
复制代码

原文地址:https://www.cnblogs.com/autotest/p/3406356.html