Posted by Catur Nugroho | File under :

Simple Notepad Automation Using Autoit C#

This is a simple example of how to work with Notepad for sending text, copy / paste, and save it as a file. I will try to explain as details so that you can easily understand.
You can adding this code into your project. This is additional function that I have made, you can copy and paste.

private void Type(string strKeys, int nMode, string strControl, bool blnSleepKeys, int nSleep, string strWindow)
        {
            if (blnSleepKeys)
            {
                char[] achKeys = strKeys.ToCharArray();

                foreach (char ch in achKeys)
                {
                    autoit.ControlSend(strWindow, "", strControl, ch.ToString(), nMode);
                    Thread.Sleep(100);
                    Trace.WriteLine("Sent Key: '" + ch.ToString() + "'");
                }
            }
            else
            {
                autoit.ControlSend(strWindow, "", strControl, strKeys, nMode);
                Trace.WriteLine("Sent Keys: " + strKeys);
            }

            for (int i = 0; i < (nSleep / 100); i++)
            {
                Application.DoEvents();
                Thread.Sleep(100);
            }
        }

        private void Click(string strButton, int nNumClicks, int nX, int nY, string strControl, int nSleep, string strWindow)
        {
            autoit.ControlClick(strWindow, "", strControl, strButton, nNumClicks, nX, nY);

            for (int i = 0; i < (nSleep / 100); i++)
            {
                Application.DoEvents();
                Thread.Sleep(100);
            }
        }


The first step that must be done is to determine the required parameters like as title, text, control, etc..
You can see the images below.



Parameters :
Title : "[CLASS:Notepad]" or "Untitled - Notepad"
Text : ""
Control : Edit1
nX : 78
nY : 53
private void runButton_Click(object sender, EventArgs e)
        {
            //check if notepad is already exist
            if (autoit.WinExists("[CLASS:Notepad]", "") == 1)
            {
                //notepad closed
                autoit.WinKill("[CLASS:Notepad]", "");
            }
            //execute notepad.exe
            autoit.Run("Notepad.exe", "", 1);
            //wait for max 25 seconds
            autoit.WinWaitActive("[CLASS:Notepad]", "", 25);
            //check if notepad doesn't exist
            if (autoit.WinExists("[CLASS:Notepad]", "") == 0)
            {
                MessageBox.Show("Notepad didn't opened");
            }
            else
            {
                //activate the notepad
                //or set notepad into front

                autoit.WinActivate("[CLASS:Notepad]", "");
            }
            //send some text into text area
            string strText = "autoitsourcecode.blogspot.com";
            Type(strText, 0, "Edit1", true, 500, "[CLASS:Notepad]");
            //try to copy / paste

            //send CTRL+ a
            Type("^a", 0, "Edit1", true, 1000, "[CLASS:Notepad]");

            //send CTRL + c (Copy)
            Type("^c", 0, "Edit1", true, 1000, "[CLASS:Notepad]");

            //send CTRL + v (paste)
            Type("^v", 0, "Edit1", true, 1000, "[CLASS:Notepad]");

            //send CTRL + v (paste)
            Type("^v", 0, "Edit1", true, 1000, "[CLASS:Notepad]");

            //send CTRL + v (paste)
            Type("^v", 0, "Edit1", true, 1000, "[CLASS:Notepad]");
            //send CTRL + v (paste)
             Type("^v", 0, "Edit1", true, 1000, "[CLASS:Notepad]");
            //save the file
            //send key alt + f + a

            Type("!fa", 0, "Edit1", true, 500, "[CLASS:Notepad]");
            //wait the "save as" win dialog is opened until 25 seconds
            autoit.WinWaitActive("Save As", "", 25);
            //check if didn't open
            if (autoit.WinExists("Save As", "") == 0)
            {
                MessageBox.Show("The can't saved");
            }


This is images Save As window info



Parameters :
Title : "Save As"
Text : ""
Control : Edit1
nX : 109
nY : 8
            //send key directory folder
            Type(@"C:\autoitDemo.txt", 0, "Edit1", true, 1000, "Save As");
            //check the files
            if (File.Exists(@"C:\autoitDemo.txt"))
                File.Delete(@"C:\autoitDemo.txt");
The image for click Save button


Parameters :
Title : "Save As"
Text : ""
Control : Edit1
nX : 46
nY :13

            //click button save
            Click("left", 1, 42, 15, "Button1", 1000, "Save As");
            //check if notepad is already exist
            if (autoit.WinExists("[CLASS:Notepad]", "") == 1)
            {
                //notepad closed
                autoit.WinKill("[CLASS:Notepad]", "");
            }
        }

