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.