AutoIt Tutorial [GUI, Switch, variables]

Discussion in 'Programming & Scripting' started by Zisly, Sep 14, 2008.

  1. Zisly

    Zisly Well-Known Member

    Joined:
    Jun 9, 2008
    Messages:
    7,349
    As people wanted me to write more tutorials I will do so :p
    But before we start you should get Koda, if you don't already got it that is.
    You can get it from HERE

    So this time we will go thought: gui creation,#include and some other things :)

    So now we will start with the #include.
    #include simply means that you will include a prewriten library of functions.

    Now lets create your first gui.
    open FD.exe which is located in AutoIt3\SciTE\Koda

    When the program first pops up you will see grey window in the middle of the screen, that is the gui.
    You can drag it and so on to get the desired height and width.
    Above that window you can see some gui controls, ex: label,input, check boxes.
    So take 1 input box and place it where you want on the gui, and take 1 button and also place it on the gui :)

    Now click: Tools and then run form, now the code will be generated and appear in Scite.
    I will put out some comments in the code to help you.
    Remember! comments start with ';'
    My code:

    Code:
    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    
    #Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Form1", 269, 99,193,115)
    $Input1 =GUICtrlCreateInput("Input1",72,16,121,21)
    $Button1 = GUICtrlCreateButton("Button1",96,56,75,25,0)
    GUISetState(@SW_SHOW) ;; set the gui state to show so you can see it 
    #EndRegion ### END Koda GUI section ###
    
    While1 ;;; start of loop[/COLOR]
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE ;;; if the window is close, ex: the uses clicked X the script will do the action underneath
    Exit ;;; Will exit the script 
    EndSwitch
    WEnd ;;; end of loop
    
    Ok, as you can see there are now 4 included library's which are needed to this to work.
    Now I will explain:
    Code:
    $Form1 = GUICreate("Form1", 269, 99,193,115)
    
    As I could not think of a good way to explain variables I toke the explanation from the help file :(

    "A variable is just a place to store data in memory so that it can be accessed quickly.
    Think of it as a mailbox in memory that you can put information in or take information out of.
    For example you might create a variable to store the number a user's response to a question,
    or the result to a math equation."

    Now I will explain in my way again.
    A variable must always start with a '$' and it cannot contain spaces, but you can use _ , ex; $my_name_is
    So what we did here was to assign $Forml as the gui window.
    Which was
    Code:
    ]GUICreate("Form1", 269, 99,193, 115)
    Ok, I will "explain" the litle code above.
    [GUICreate("TITLE", WIDTH,HEIGHT,LEFT,TOP)
    Where it now says title is where you put the windows title name, followed by width and height.
    Left and top is where the window will appear when created.

    Ok, that was the gui window now we will go thought the rest of the gui controls.
    GUICtrlCreateInput("TEXT", 72,16,121,21)
    This is almost the same as the gui explanation, so I will just explain the difference.
    Where it now says "TEXT", that's what text should be shown, as in this case we don't want any text from the beginning
    we will just delete it, then it will look like this:
    Code:
    GUICtrlCreateInput("",72,16,121,21)
    
    Now lets move on to the button.
    GUICtrlCreateButton("Button1", 96,56,75,25,0)
    It's almost like the input control, except the last part ( the 0 ) but as I wont explain Styles we will jump over this.

    Code:
    GUISetState(@SW_SHOW) ;;; set the gui state to show so you can see it 
    
    That sets the gui to shown, else we won't be able to see the gui.
    Ok, that was the gui window and control, now let's move on to the rest.
    Code:
    While 1 ;;; start of loop
    $nMsg = GUIGetMsg()
    Switch $nMsg
    	Case $GUI_EVENT_CLOSE ;;; if the window is close, ex: the uses clicked X the script will do the action underneath[/COLOR]
    			Exit ;;;; Will exit the script 
    	EndSwitch
    WEnd ;;; end of loop
    
    First line is "While 1" which I wont explain.
    Now line 2,$nMsg is assigned to GUIGetMsg(), which will, hum.. let's say get event (ex: button was pressed).
    The Switch will switch $nMsg] and wait for a GUI related expressions.

    Now I will explain "Case" which is very simple.
    If the event followed by case is true then statement underneath case will be executed , in this case we have
    "$GUI_EVENT_CLOSE" which means that the user pressed X (window was closed.)
    And underneath we got "Exit" which will exit the script.

    Now the last lines.
    "EndSwitch" which will end the swich, and "Wend" which will end the loop."

    Ok, so I hope you understood what this tutorial, if you didn't understand something just ask :)

    I will make a new tutorial where we will use this GUI later.


    If you think it's use full give reputation please! :)
    Last edited: Oct 21, 2009
  2. JayBeee

    JayBeee Retired Global Mod

    Joined:
    Jun 2, 2007
    Messages:
    5,232
    Alright, yet again, good work explaining. I think I'll check this AutoIt out a little more. Thanks for giving me the heads-up! :thumbup:
  3. Zisly

    Zisly Well-Known Member

    Joined:
    Jun 9, 2008
    Messages:
    7,349
    Np nice that someone liked it :D
  4. Wezza

    Wezza lua nOOb

    Joined:
    May 31, 2008
    Messages:
    2,284
    Thanks, it helped me alot :eek: Thanks for that program too!
  5. Wezza

    Wezza lua nOOb

    Joined:
    May 31, 2008
    Messages:
    2,284
    Hmm, I got that problem ": The Installer requires the AutoIt3 profuction, realease to be Installed first.Stopping Installation.
  6. Zisly

    Zisly Well-Known Member

    Joined:
    Jun 9, 2008
    Messages:
    7,349
    Bump my tutorial. :)
  7. Launian

    Launian Tsuki~

    Joined:
    Jul 7, 2008
    Messages:
    34
    Gah, nevermind. Just found the download on the AutoIt webpage =x

    Trying the tutorial now.

    About the code you gave us (the first one): take out the [/COLOR] label xD I was trying to run it, and it was throwing an error.
    Last edited: Oct 21, 2009
  8. Pro Warbringer

    Pro Warbringer Retired with developing

    Joined:
    Sep 23, 2008
    Messages:
    3,581
    Could you do some pictures? (PLX)
  9. Zisly

    Zisly Well-Known Member

    Joined:
    Jun 9, 2008
    Messages:
    7,349
    '??????????????:huh:
  10. Pro Warbringer

    Pro Warbringer Retired with developing

    Joined:
    Sep 23, 2008
    Messages:
    3,581
    Do some pictures in the tutorial for new guy's
  11. Zisly

    Zisly Well-Known Member

    Joined:
    Jun 9, 2008
    Messages:
    7,349
    I wrote this some time ago and I'm not a good writer but if people can't understand this they shouldn't bother starting IMO.

Share This Page