Autoit 5643

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

AutoIt

Jellydn Lp 06T1 Khoa CNTT ai hc Bch Khoa Nng

Ni dung
n n n n n n

Gii thiu v AutoIt Cc v d m u C php Mt s th thut GUI v x l s kin Li kt

Gii thiu v AutoIt


n

AutoIt
n n

L mt ngn ng script Gip NSD gi lp t ng:


n n n

n n n n

n n

Tips: nn g v thc thi li cc v d khi hc !!! Ti ti v ci t AutoIt ti a ch http://www.autoitscript.com/autoit3/

C cu trc ta BASIC n gin , d hc Nh gn Min ph

Phm Kch chut Tng tc vi chng trnh,file khc ca my tnh.

Cc v d m u
n

HelloWorld
n

Chut phi ngoi desktop -> new -> AutoIt v3 Script

Cc v d m u
n

HelloWorld
n

t tn l helloworld.au3

Cc v d m u
n

HelloWorld
n

Chut phi ln file va to -> Edit Script

Cc v d m u
n

HelloWorld
n

SciTE s c m

Cc v d m u
n

HelloWorld
n

Bn thm vo cui file helloworld.au3


n

MsgBox(0, "Tutorial", "Hello World!")

n n n n

Save Kim tra c php :Tool->SyntaxCheck Prod Nhn F5 (Tool -> Go) Kt qu

Cc v d m u
n

Tng tc vi notepad
n n n

To file note.au3 nh v d trn Vo Start -> Run : g notepad M cng c : AutoIt Window Info
n

Vo Start->Programs->AutoIt v3->AutoIt Window Info Dng ly thng tin ca mt ca s ng dng

Cc v d m u
n

Tng tc vi notepad
n

Kch chut vo ca s notepad s c thng tin v title

Cc v d m u
n

Tng tc vi notepad
n

Thm vo cui file note.au3


n

Run("notepad.exe")
n

; lnh run s gi file notepad.exe ; i ti khi ca s notepad kch hot(active) ; title : Untitled - Notepad ; gi dng text ti notepad

WinWaitActive("Untitled - Notepad")
n n

Send("This is some text.")


n

Cc v d m u
n

Tng tc vi notepad
n

Chy th file ( Tool->Go hoc F5)


n

Kt qu

Cc v d m u
n

Tng tc vi notepad
n

Tip tc thm vo cui file note.au3


n

WinClose("Untitled - Notepad")
n

;tt notepad vi title:Untitled Notepad

Cc v d m u
n

Tng tc vi notepad
n

Khi tt s gp thng bo -> ly title

Cc v d m u
n

Tng tc vi notepad
n

Tip tc thm vo cui file note.au3


n

WinWaitActive("Notepad", "Do you want to save")


n

;i thng bo tt xut hin ; !n=Alt+N -> phm tt ca No trong la chn thot

Send("!n")
n

Save v thc thi (F5)

C php
n

Kiu d liu
n

AutoIt ch 1 kiu d liu gi l Variant


n n n

N c th cha s hay chui k t Ty thuc vo lc x l Gm nhng loi d liu sau:


n n n n n n

Int32 Int64 Double String Binary Pointer

C php
n

Bin
n
n n n

nh danh:

Bt u vi $ Gm ch , s v _ V d : $myvar , $my_var Dim


n

Khai bo:
n n n

Local Global

V d : Dim $var1

C th khng khai bo m s dng trc tip


n

V d: $var1 = "create and assign"

C php
n

Bin
n

Hng
n

Dng t kha const


n

Const $const1 = 1, $const2=12

Phm vi hot ng
n

Global
n n

S dng ton chng trnh T lc khai bo Ch trong hm Hy khi hm kt thc

Local
n n

C php
n

Bin
n

Mng
n

Truy cp phn t qua ch s


n

$Array[0]="A $Array[0]=1 $Array[1]=true $Array[2]="Text" $Array[3]=$AnotherArray iu ny khng b cm nhng khng nn dng

Trong 1 mng c th cha nhiu loi d liu


n

C php
n

Macros
n n n n

L nhng bin ch c t bi AutoIt Tn ca macro bt u vi @ Khng th gn gi tr cho macro

C php
n

Ton t
n
n n n

S hc

=,+=,-=,*=,/=,&=,+,-,*,/,&,^
& : ni 2 chui ^ : m

Logic
n

AND , OR , NOT =,==,<,<=,>,>=,<>


n n n

So snh
n

= : so snh 2 s == : so snh 2 chui <> : khc

C php
n

Cu iu kin
n

If...Then...Else
n

$var = 20 If $var > 10 Then MsgBox(0, "Example", "$var was greater than 10!") Else MsgBox(0, "Example", "$var was less than 10") EndIf

C php
n

Cu iu kin

Select...Case
n

