Posted by Catur Nugroho | File under :
How to read text from xps document using C#.

The purpose of this article is a personal note from the author. When the project is lost or forgotten so it is easier to find on this page. For over 2 years ago I use this code and really helped me.

I'm sorry, My English is not very good and and a little difficult to make the article.

The first step please add some this references of dot net class library :
1. PresentationCore.
2. PresentationFramework.
3. ReachFramework
4. System
5. System.Core.
6. System.Data
7. System.Data.DataSetExtensions
8. System.Deployment
9. System.Drawing
10. System.Runtime.Serialization
11. System.ServiceModel
12. System.Windows.Forms
13. System.Xml
14. System.Xml.Linq
15. WindowsBase.

Add this code into your name space :
using System.Threading;
using System.Windows.Xps.Packaging;
using System.Windows.Documents;


Usually I using threading to run the function. You can use the following code :
Thread _thMain = new Thread(new ThreadStart(StepOne)); //StepOne is function.
_thMain.IsBackground = true;
_thMain.SetApartmentState(ApartmentState.STA);
_thMain.Start();


The first way :
private void StepOne()
        {
            List<string> lData = new List<string>();
            using (XpsDocument xpsDoc = new XpsDocument(@"C:\sample.xps", System.IO.FileAccess.Read))
            {
                FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
                Dictionary<string, string> docPageText = new Dictionary<string, string>();
                for (int pageNum = 0; pageNum < docSeq.DocumentPaginator.PageCount; pageNum++)
                {
                    DocumentPage docPage = docSeq.DocumentPaginator.GetPage(pageNum);
                    foreach (System.Windows.UIElement uie in ((FixedPage)docPage.Visual).Children)
                    {
                        if (uie is System.Windows.Documents.Glyphs)
                        {
                            lData.Add(((System.Windows.Documents.Glyphs)uie).UnicodeString);
                        }
                    }
                }
            }

            string strText = string.Empty;
            foreach (string strItem in lData)
            {
                if (string.IsNullOrEmpty(strItem))
                    continue;

                strText += strItem;
            }
        }


The second way :
private void StepTwo()
        {
            List<string> lData = new List<string>();
            using (XpsDocument xpsDoc = new XpsDocument(@"C:\sample.xps", System.IO.FileAccess.Read))
            {
                FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
                Dictionary<string, string> docPageText = new Dictionary<string, string>();
                for (int pageNum = 0; pageNum < docSeq.DocumentPaginator.PageCount; pageNum++)
                {
                    DocumentPage docPage = docSeq.DocumentPaginator.GetPage(pageNum);
                    foreach (System.Windows.UIElement uie in ((FixedPage)docPage.Visual).Children)
                    {
                        if (uie is System.Windows.Documents.Glyphs)
                        {
                            //lData.Add(((System.Windows.Documents.Glyphs)uie).UnicodeString);
                            string strUnicode = ((System.Windows.Documents.Glyphs)uie).UnicodeString;
                            string strIndices = ((System.Windows.Documents.Glyphs)uie).Indices;
                            string strFormattedLine = "";
                            string[] astrIndices = strIndices.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                            for (int i = 0; i < astrIndices.Length; i++)
                            {
                                strFormattedLine += strUnicode[i].ToString();

                                if (astrIndices[i].Contains(","))
                                {
                                    if (int.Parse(astrIndices[i].Substring(astrIndices[i].IndexOf(",") + 1)) > 150)
                                        if (!strUnicode[i].ToString().Equals(" "))
                                            strFormattedLine += " ";
                                }
                            }

                            lData.Add(strFormattedLine);
                        }
                    }
                }
            }

            string strText = string.Empty;
            foreach (string strItem in lData)
            {
                if (string.IsNullOrEmpty(strItem))
                    continue;

                strText += strItem;
            }
        }


The difference in the two ways above is space.
Sometimes we will find a line that does not have the correct limit so we often struggle to define the data that we want.
Example :
The first way :
444444444444444444444 
The second way :
 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