Download Full Source Code
Posted by Catur Nugroho | File under :

Autoit Win Active Using C#

Checks to see if a specified window exists and is currently active.
WinActive ( "title" [, "text"] )

Parameters

title The title of the window to check. See Title special definition.
text [optional] The text of the window to check.

Return Value

Success: Returns the handle to the window if it is active.
Failure: Returns 0 otherwise.

Remarks

None.

Source : Autoit Help Document

Autoit Win Active In C#



Parameters Required :
Title : [CLASS:Notepad]
Text : ""
Time Out :

 private void winWaitbutton_Click(object sender, EventArgs e)
        {           
            //open notepad
            autoit.Run("Notepad.exe", "", 1);

            //Wait a maximum of 20 seconds for "[CLASS:Notepad]" to exist
            autoit.WinWait("[CLASS:Notepad]", "", 20);

            if (autoit.WinActive("[CLASS:Notepad]", "") == 0)
            {
                MessageBox.Show("Notepad didn't active");
            }
        }


Download Full Source Code
Posted by Catur Nugroho | File under :

Autoit Win Wait Active Using C#

Pauses execution of the script until the requested window is active.
WinWaitActive ( "title" [, "text" [, timeout]] )

Parameters

title The title of the window to check. See Title special definition.
text [optional] The text of the window to check.
timeout [optional] Timeout in seconds

Return Value

Success: Returns handle to the requested window.
Failure: Returns 0 if timeout occurred.

Remarks

None.

Source : Autoit Help Document

Source Code Autoit Win Wait In C#

 


Parameters Required :
Title : [CLASS:Notepad]
Text : ""
Time Out :

   private void winWaitButton_Click(object sender, EventArgs e)
        {
            DateTime dtStart = DateTime.Now;

            //open notepad
            autoit.Run("Notepad.exe", "", 1);

            //Wait a maximum of 20 seconds for "[CLASS:Notepad]" to exist and be active
            autoit.WinWaitActive("[CLASS:Notepad]", "", 20);

            label1.Text = "waiting for " + (DateTime.Now - dtStart).Seconds.ToString() + " seconds from 10 seconds";
        }


