AutoIT tut 27: 1 số ví dụ về chương trình AutoIT

0
0
(0)

A text basic

#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

A text typer Advance

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <Timers.au3>
Global $string , $def_str, $temp_str , $i , $len , $run , $nDelay , $misc , $timer2type
; run = 1 : playing                run=2 : paused                   run=0 : stoped
; i : dem so ky tu can lay
$str1 = "when i saw you at the party , when you asked me girlfriend , i didn't know why i can't say clearly . You 're as pretty as angel , your eyes seemed to burn my heart. My strange lover..." & @CRLF
$str2 = "When i smelt a perfume from your hair , when i kissed your body , the waves of love were flowing in my and your soul . You said , don't leave me baby ..." & @CRLF
$str3 = "When i heard words you told , when i believed your heart , and now i knew i was a fool . I didn't cry but my soul was dying , my heart broke when you said ''Good bye''" & @CRLF
$str4 = "When i looked the waves of ocean , when i missed my memories , i felt all thing were only a deam . A full house which would never exist in this world. The sun was setting..." & @CRLF
$str5 = "When i told my children stories , when i took my wife's hands , i knew the happy life was right here . '' Every thing is never perfect , i have to accept . '' " & @CRLF
$str6 = "When i see the winter of my life , when i write these lines , all left ever and ever, all will never return . Outside the window , sights are a white by snow , the wind 's still blowing , it's still cold, very cold. "
$str7 = "Maybe this's final winter i can look , think and count leaves ..."
$def_str = $str1 & $str2 & $str3 & $str4 & $str5 & $str6 & $str7
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$string = $def_str
$len = StringLen($string)
$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

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave A Reply

Your email address will not be published.