I hope this article can help you in managing text in the XPS documents. 

Good Luck!! 

 
 
Posted by Catur Nugroho | File under :
Simple PDF Automation Using Autoit in C#

The topic for this article is how read the text of a PDF document, export into txt file, and return the results as text or string.

This is code for adobe reader X Version 10.1.3, so if the next time you get some code that doesn't run as well might be due to different versions.

This article will explain clearly how we can make simple PDF Automation. If you can't understanding  the code, I have set up the source code so you can debug the code and you can download it from here.

Let's start the code :

This is the image information from the PDF 

 
Parameters :
Title : "[CLASS:AcrobatSDIWindow]" or "sample.pdf - Adobe Reader"
Text : ""

private void RunButton_Click(object sender, EventArgs e)
{
        //open pdf application
        //change it if you have different pdf version 10.1.3
        string strAcrobatReaderType = "AcroRd32";        string strDir = Application.ExecutablePath.Substring(0,      Application.ExecutablePath.LastIndexOf("\\bin") + 1) + "files\\sample.pdf";
        ProcessStartInfo pInfo = new ProcessStartInfo(@"C:\Program Files\Adobe\Reader 10.0\Reader\" + strAcrobatReaderType + ".exe", strDir);
        Process.Start(pInfo);


       //wait the window is opened
       autoit.WinWaitActive("[CLASS:AcrobatSDIWindow]", "", 50);
       if (autoit.WinExists("[CLASS:AcrobatSDIWindow]", "") == 0)
       {
                MessageBox.Show("Windows didn't open");
        }


       //save as text document
       //send keys ALT + f + a

       autoit.Send("!fax", 0);


Save As Image Window Info


Parameters :
Title : "Save As"
Text : ""
Control : Edit1

      string strSaveAsTitle = "Save As";
      string strDir = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\bin") + 1) + "files\\sample.txt";
      int iCountRentry = 0;
     

      //Wait Save As Window is open and check if don't open
      autoit.WinWaitActive(strSaveAsTitle, "", 1000);
      if (autoit.WinExists(strSaveAsTitle, "") == 0)
      {
                MessageBox.Show("Save As Windows didn't open");
       }


       //send keys file directory to save
       do
       {
                if (autoit.ControlGetText(strSaveAsTitle, "", "Edit1").Equals(strDir))
                break;

                //send CTRL + a
                Type("^a", 0, "Edit1", true, 1000, strSaveAsTitle);
                Type(strDir, 0, "Edit1", true, 1000, strSaveAsTitle);

                iCountRentry++;
        }
        while (iCountRentry < 10);
        if (iCountRentry > 10)
        {
                MessageBox.Show("Error sending keys file directory");
        }


Click Save Button Image

Parameters :
Title : "Save As"
Text : ""
Control : Button2
Control Click X : 38
Control Click Y : 10 

        //You have 2 option to save the text document
        //using click button Save As

        Click("left", 1, 44, 12, "Button2", 1000, strSaveAsTitle);

        //or using send key ENTER
        //Type("{ENTER}", 0, "Button2", false, 1000, strSaveAsTitle);

        //read text

        iCountRentry = 0;
        do
        {
                try
                {
                        using (StreamReader reader = new StreamReader(strDir))
                        {
                                 resultTextBox.Text = reader.ReadToEnd();
                        }
                        break;
                 }
                 catch
                
                        Application.DoEvents();
                        Thread.Sleep(1000);
                        iCountRentry++;
                 }
           } while (iCountRentry < 10);

}

Notes :
If this project  didn't working, you have to change some code such as Title, Control, Control Click X and Y. Autoit working because of the screen according to the PC you are using.
Using function of autoit control is better than the others because it is more accurate if used on another PC.

If you getting problems about this please leave a note in the comments so I can help you.

Download Full Source Code 


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