Download Full Source Code (look into source code if the notepad doesn't exist)
Posted by Catur Nugroho | File under :

Autoit Win Wait Using C#

WinWait

Pauses execution of the script until the requested window exists.
WinWait ( "title" [, "text" [, timeout]] )

Parameters
title The title of the window to check. See Title special definition.
text [optional] The text of the window to check.
timeout [optional] Timeout in seconds

Return Value
Success: Returns handle to the requested window.
Failure: Returns 0 if timeout occurred.

Remarks
None.

Source : Autoit Help Document

Source Code Autoit Win Wait In C#


Parameters Required :
Title : [CLASS:Notepad]
Text : ""
Time out :

        private void winWaitButton_Click(object sender, EventArgs e)
        {
            DateTime dtStart = DateTime.Now;

            //open notepad
            autoit.Run("Notepad.exe", "", 1);

            //Wait a maximum of 20 seconds for "[CLASS:Notepad]" to exist
            autoit.WinWait("[CLASS:Notepad]", "", 20);

            label1.Text = "waiting for " + (DateTime.Now - dtStart).Seconds.ToString() + " seconds";
        }


Download Full Source Code (look into source code if the notepad doesn't exist)
Posted by Catur Nugroho | File under :

Autoit Control List View Using C#


Sends a command to a TreeView32 control.
ControlTreeView ( "title", "text", controlID, "command" [, option1 [, option2]] )

Parameters

title The title of the window to access.
text The text of the window to access.
controlID The control to interact with. See Controls.
command The command to send to the control (see below).
option1 [optional] Additional parameter required by some commands.
option2 [optional] Additional parameter required by some commands.

Return Value

Depends on command as table below shows. In case of an error (such as an invalid command or window/control could not be found) then @error is set to 1.


Command, Option1, Option2 Operation
"Check", "item" Checks an item (if the item supports it).
"Collapse", "item" Collapses an item to hide its children.
"Exists", "item" Returns 1 if an item exists, otherwise 0.
"Expand", "item" Expands an item to show its children.
"GetItemCount", "item" Returns the number of children for a selected item.
"GetSelected" [, UseIndex] Returns the item reference of the current selection using the text reference of the item (or index reference if UseIndex is set to 1).
"GetText", "item" Returns the text of an item.
"IsChecked" Returns the state of an item. 1:checked, 0:unchecked, -1:not a checkbox.
"Select", "item" Selects an item.
"Uncheck", "item" Unchecks an item (if the item supports it).


The "item" parameter is a string-based parameter that is used to reference a particular treeview item using a combination of text and indices. Indices are 0-based. For example:

Heading1
----> H1SubItem1
----> H1SubItem2
----> H1SubItem3
----> ----> H1S1SubItem1
Heading2
Heading3

Each "level" is separated by |. An index is preceded with #. Examples:


Item Item Reference
Heading2 "Heading2" or "#1"
H1SubItem2 "Heading1|H1SubItem2" or "#0|#1"
H1S1SubItem1 "Heading1|H1SubItem3|H1S1SubItem1" or "#0|#2|#0"


References can also be mixed like "Heading1|#1".

Remarks

As AutoIt is a 32-bit application some commands are not available when referencing a 64-bit application as Explorer when running on 64-bit Windows.

Source : Autoit Help Document
Posted by Catur Nugroho | File under :

Autoit Control Show Using C#

Shows a control that was hidden.
ControlShow ( "title", "text", controlID )

Parameters

title The title of the window to access.
text The text of the window to access.
controlID The control to interact with. See Controls.

Return Value

Success: Returns 1.
Failure: Returns 0 if window/control is not found.

Remarks

None.

Source : Autoit Help Document

Source Code Autoit Control Show In C#



Parameters Required :
Title : [CLASS:Notepad]
Text : ""
Control : Edit1

private void controlShowButton_Click(object sender, EventArgs e)
        {
            //open notepad program
            autoit.Run("notepad.exe", "", 1);

            //wait notepad opened until 10 seconds
            autoit.WinWait("[CLASS:Notepad]", "", 10);

            //hide text area
            autoit.ControlHide("[CLASS:Notepad]", "", "Edit1");
            Thread.Sleep(2000);

            //show text area
            autoit.ControlShow("[CLASS:Notepad]", "", "Edit1");
        }


Download Full Source Code
Posted by Catur Nugroho | File under :

Autoit Control Set Text Using C#

Sets text of a control.
ControlSetText ( "title", "text", controlID, "new text" [, flag] )

Parameters

title The title of the window to access.
text The text of the window to access.
controlID The control to interact with. See Controls.
new text The new text to be set into the control.
flag [optional] when different from 0 (default) will force the target window to be redrawn.

Return Value

Success: Returns 1.
Failure: Returns 0 if window/control is not found.

Remarks

None.

Source : Autoit Help Document

Source Code Autoit Control Set Text In C#



Parameters Required :
Title : [CLASS:Notepad]
Text : ""
Control : Edit1

private void controlSetTextButton_Click(object sender, EventArgs e)
        {
            //open notepad program
            autoit.Run("notepad.exe", "", 1);

            //wait notepad opened until 10 seconds
            autoit.WinWait("[CLASS:Notepad]", "", 10);

            //set the text
            autoit.ControlSetText("[CLASS:Notepad]", "", "Edit1", "autoitsourcecode.blogspot.com");
        }


Download Full Source Code
Posted by Catur Nugroho | File under :

Autoit Control Move Using C#

Moves a control within a window.
ControlMove ( "title", "text", controlID, x, y [, width [, height]] )

Parameters

title The title of the window to move.
text The text of the window to move.
controlID The control to interact with. See Controls.
x X coordinate to move to relative to the window client area.
y Y coordinate to move to relative to the window client area.
width [optional] New width of the window.
height [optional] New height of the window.

Return Value

Success: Returns 1.
Failure: Returns 0 if window/control is not found.

Remarks

If x and y equal to the Default keyword no move occurs, just resizing.

Source : Autoit Help Document

Source Code Autoit Control Move In C#

 


Parameters Required :
Title : [CLASS:Notepad]
Text : ""
Control : Edit1

private void controlMoveButton_Click(object sender, EventArgs e)
        {
            //open notepad program
            autoit.Run("notepad.exe", "", 1);

            //wait notepad opened until 10 seconds
            autoit.WinWait("[CLASS:Notepad]", "", 10);

            if (MessageBox.Show("Are you sure want to move it?", "confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
            {
                autoit.ControlMove("[CLASS:Notepad]", "", "Edit1", 200, 200, 200, 200);
            }
        }


Download Full Source Code
Posted by Catur Nugroho | File under :

Autoit Control Get List View Using C#

Sends a command to a ListView32 control.
ControlListView ( "title", "text", controlID, "command" [, option1 [, option2]] )

Parameters

title The title of the window to access.
text The text of the window to access.
controlID The control to interact with. See Controls.
command The command to send to the control (see below).
option1 [optional] Additional parameter required by some commands.
option2 [optional] Additional parameter required by some commands.

Return Value

Depends on command as table below shows. In case of an error (such as an invalid command or window/control could not be found) then @error is set to 1.


Command, Option1, Option2 Operation
"DeSelect", From [, To] Deselects one or more items.
"FindItem", "string to find" [, SubItem] Returns the item index of the string. Returns -1 if the string is not found.
"GetItemCount" Returns the number of list items.
"GetSelected" [, option] Returns a string containing the item index of selected items. If option=0 (default) only the first selected item is returned. If option=1 then all the selected items are returned delimited by |, e.g: "0|3|4|10". If no items are selected a blank "" string is returned.
"GetSelectedCount" Returns the number of items that are selected.
"GetSubItemCount" Returns the number of subitems.
"GetText", Item, SubItem Returns the text of a given item/subitem.
"IsSelected", Item Returns 1 if the item is selected, otherwise returns 0.
"Select", From [, To] Selects one or more items.
"SelectAll" Selects all items.
"SelectClear" Clears the selection of all items.
"SelectInvert" Inverts the current selection.
"ViewChange", "view" Changes the current view. Valid views are "list", "details", "smallicons", "largeicons".


All items/subitems are 0 based. This means that the first item/subitem in a list is 0, the second is 1, and so on.
In a "Details" view of a ListView32 control, the "item" can be thought of as the "row" and the "subitem" as the "column".

Remarks

Some commands may fail when using a 32-bit AutoIt process to read from a 64-bit process. Likewise commands may fail when using a 64-bit AutoIt process to read from a 32-bit process.

Source : Autoit Help Document
Posted by Catur Nugroho | File under :

Autoit Control Hide Using C#

Hides a control.
ControlHide ( "title", "text", controlID )

Parameters

title The title of the window to access.
text The text of the window to access.
controlID The control to interact with. See Controls.

Return Value

Success: Returns 1.
Failure: Returns 0 if window/control is not found.

Remarks

None.

Source : Autoit Help Document

Source Code Autoit Control Hide In C#

 


Parameters Required :
Title : [CLASS:Notepad]
Text : ""
Control : Edit1

private void controlHidebutton_Click(object sender, EventArgs e)
        {
            //open notepad program
            autoit.Run("notepad.exe", "", 1);

            //wait notepad opened until 10 seconds
            autoit.WinWait("[CLASS:Notepad]", "", 10);

            autoit.ControlHide("[CLASS:Notepad]", "", "Edit1");
        }


Download Full Source Code
Posted by Catur Nugroho | File under :

Autoit Control Get Text Using C#

Retrieves text from a control.
ControlGetText ( "title", "text", controlID )

Parameters

title The title of the window to access.
text The text of the window to access.
controlID The control to interact with. See Controls.

Return Value

Success: Returns text from a control.
Failure: Sets @error to 1 and returns a blank string of "".

Remarks

None.

Source : Autoit Help Document

Source Code Autoit Control Get Text In C#


Parameters Required :
Title : [CLASS:Notepad]
Text : ""
Control : Edit1

 private void controlGetTextbutton_Click(object sender, EventArgs e)
        {
            //open notepad program
            autoit.Run("notepad.exe", "", 1);

            //wait notepad opened until 10 seconds
            autoit.WinWait("[CLASS:Notepad]", "", 10);

            //send text example
            autoit.ControlSend("[CLASS:Notepad]", "", "Edit1", "autoitsourcode.blogspot.com", 0);

            //get the texts
            string strReturnText = autoit.ControlGetText("[CLASS:Notepad]", "", "Edit1");

            returnGetTextLabel.Text = "Return text from text area notepad : \n" + strReturnText;
        }


 Download Full Source Code


Posted by Catur Nugroho | File under :

Autoit Control Get Pos (x) (y) Using C#

Retrieves the position and size of a control relative to its window.
ControlGetPos ( "title", "text", controlID )

Parameters

title The title of the window to access.
text The text of the window to access.
controlID The control to interact with. See Controls.

Return Value

Success: Returns an array containing the size and the control's position relative to its client window:

$array[0] = X position

$array[1] = Y position

$array[2] = Width

$array[3] = Height
Failure: Sets @error to 1.

Remarks

The title/text is referencing the parent window, so be careful with "","" which references the active window which may not be the one containing the controlID control.

Source : Autoit Help Document

Source Code Autoit Control Get Pos (x) (y) In C#


Parameters Required :
Title : [CLASS:Notepad]
Text : ""
Control : Edit1

private void controlGetPosXYButton_Click(object sender, EventArgs e)
        {
            //open notepad program
            autoit.Run("notepad.exe", "", 1);

            //wait notepad opened until 10 seconds
            autoit.WinWait("[CLASS:Notepad]", "", 10);

            //get x position
            int iReturnGetPosX = autoit.ControlGetPosX("[CLASS:Notepad]", "", "Edit1");

            //get y position
            int iReturnGetPosY = autoit.ControlGetPosY("[CLASS:Notepad]", "", "Edit1");

            returnGetPosLabel.Text = "Autoit get control pos (x) : " + iReturnGetPosX.ToString() + "\n";
            returnGetPosLabel.Text += "Autoit get control pos (y) : " + iReturnGetPosY.ToString() + "\n";
        }


Download Full Source Code
Posted by Catur Nugroho | File under :

Autoit Control Get Post Height / Width Using C#

Retrieves the position and size of a control relative to its window.
ControlGetPos ( "title", "text", controlID )

Parameters

title The title of the window to access.
text The text of the window to access.
controlID The control to interact with. See Controls.

Return Value

Success: Returns an array containing the size and the control's position relative to its client window:

$array[0] = X position

$array[1] = Y position

$array[2] = Width

$array[3] = Height
Failure: Sets @error to 1.

Remarks

The title/text is referencing the parent window, so be careful with "","" which references the active window which may not be the one containing the controlID control.

Source : Autoit Help Document

Source Code Autoit Control Get Height / Width in C#


Parameters Required :
Title : [CLASS:Notepad]
Text : ""
Control : Edit1

private void controlGetPosButton_Click(object sender, EventArgs e)
        {
            //open notepad program
            autoit.Run("notepad.exe", "", 1);

            //wait notepad opened until 10 seconds
            autoit.WinWait("[CLASS:Notepad]", "", 10);

            //get height position
            int iReturnGetPosHeight = autoit.ControlGetPosHeight("[CLASS:Notepad]", "", "Edit1");

            int iReturnGetPosWidth = autoit.ControlGetPosWidth("[CLASS:Notepad]", "", "Edit1");

            returnGetPosLabel.Text = "Autoit get control height (x) : " + iReturnGetPosHeight.ToString() + "\n";
            returnGetPosLabel.Text += "Autoit get control width (y) : " + iReturnGetPosWidth.ToString() + "\n";
        }


 Download Full Source Code


Posted by Catur Nugroho | File under :

Autoit Control Get Handle Using C#

Retrieves the internal handle of a control.
ControlGetHandle ( "title", "text", controlID )

Parameters

title The title of the window to access.
text The text of the window to access.
controlID The control to interact with. See Controls.

Return Value

Success: Returns the handle (HWND) value.
Failure: Returns "" (blank string) and sets @error to 1 if no window matches the criteria.

Remarks

This function returns a HWND/Handle value.

Source : Autoit Help Document

Source Code Autoit Control Get Handle in C#



Parameters Required :
Title : [CLASS:Notepad]
Text : ""
Control : Edit1

private void controlGetHandleButton_Click(object sender, EventArgs e)
        {
            //open notepad program
            autoit.Run("notepad.exe", "", 1);

            //wait notepad opened until 10 seconds
            autoit.WinWait("[CLASS:Notepad]", "", 10);

            string strReturnHandle = autoit.ControlGetHandle("[CLASS:Notepad]", "", "Edit1");

            MessageBox.Show("Autoit control get handle return value : " + strReturnHandle);
        }


 Download Full Source Code
Posted by Catur Nugroho | File under :

Autoit Control Focus Using C#

Sets input focus to a given control on a window.
ControlFocus ( "title", "text", controlID )

Parameters

title The title of the window to access.
text The text of the window to access.
controlID The control to interact with. See Controls.

Return Value

Success: Returns 1.
Failure: Returns 0.

Remarks

None.

Source : Autoit Help Document

Source Code Autoit Control Enable / Disable in C#



Parameters Required :
Title : [CLASS:Notepad]
Text : ""
Control : Edit1

private void controlFocusButton_Click(object sender, EventArgs e)
        {
            //open notepad program
            autoit.Run("notepad.exe", "", 1);

            //wait notepad opened until 10 seconds
            autoit.WinWait("[CLASS:Notepad]", "", 10);

            //set the focus
            //in C# code similar with this.focus()           
            autoit.ControlFocus("[CLASS:Notepad]", "", "Edit1");
        }


Download Full Source Code

Posted by Catur Nugroho | File under :
Autoit Control Enable / Disable Using C#

Disables or "grays-out" a control.
ControlDisable ( "title", "text", controlID )

Parameters

title The title of the window to access.
text The text of the window to access.
controlID The control to interact with. See Controls.

Return Value

Success: Returns 1.
Failure: Returns 0.

Remarks

None.

Source : Autoit Help Document


Source Code Autoit Control Enable / Disable in C#

Parameters Required :
Title : [CLASS:Notepad]
Text : ""
Control : Edit1

private void controlDisableButton_Click(object sender, EventArgs e)
        {
            //open notepad program
            autoit.Run("notepad.exe", "", 1);

            //wait notepad opened until 10 seconds
            autoit.WinWait("[CLASS:Notepad]", "", 10);

            //Disable Text Area
            autoit.ControlDisable("[CLASS:Notepad]", "", "Edit1");
            Thread.Sleep(1000);

            //Enable Text Area
            autoit.ControlEnable("[CLASS:Notepad]", "", "Edit1");
            Thread.Sleep(1000);
        }


Download Full Source Code
Posted by Catur Nugroho | File under :

Autoit Control Command Using c#

Sends a command to a control.
ControlCommand ( "title", "text", controlID, "command" [, "option"] )

Parameters

title The title of the window to access.
text The text of the window to access.
controlID The control to interact with. See Controls.
command The command to send to the control.
option [optional] Additional parameter required by some commands.

Return Value

Depends on command as table below shows. In case of an error (such as an invalid command or window/control), @error=1.
Command, Option Return Value
"IsVisible", "" Returns 1 if Control is visible, 0 otherwise
"IsEnabled", "" Returns 1 if Control is enabled, 0 otherwise
"ShowDropDown", "" Drops a ComboBox
"HideDropDown", "" Undrops a ComboBox
"AddString", 'string' Adds a string to the end in a ListBox or ComboBox
"DelString", occurrence Deletes a string according to occurrence in a ListBox or ComboBox
"FindString", 'string' Returns occurrence ref of the exact string in a ListBox or ComboBox
"SetCurrentSelection", occurrence Sets selection to occurrence ref in a ListBox or ComboBox
"SelectString", 'string' Sets selection according to string in a ListBox or ComboBox
"IsChecked", "" Returns 1 if Button is checked, 0 otherwise
"Check", "" Checks radio or check Button
"UnCheck", "" Unchecks radio or check Button
"GetCurrentLine", "" Returns the line # where the caret is in an Edit
"GetCurrentCol", "" Returns the column # where the caret is in an Edit
"GetCurrentSelection", "" Returns name of the currently selected item in a ListBox or ComboBox
"GetLineCount", "" Returns # of lines in an Edit
"GetLine", line# Returns text at line # passed of an Edit
"GetSelected", "" Returns selected text of an Edit
"EditPaste", 'string' Pastes the 'string' at the Edit's caret position
"CurrentTab", "" Returns the current Tab shown of a SysTabControl32
"TabRight", "" Moves to the next tab to the right of a SysTabControl32
"TabLeft", "" Moves to the next tab to the left of a SysTabControl32
"SendCommandID", Command ID Simulates the WM_COMMAND message. Usually used for ToolbarWindow32 controls - use the ToolBar tab of Au3Info to get the Command ID.

Remarks

Some controls will resist automation unless they are the active window. Use the WinActivate() function to force the control's window to the top before using ControlCommand() on these controls.

Certain commands that work on normal Combo and ListBoxes do not work on "ComboLBox" controls.

Source : Autoit Help Document

Source Code In C#

Parameter requires :
Title : [CLASS:Notepad]
Text : ""
Control : Edit1
Command : GetLineCount
Extra : ""

 private void controlCommandButton_Click(object sender, EventArgs e)
        {
            //open notepad program
            autoit.Run("notepad.exe", "", 1);

            //wait notepad opened until 10 seconds
            autoit.WinWait("[CLASS:Notepad]", "", 10);

           //get line count
            string strText = autoit.ControlCommand("[CLASS:Notepad]", "", "Edit1", "GetLineCount", "");
        }


 Download Full Source Code
Posted by Catur Nugroho | File under :

Autoit Control Click Using C

Sends a mouse click command to a given control.
ControlClick ( "title", "text", controlID [, button [, clicks [, x [, y]]]] )

Parameters

title The title of the window to access.
text The text of the window to access.
controlID The control to interact with. See Controls.
button [optional] The button to click, "left", "right", "middle", "main", "menu", "primary", "secondary". Default is the left button.
clicks [optional] The number of times to click the mouse. Default is 1.
x [optional] The x position to click within the control. Default is center.
y [optional] The y position to click within the control. Default is center.

Return Value

Success: Returns 1.
Failure: Returns 0.

Remarks

Some controls will resist clicking unless they are the active window. Use the WinActivate() function to force the control's window to the top before using ControlClick().
Using 2 for the number of clicks will send a double-click message to the control - this can even be used to launch programs from an explorer control!

If the user has swapped the left and right mouse buttons in the control panel, then the behaviour of the buttons is different. "Left" and "right" always click those buttons, whether the buttons are swapped or not. The "primary" or "main" button will be the main click, whether or not the buttons are swapped. The "secondary" or "menu" buttons will usually bring up the context menu, whether the buttons are swapped or not.

Button Normal Swapped
"" Left Left
"left" Left Left
"middle" Middle Middle
"right" Right Right
"primary" Left Right
"main" Left Right
"secondary" Right Left
"menu" Right Left


Source : Autoit Help Document

Example code Using c#


Parameters required :
Title : [CLASS:SciCalc]
Text : ""
Control : left
Button :  Button5
NumClick : 1
X : 16
Y : 14

For example we will use a calculator and click some numbers.

Added this script into project :
private void Click(string strButton, int nNumClicks, int nX, int nY, string strControl, int nSleep, string strWindow)
        {
            _AutoIT.ControlClick(strWindow, "", strControl, strButton, nNumClicks, nX, nY);

            for (int i = 0; i < (nSleep / 100); i++)
            {
                Application.DoEvents();
                Thread.Sleep(100);
            }
        }

Run the command :
private void controlClickButton_Click(object sender, EventArgs e)
        {

            //open calculator program
            autoit.Run("calc.exe", "", 3);
            Thread.Sleep(1000);

            //click number 7
            Click("left", 1, 16, 14, "Button5", 1000, "[CLASS:SciCalc]");

            //click number 3
            Click("left", 1, 19, 14, "Button15", 1000, "[CLASS:SciCalc]");
        }

Download Full Source Code
Posted by Catur Nugroho | File under :

ControlSend

Sends a string of characters to a control.
ControlSend ( "title", "text", controlID, "string" [, flag] )

Parameters
  1. title : The title of the window to access.
  2. text : The text of the window to access.
  3. controlID : The control to interact with. See Controls.
  4. string : String of characters to send to the control.
  5. flag : [optional] Changes how "keys" is processed: flag = 0 (default), Text contains special characters like + to indicate SHIFT and {LEFT} to indicate left arrow. flag = 1, keys are sent raw.

Return value 
Success: Returns 1.
Failure: Returns 0 if window/control is not found.

Remark
ControlSend works in a similar way to Send but it can send key strokes directly to a window/control, rather than just to the active window.

ControlSend is only unreliable for command prompts as that works differently to normal windows (seems to check physical states rather than accepting the keystroke messages). For normal windows ControlSend should be way more reliable than a normal Send - and yes it does send shift, ctrl, alt etc.

As mention in the Send help the keyboard that send different chars when in CAPS LOCK and using the Shift Key cannot be simulated. An example is the Czech Keyboard. A good workaround is to use the ControlSetText.

The control might first need to be given focus with the ControlFocus command, specially when referencing an controlID created by the script itself.

Opt("SendKeyDelay",...) alters the length of the brief pause in between sent keystrokes.
Opt("SendKeyDownDelay",...) alters the length of time a key is held down before being released during a keystroke.

 Source : Auotit Help Documents

This is the code in c# how autoit control send worked with little modification to getting perfect result.

private void Type(string strKeys, int nMode, string strControl, bool blnSleepKeys, int nSleep, string strWindow)
        {
            if (blnSleepKeys)
            {
                char[] achKeys = strKeys.ToCharArray();

                foreach (char ch in achKeys)
                {
                    _AutoIT.ControlSend(strWindow, "", strControl, ch.ToString(), nMode);
                    Thread.Sleep(100);
                    Trace.WriteLine("Sent Key: '" + ch.ToString() + "'");
                }
            }
            else
            {
                _AutoIT.ControlSend(strWindow, "", strControl, strKeys, nMode);
                Trace.WriteLine("Sent Keys: " + strKeys);
            }

            for (int i = 0; i < (nSleep / 100); i++)
            {
                Application.DoEvents();
                Thread.Sleep(100);
            }
        }

How it work? 

You can try to call notepad and send some key.
Gathering window information :
Drag Finder Tool and drop into notepad text area.



Parameters required :
Click Control Tab (Parameters required in there)
Tittle : Untitled - Notepad or Title : [CLASS:Notepad]
Text : Optional
Control : Edit1

Source Code C#
private void btnSend_Click(object sender, EventArgs e)
        {
            //run notepad.exe program
            autoit.Run("notepad.exe", "", 1);
            Thread.Sleep(1000);

            Type("autoitsourcecode.blogspot.com", 0, "Edit1", true, 2000, "[CLASS:Notepad]");
        }

with send key ENTER
private void btnSendEnter_Click(object sender, EventArgs e)
        {
            //run notepad.exe program
            autoit.Run("notepad.exe", "", 1);
            Thread.Sleep(1000);

            for (int x = 1; x < 5; x++)

            {
                //send keys
                Type("autoitsourcecode.blogspot.com", 0, "Edit1", true, 2000, "[CLASS:Notepad]");

                //send Enter

                Type("{ENTER}", 0, "Edit1", false, 2000, "[CLASS:Notepad]");
            }
           
        }

Download Full Source Code

Posted by Catur Nugroho | File under :
Autoit.Run()

Runs an external program.
Run ( "program" [, "workingdir" [, show_flag[, opt_flag ]]] ).

Parameters :
  1.  program : The full path of the program (EXE, BAT, COM, or PIF) to run (see remarks).
  2.  workingdir :  [optional] The working directory. This is not the path to the program.
  3.  show_flag :
    [optional] The "show" flag of the executed program:
    @SW_HIDE = Hidden window (or Default keyword)
    @SW_MINIMIZE = Minimized window
    @SW_MAXIMIZE = Maximized window
  4.  opt_flag :[optional] Controls various options related to how the parent and child process interact.
    0x1 ($STDIN_CHILD) = Provide a handle to the child's STDIN stream
    0x2 ($STDOUT_CHILD) = Provide a handle to the child's STDOUT stream
    0x4 ($STDERR_CHILD) = Provide a handle to the child's STDERR stream
    0x8 ($STDERR_MERGED) = Provides the same handle for STDOUT and STDERR. Implies both $STDOUT_CHILD and $STDERR_CHILD.
    0x10 ($STDIO_INHERIT_PARENT) = Provide the child with the parent's STDIO streams. This flag can not be combined with any other STDIO flag. This flag is only useful when the parent is compiled as a Console application.
    0x10000 ($RUN_CREATE_NEW_CONSOLE) = The child console process should be created with it's own window instead of using the parent's window. This flag is only useful when the parent is compiled as a Console application.
Return Value
Success: The PID of the process that was launched.
Failure: Returns 0 and sets @error to non-zero.

Remarks
Paths with spaces need to be enclosed in quotation marks.

To run DOS (console) commands, try Run(@ComSpec & " /c " & 'commandName', "", @SW_HIDE) ; don't forget " " before "/c"

After running the requested program the script continues. To pause execution of the script until the spawned program has finished use the RunWait function instead.

Providing the Standard I/O parameter with the proper values permits interaction with the child process through the StderrRead, StdinWrite and StdoutRead functions. Combine the flag values (or use $STDERR_CHILD, $STDIN_CHILD & $STDOUT_CHILD, defined in Constants.au3) to manage more than one stream.

In order for the streams to close, the following conditions must be met: 1) The child process has closed it's end of the stream (this happens when the child closes). 2) AutoIt must read any captured streams until there is no more data. 3) If STDIN is provided for the child, StdinWrite() must be called to close the stream. Once all streams are detected as no longer needed, all internal resources will automatically be freed.
StdioClose can be used to force the STDIO streams closed.

Source : Autoit Help Documentary

Source Code Using C#

//add namespace
using AutoItX3Lib;

//call variable
private AutoItX3 _autoit = new AutoItX3();

private void button1_Click(object sender, EventArgs e)
        {
            //call nodepad
            string strRun = @"notepad.exe";

            //call another application
            //string strRun = @"C:\Program Files\Notepad++\notepad++.exe";

            //execute the program
            _autoit.Run(strRun, "", 1);
        }
Posted by Catur Nugroho | File under :
How to Use Autoit in c#?

You just need to adding the autoit class library to your project. Go to Solution Explorer - right click Refences - select Add Reference.


Click Browse tab and go to C:\Program Files\AutoIt3\AutoItX\AutoItX3.dll


Autoit class library ready to used.
If you have not installed autoit, please go to my article Hot to Use And Instal Autoit Window info or download AutoitX3.dll here.

This is sample code how autoitX3.dll work :


Thank you for reading.

Posted by Catur Nugroho | File under :
First time you need to download and instal Autoit application, download at here http://www.autoitscript.com/site/autoit/downloads/.

This is basic image of autoit :


Find the basic control info win form application
Drag and drop Finder Tool to win form application.


You will find the Basic Window Info (Title, Class), Basic Control Info (Class, Instance) this is text box name of notepad.


This is the basis of how autoit work. May be useful for people who are learning about how to work with Autoit Window Info.

to be continue how work how autoit worked in the code