$var = 30 Select Case $var > 1 AND $var <= 10 MsgBox(0, "Example", "$var was greater than 1") Case $var > 10 AND $var <= 20 MsgBox(0, "Example", "$var was greater than 10") Case $var > 20 AND $var <= 30 MsgBox(0, "Example", "$var was greater than 20") Case $var > 30 AND $var <= 40 MsgBox(0, "Example", "$var was greater than 30") Case $var > 40 MsgBox(0, "Example", "$var was greater than 40") EndSelect

C php
n

Cu iu kin

Switch...Case
n

$var = 30 Switch Int($var) Case 1 To 10 MsgBox(0, "Example", "$var was greater than 1") Case 11 To 20 MsgBox(0, "Example", "$var was greater than 10") Case 21 To 30 MsgBox(0, "Example", "$var was greater than 20") Case 31 To 40 MsgBox(0, "Example", "$var was greater than 30") Case Else MsgBox(0, "Example", "$var was greater than 40 or less or equal to 0") EndSwitch

C php
n

Vng lp cho php chy trong n vng lp for khc

Vng lp
n

For...To...Step...Next
n

For <variable> = <start> To <stop> [Step <stepval>] statements ... Next V d:


n

For $i = 5 to 1 Step -1 MsgBox(0, "Count down!", $i) Next MsgBox(0,"", "Blast Off!")

C php
n

Vng lp
n

Vng lp cho php chy trong n vng lp While khc

While...WEnd
n

While <expression> statements ... WEnd V d:


n

$i = 0 While $i <= 10 MsgBox(0, "Value of $i is:", $i) $i = $i + 1 WEnd !")

C php
n

Vng lp cho php chy trong n vng lp Do Until khc

Vng lp
n

Do...Until
n

Do statements ... Until <expression> V d:


n

$i = 0 Do MsgBox(0, "Value of $i is:", $i) $i = $i + 1 Until $i = 10

C php
n

Vng lp khng cho php chy trong n vng lp With khc

Vng lp n With...EndWith
n

With <expression> statements ... EndWith


V d:
n

$oExcel = ObjCreate("Excel.Application") $oExcel.visible =1 $oExcel.workbooks.add With $oExcel.activesheet .cells(2,2).value = 1 .range("A1:B2").clear EndWith $oExcel.quit

C php
n

Vng lp cho php chy trong n vng lp For khc

Vng lp
n

For...In...Next
n

For <$Variable> In <expression> statements ... Next Ch lm vic vi mng hay tp i tng

C php
n

Vng lp
n

For...In...Next
n

;Using an Array Dim $aArray[4] $aArray[0]="a" $aArray[1]=0 $aArray[2]=1.3434 $aArray[3]="test" $string = "" FOR $element IN $aArray $string = $string & $element & @CRLF NEXT Msgbox(0,"For..IN Arraytest","Result is: " & @CRLF & $string) ;Using an Object Collection $oShell = ObjCreate("shell.application") $oShellWindows=$oShell.windows if Isobj($oShellWindows) then $string="" for $Window in $oShellWindows $String = $String & $Window.LocationName & @CRLF next msgbox(0,"","You have the following windows open:" & @CRLF & $String) else msgbox(0,"","you have no open shell windows.") endif

C php
n

Hm
n n

nh danh : ging nh bin Func functioname ( [Const] [ByRef] $param1, ..., [Const] [ByRef] $paramN, $optionalpar1 = value, ...) ... [Return [value]] EndFunc V d:
n

$val = 10 For $i = 1 To 10 $doubled = MyDouble($val) MsgBox(0, "", $val & " doubled is " & $doubled) $val = $doubled Next Exit

Func MyDouble($value) $value = $value * 2 Return $value EndFunc

Mt s th thut
n

n icon ca AutoIt di khay h thng


n

Thm #NoTrayIcon vo u file Vista yu cu quyn trc khi thc thi Thm #RequireAdmin vo u file

Thc thi AutoIt trn Vista


n n

GUI v lp trnh s kin


n

GUI(Graphical User Interfaces)


n n
n n n n n n n n n n n

AutoIT cho php bn d dng to cc control Cc control thng dng:


Label : tn nhn Button : nt bm Input : hp text nhp 1 dng Edit : hp text nhiu dng Checkbox,Radio Combo,List Date Pic , Icon Progress : thanh loading Menu,ContextMenu Slider

GUI v lp trnh s kin


n

Cc hm GUI
n n n n n n n

GUICreate() : to ca s lm vic GUICtrlCreate() : to cc control tng ng GUISetState() : hin hay n ca s GUIGetMsg() : ly thng tin v ca s GUICtrlRead() : c d liu t control GUICtrlSetData() : ghi d liu vo control GUICtrlSet(): ghi d liu vo mt s thuc tnh khc ( mu sc , font ch,)

GUI v lp trnh s kin


n

GUI(Graphical User Interfaces)


n

HelloGUI.au3
n

