1. Trang chủ
  2. » Công Nghệ Thông Tin

GIỚI THIỆU VỀ AUTOIT-Lập Trình Trên AutoIT part 32 ppt

6 342 4

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

$i = 0 $run = 0 $nDelay = 50 ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ $gui = GUICreate("A Text Typer", 700, 500) $eMainText = GUICtrlCreateEdit("", 5, 5, 690, 420, $ES_AUTOVSCROLL + $WS_VSCROLL+ $ES_READONLY) GUICtrlSetFont(-1, 10, 400, default, "Tahoma") $text_ShowSource = GUICtrlCreateLabel("Source : <default text>", 8, 435, 690, 20) $text_delay = GUICtrlCreateLabel("Delay (miliSecond) :" , 10, 470, 100, 30) $eDelay = GUICtrlCreateInput($nDelay, 115, 467, 40, 20, $ES_NUMBER) $btnPlayPause = GUICtrlCreateButton("Start", 170, 462, 80, 30) $btnStop = GUICtrlCreateButton("Stop", 250, 462, 80, 30) $btnOpenText = GUICtrlCreateButton("Open text", 350, 462, 80, 30) $btnLoadDefault = GUICtrlCreateButton("Load default text", 430, 462, 110, 30) $text_status = GUICtrlCreateLabel("Status : Stoped ", 580, 470, 150, 30) GUISetState() ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ while 1 $msg = GUIGetMsg() Switch $msg case $GUI_EVENT_CLOSE ExitLoop Case $btnPlayPause If $run=0 Or $run=2 Then; if state is "stoped" or "paused", then $run = 1 ; set state to "start" update_status(1) ; set new text for buttons and status $nDelay = Abs(Int(GUICtrlRead($eDelay))) ; read delay value for speed type $timer2type = _Timer_SetTimer($gui, $nDelay, "TypeText") ; create a new timer (note : this will use current valure of $i and $run) GUICtrlSetState($eDelay, $GUI_DISABLE) ; disable input of delay ElseIf $run=1 Then ; if state is "typing" , then $run = 2 ; change state to "stoped" update_status(2) ; set new texts for buttons GUICtrlSetState($eDelay, $GUI_ENABLE) ; enable delay s input _Timer_KillTimer($gui, $timer2type) ; kill timer to stop typing text (note : current value will be used on next time when i create new timer) EndIf Case $btnStop _Timer_KillTimer($gui, $timer2type) $run = 0 $i = 0 update_status(0) GUICtrlSetState($eDelay, $GUI_ENABLE) Case $btnOpenText _Timer_KillTimer($gui, $timer2type) ; kill timer to stop typing text If BrowseText() Then $i = 0 ; check if a new file is opened successfully , if yes , then reset position which is reading in text $nDelay = Abs(Int(GUICtrlRead($eDelay))) ; update delay value $timer2type = _Timer_SetTimer($gui, $nDelay, "TypeText") ; create a new timer for typing text $run = 1 ; set state this time is RUNNING update_status(1) ; update text on button and status Case $btnLoadDefault _Timer_KillTimer($gui, $timer2type) $i = 0 $run = 0 update_status(0) $string = $Def_str GUICtrlSetData($text_ShowSource, "Source : <Default text>") $nDelay = Abs(Int(GUICtrlRead($eDelay))) $timer2type = _Timer_SetTimer($gui, $nDelay, "TypeText") $run = 1 update_status(1) EndSwitch WEnd ;========================================================== ==== ; this func will set new texts for buttons and status Func update_status($nState) If $nState=0 Then ; neu trang thai la stop GUICtrlSetData($btnPlayPause, "Start") GUICtrlSetData($text_status, "Status : Stoped ") ElseIf $nState=1 Then ; neu trang thai la play GUICtrlSetData($btnPlayPause, "Pause") GUICtrlSetData($text_status, "Status : Typing ") Else ; neu trang thai la pause GUICtrlSetData($btnPlayPause, "Start") GUICtrlSetData($text_status, "Status : Paused ") EndIf EndFunc ; this func will show open dialog for user who choose a text file and if success then continue to type with new texts Func BrowseText() Local $filePath, $file $filePath = FileOpenDialog("Open a text file", @MyDocumentsDir, "Text (*.txt) | All file (*.*)", 1+2) If @error <> 1 Then $file = FileOpen($filePath, 0) If $file =-1 Then MsgBox(16, "Error", " I'm sorry. A error occured when I read file. ") Return 0 Else $string = FileRead($file) FileClose($file) GUICtrlSetData($text_showSource , "Source : " & $filepath) EndIf Else return 0 EndIf Return 1 EndFunc ; it's important , this will type text and show it on GUI Func TypeText($hWnd, $Msg, $iIDTimer, $dwTime) $i += 1 If ($i>StringLen($string)) Then $i=0 $run = 0 update_status(0) GUICtrlSetData($text_status, "Status : Completed") _Timer_KillTimer($gui, $timer2type) Return EndIf $temp_str = StringLeft($string, $i) $condition = StringRight($temp_str, 1) <> @CR And StringRight($temp_str, 1)<>@LF If $condition Then $temp_str &= "_" GUICtrlSetData($eMainText, $temp_str) Else Sleep(400) EndIf EndFunc #include <GUIConstants.au3> #include <WindowsConstants.au3> #include <EditConstants.au3> #include <Timers.au3> Global $str , $count, $temp , $Timer_Types $str = " Hi there , this is a sample of auto-typed text" $count = 0 Opt("GUICloseOnESC",0) ;============== MAKE GUI =========================================== $gui = GUICreate("A type text (basic)", 500, 336, 193, 125) $eMainEdit = GUICtrlCreateEdit("", 8, 11, 481, 273, BitAND($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$ WS_VSCROLL)) $txt_speed = GUICtrlCreateLabel("Speed (miliSecond) : ", 20, 300, 104, 17) $eSpeed = GUICtrlCreateInput("50", 135, 300, 41, 21, BitOR($ES_AUTOHSCROLL,$ES_NUMBER)) $btnStartStop = GUICtrlCreateButton("Start", 200, 295, 108, 30, 0) $btnOpen = GUICtrlCreateButton("Open text", 365, 295, 112, 30, 0) GUISetState(@SW_SHOW) ;========================================================== ==================================== While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $btnStartStop If GUICtrlRead($btnStartStop)="Start" Then GUICtrlSetData($btnStartStop, "Stop") GUICtrlSetState($eSpeed, $GUI_DISABLE) $nDelay = Abs(Int(GUICtrlRead($eSpeed))) $Timer_Type = _Timer_SetTimer($gui , $nDelay, "_Type_Text") Else _Timer_KillAllTimers($gui) GUICtrlSetData($btnStartStop, "Start") GUICtrlSetState($eSpeed, $GUI_ENABLE) $count = 0 EndIf Case $btnOpen _Timer_KillAllTimers($gui) If BrowseFile() Then $count = 0 GUICtrlSetState($eSpeed, $GUI_DISABLE) $nDelay = Abs(Int(GUICtrlRead($eSpeed))) $Timer_Type = _Timer_SetTimer($gui, $nDelay, "_Type_Text") GUICtrlSetData($btnStartStop, "Stop") EndSwitch WEnd ;========================================================== ========== Func BrowseFile() Local $path, $file, $IsError=0 $path = FileOpenDialog("Open text file", @MyDocumentsDir, "Text (*.txt)|All file (*.*)" , 1+2) If @error=1 Then MsgBox(16, "Error", "Sorry. I can't open file. Previous texts will be continued.") $IsError = 1 Else $file = FileOpen($path, 0) If $file=-1 Then MsgBox(16, "Error", "Sorry. I can't read this file") $IsError = 1 EndIf $str = FileRead($file) FileClose($file) EndIf If $IsError=0 Then Return 1 ; open file successfully Else Return 0 ; fail EndIf EndFunc ; important function , this will type text automaticly Func _Type_Text($hwnd, $msg, $wParam, $lParam) $count += 1 If $count>StringLen($str) Then $count = 0 _Timer_KillAllTimers($gui) GUICtrlSetData($btnStartStop, "Start") GUICtrlSetState($eSpeed, $GUI_ENABLE) Return EndIf $temp = StringLeft($str, $count) $temp &= "|" GUICtrlSetData($eMainEdit, $temp) EndFunc

Ngày đăng: 03/07/2014, 00:21

Xem thêm: GIỚI THIỆU VỀ AUTOIT-Lập Trình Trên AutoIT part 32 ppt

TỪ KHÓA LIÊN QUAN