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


    其实,每次只要输入License文件的路径,相应的HID就能够根据路径,或者该路径下的txt文件名称或者txt文档中的第一行字符串自动获取,所以为了进一步简化手工操作,对代码可以进一步优化,只要输入一个包含License文件,hid文件的路径,其余的事情,都可以自动化完成。

    代码如下:

    #include <ScreenCapture.au3>
    #include <GuiConstantsEx.au3>
    #include <date.au3>
    #include <GuiTab.au3>
    #include <GuiEdit.au3>
    
    Opt("WinTitleMatchMode")
    AutoItSetOption("SendKeyDelay")
    AutoItSetOption("WinWaitDelay")
    ;区域截图
    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")
            if $handle="" then return 0
            ConsoleWrite(_Now() & " "& "Screen capture win handle: " & $handle & @CRLF)
            WinActivate($handle)
            $control = ControlGetHandle($handle,"","[Name:groupBox6]")
            ConsoleWrite(_Now() & " "& "Screen capture Control handle: " & $control & @CRLF)
            ;_ScreenCapture_Capture("D:abc.jpg", 350,290,900,420)
            _ScreenCapture_CaptureWnd($path & "AutoIt.jpg", $control)
        EndIf
    EndFunc
    
    ;无论焦点在哪儿,都回归到包含 EvaluationType 行
    Func init($handle)
        $text = ControlGetText($handle,"","[class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1]")
        ConsoleWrite(_Now()& " a "& $text & @CRLF)
        $text2 = $text
        While StringInStr($text, "EvaluationType =") = 0
            ConsoleWrite(_Now()& " "& $text & @CRLF)
            ControlSend($handle,"","", "{UP}")
            $text = ControlGetText($handle,"","[class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1]")
            If $text = $text2 And $text2 = "False" Then
                ControlSend($handle,"","", "{Down}")
                ExitLoop
            EndIf
        WEnd
        ConsoleWrite(_Now()& " b "& $text & @CRLF)
    EndFunc
    
    ;无论选项展开了多少次,都让所有的展开项收缩
    Func collapse($handle)
        $text = ControlGetText($handle,"","[class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1]")
        ControlSend($handle,"","", "{Down}")
        $text2 = ControlGetText($handle,"","[class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1]")
        While $text <> $text2
            If StringInStr($text,$text2) <> 0 Then
                ControlSend($handle,"","", "{up}{enter}{down}")
                $text = ControlGetText($handle,"","[class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1]")
                ControlSend($handle,"","", "{Down}")
                $text2 = ControlGetText($handle,"","[class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1]")
            Else
                $text = $text2
                ControlSend($handle,"","", "{Down}")
                $text2 = ControlGetText($handle,"","[class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1]")
            EndIf
        WEnd
    EndFunc
    
    ;Display the HID in text field automatic
    Func GetHid($filepath)
        Dim $hid
        if FileExists($filepath) Then
            $matches = StringRegExp($filepath, "w{4}-w{4}-w{4}-w{4}-w{4}", 1, 1)
            If IsArray($matches) Then
                $hid = $matches[0]
                Return $hid
            EndIf
    
            FileChangeDir($filepath)
            $search = FileFindFirstFile("*.txt")
            ; 检查搜索是否成功
            If $search = -1 Then
                Return "No HID .txt file found in the path"
            EndIf
            While 1
                $file = FileFindNextFile($search)
                If @error Then
                    ExitLoop
                EndIf
                $matches = StringRegExp($file, "w{4}-w{4}-w{4}-w{4}-w{4}", 3, 1)
                If IsArray($matches) Then
                    $hid = $matches[0]
                    ExitLoop
                EndIf
    
                Local $file2 = FileOpen($file)
                If $file2 = -1 Then
                    ExitLoop
                EndIf
                While 1
                    Local $line = FileReadLine($file2)
                    If @error = -1 Then ExitLoop
                    $matches = StringRegExp($line, "w{4}-w{4}-w{4}-w{4}-w{4}", 3, 1)
                    If IsArray($matches) Then
                        $hid = $matches[0]
                        ExitLoop
                    EndIf
                WEnd
                FileClose($file2)
            WEnd
            ; 关闭搜索句柄
            FileClose($search)
        Else
            Return "File path is not correct"
        EndIf
        Return $hid
    EndFunc
    
    ;设置 Hardware ID
    Func set_hardwareID($hardware_id, $date)
        if StringLen($hardware_id) <> 24 or StringInStr($date,"/")=0 or StringLen($date)<8 Or  StringLen($date)>19 Then
            MsgBox(0, "Create license error", "Please input correct License create info!")
        Else
            $handle = WinGetHandle(".NET Reactor")
            if $handle="" then return 0
            ConsoleWrite(_Now() & " "& "Set hardware ID Window handle: " & $handle & @CRLF)
            WinActivate($handle)
            $tab_control = ControlGetHandle($handle,"", "[name:tabControl2]")
            _GUICtrlTab_ClickTab($tab_control, 2)
            SendKeepActive($handle)
            ControlSend($handle,"","", "{tab 4}")
            $text = ControlGetText($handle,"","[class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1]")
            While StringInStr($text, "RSAKeyValue") <>0
                ControlSend($handle,"","", "{tab 4}")
                $text = ControlGetText($handle,"","[class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1]")
            WEnd
    
            init($handle)
            collapse($handle)
            init($handle)
    
            ;输入日期
            ControlSend($handle,"","", "{down}{enter}{down 2}")
            $text = ControlGetText($handle,"","[class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1]")
            If StringInStr($text,"/") <>0 Then
                ControlSetText($handle,"", "[Class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1; Text:False]",$date)
                ControlSend($handle,"","", "{up 2}{enter}{up}")
            Else
                collapse($handle)
                init($handle)
            EndIf
    
            ;输入Hardware ID
            ControlSend($handle,"","", "{down 2}{enter}{down 2}")
            $text = ControlGetText($handle,"","[class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1]")
            If StringInStr($text,"-") <>0 Then
                ControlSetText($handle,"", "[Class:WindowsForms10.EDIT.app.0.1f550a4_r15_ad1; Text:False]",$hardware_id)
                ControlSend($handle,"","", "{up 2}{enter}{up 2}")
            Else
                collapse($handle)
                init($handle)
            EndIf
        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")
            if $handle="" then return 0
            ConsoleWrite(_Now() & " "& "Save License win handle: " & $handle & @CRLF)
            WinActivate($handle)
    
            $control_handle = ControlGetHandle($handle,"","[Name:button1]")
            ConsoleWrite(_Now() & " "& "Save License Create License button: " & $handle & @CRLF)
            ControlClick($handle,"",$control_handle)
    
            $check = WinWaitActive("Save License As..")
            if $check = 0 Then
                ConsoleWrite(_Now() & " " & "Save License As pop up window failed" & @CRLF)
                Return 0
            EndIf
            $save_window_handle = WinGetHandle("Save License As..")
            ConsoleWrite(_Now() & " "& "Save License popup window handle: " & $save_window_handle & @CRLF)
    
            $save_input_control = ControlGetHandle($save_window_handle,"","Edit1")
            ConsoleWrite(_Now() & " "& "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(_Now() & " "& "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")
            if $handle="" then return 0
            WinActivate($handle)
            SendKeepActive($handle)
            $handle2 = ControlGetHandle($handle, "", "[NAME:menuStrip1]")
            if $handle2 = "" then return 0
            ConsoleWrite(_Now() & " " & "Get menu handle" & $handle2 & @CRLF)
            ControlSend($handle, "","","{ALT}{Tab 2}{Enter}{Down}{Enter}")
            $check = WinWaitActive("License Examiner", "", 5)
            if $check = 0 Then
                ConsoleWrite(_Now() & " " & "License Examiner pop up window failed" & @CRLF)
                Return 0
            EndIf
            $license_exam_handle = WinGetHandle("License Examiner")
            ConsoleWrite(_Now() & " "& "License examiner window handle: " & $license_exam_handle & @CRLF)
    
            $open_license_handle = ControlGetHandle($license_exam_handle,"","[Name:button2]")
            ConsoleWrite(_now() & " Open License button handle: "&$open_license_handle & @CRLF)
            ControlClick($license_exam_handle,"",$open_license_handle)
    
            $check = WinWaitActive("Please Select License File", "", 5)
            if $check = 0 Then
                ConsoleWrite(_Now() & " " & "Select License File pop up window failed" & @CRLF)
                Return 0
            EndIf
    
            $select_license_handle = WinGetHandle("Please Select License File")
            ConsoleWrite(_Now()& " "& "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)
    $tTime = _Date_Time_GetSystemTime()
    $aTime = _Date_Time_SystemTimeToArray($tTime)
    $date = $aTime[2] & "/"  & $aTime[0] & "/" & "1"
    $date2 = _DateAdd("M", 2, $date)
    $date = GUICtrlCreateInput($date2, 300, 50, 200, 20)
    
    
    GUICtrlCreateLabel("License File Path", 30, 100, 120, 20)
    $path = GUICtrlCreateInput("", 30, 120, 570, 20)
    
    $display_HID = GUICtrlCreateButton("Display HID", 30, 170, 120, 20)
    $exam_License = GUICtrlCreateButton("Exam License", 30, 220, 120, 20)
    $Create_License = GUICtrlCreateButton("Create License", 180, 220, 120, 20)
    $Save_License = GUICtrlCreateButton("Save License", 330, 220, 120, 20)
    $Screen_Capture = GUICtrlCreateButton("Screen Capture", 480, 220, 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))
            Case $msg = $display_HID
                $hid = GetHid(GUICtrlRead($path))
                _GUICtrlEdit_SetText($hardware_id, $hid)
        EndSelect
    Wend

    用户只要输入License文件所在的路径,点击“Display HID”,Hid就会呈现在输入框中,用户此时之需要按顺序点击按钮就能快速制作出License。

  • 相关阅读:
    MVC5 Controller构造方法获取User为空解决方法
    js删除Array数组中的某个元素
    VS2015 工具箱 保存位置
    Mapper 赋值对应实体属性
    cookieHelper
    python3练习100题——007
    python3练习100题——006
    python3练习100题——005
    python3练习100题——004
    python3练习100题——003
  • 原文地址:https://www.cnblogs.com/autotest/p/3406384.html
Copyright © 2020-2023  润新知