#include <GUIConstantsEx.au3> ;Tao cua so 200x100 voi ten la Hello World GUICreate("Hello World", 200, 100) ;Tao label va button GUICtrlCreateLabel("Hello world! How are you?", 30, 10) GUICtrlCreateButton("OK", 70, 50, 60) ;Hien cua so GUISetState(@SW_SHOW) ;doi 2s Sleep(2000)

GUI v lp trnh s kin


n

Lp trnh s kin
n

AutoIt c 2 ch
n

MessageLoop
n n

L mc nh ca AutoIt Dng hm GUIGetMsg() ly s kin Dng hm Opt("GUIOnEventMode", 1) Dng hm GUICtrlSetOnEvent() to hnh ng cho s kin

OnEvent
n n

GUI v lp trnh s kin


n

Lp trnh s kin
n

MessageLoop
n n n n

GUIGetMsg() s tr v 3 kiu

Khng c s kin : tr v 0 S kin ca control : tr v controlID S kin ca h thng : c khai bo trong GUIConstantsEx.au3 n $GUI_EVENT_CLOSE $GUI_EVENT_MINIMIZE $GUI_EVENT_RESTORE $GUI_EVENT_MAXIMIZE $GUI_EVENT_PRIMARYDOWN $GUI_EVENT_PRIMARYUP $GUI_EVENT_SECONDARYDOWN $GUI_EVENT_SECONDARYUP $GUI_EVENT_MOUSEMOVE $GUI_EVENT_RESIZED $GUI_EVENT_DROPPED

GUI v lp trnh s kin


n

Lp trnh s kin
n

MessageLoop
n n

V d n gin:

#include <GUIConstantsEx.au3> GUICreate("Hello World", 200, 100) GUICtrlCreateLabel("Hello world! How are you?", 30, 10) $okbutton = GUICtrlCreateButton("OK", 70, 50, 60) GUISetState(@SW_SHOW) While 1 $msg = GUIGetMsg() Select Case $msg = $okbutton MsgBox(0, "GUI Event", "You pressed OK!") Case $msg = $GUI_EVENT_CLOSE MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...") ExitLoop EndSelect WEnd

GUI v lp trnh s kin


n

Lp trnh s kin
n

MessageLoop
n

GUIGetMsg(1)
n n n n n n

Tr v mt mng $array[0] = 0 hoc Event ID hoc Control ID $array[1] = ca s ang x l $array[2] = control ang thc thi $array[3] = ta X ca ca s $array[4] = ta Y ca ca s

GUI v lp trnh s kin


n

Lp trnh s kin
n
n

MessageLoop

#include <GUIConstantsEx.au3> $mainwindow = GUICreate("Hello World", 200, 100) GUICtrlCreateLabel("Hello world! How are you?", 30, 10) $okbutton = GUICtrlCreateButton("OK", 70, 50, 60) $dummywindow = GUICreate("Dummy window for testing ", 200, 100) GUISwitch($mainwindow) GUISetState(@SW_SHOW) While 1 $msg = GUIGetMsg(1) Select Case $msg[0] = $okbutton MsgBox(0, "GUI Event", "You pressed OK!") Case $msg[0] = $GUI_EVENT_CLOSE And $msg[1] = $mainwindow MsgBox(0, "GUI Event", "You clicked CLOSE on the main window! Exiting...") ExitLoop EndSelect WEnd

GUI v lp trnh s kin


n

Lp trnh s kin
n

OnEvent
n

Thao tc vi 2 loi s kin


n n

S kin ca control S kin ca h thng GUISetOnEvent() GUICtrlSetOnEvent() Code di v rm r Phi bt option (opt)

Dng 2 hm to hnh ng cho s kin


n n

Khng nn dng cch ny v:


n n

GUI v lp trnh s kin


n

Lp trnh s kin
n

OnEvent
n

Cng chc nng nhng code di hn

#include <GUIConstantsEx.au3> Opt("GUIOnEventMode", 1) ; Change to OnEvent mode $mainwindow = GUICreate("Hello World", 200, 100) GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") GUICtrlCreateLabel("Hello world! How are you?", 30, 10) $okbutton = GUICtrlCreateButton("OK", 70, 50, 60) GUICtrlSetOnEvent($okbutton, "OKButton") GUISetState(@SW_SHOW) While 1 Sleep(1000) ; Idle around WEnd Func OKButton() ;Note: at this point @GUI_CTRLID would equal $okbutton, ;and @GUI_WINHANDLE would equal $mainwindow MsgBox(0, "GUI Event", "You pressed OK!") EndFunc Func CLOSEClicked() ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE, ;and @GUI_WINHANDLE would equal $mainwindow MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...") Exit EndFunc

Li kt
n n n

Ti y l bn vng kin thc v AutoIt Nn autoitscript.com hc thm Hy tm tng v th thc hin n bng AutoIT Ti liu ny c bin son t help ca AutoIt Mi kin gp gi v a ch 06Tonline.net@gmail.com

You might also like