Autoit automated programming (2)

Source: Internet
Author: User

Note: The window title and window text parameters are always case sensitive.
1. Wait for window commands/functions
Both Ahk and au3 provide a similar set of window wait commands/functions: winwait/winwaitactive/winwaitclose.
They are used to wait for the window to appear, wait for the window to be activated, and wait for the window to be closed. Because these command/function parameters are similar, we only use winwait as an example.
Ahk:
Winwait [, window title, window text, timeout time, exclude title, exclude text]
Au3:
Winwait ("window title" [, "window text" [, timeout])
Winwait is used to stop executing all subsequent statements before the target window appears.
Suppose we want to run notepadProgramAnd prompt the user when the window appears:
[Example 3.1.1]

Ahk:
Run notepad
Winwait, untitled-Notepad
The msgbox notepad window has been opened!
Au3:
Run ("Notepad ")
Winwait ("untitled-Notepad ")
Msgbox (0, "", "the notepad window has been opened! ")

2. Activation window-related commands/functions
To make the target window an active window, activate it. The available command/function is winactivate:
Ahk:
Winactivate [, window title, window text, exclude title, exclude text]
Au3:
Winactivate ("window title" [, "window text"])

3. Close the window
You can close a window in either of the following ways ):
Ahk:
Winclose/winkill [, window title, window text, timeout time, exclude title, exclude text]
Au3:
Winclose/winkill ("window title" [, "window text"])

Now we can implement a relatively simple function. For example, we can open the System Properties window and wait for it to appear, activate it after the window appears, and then close it after 3 seconds:
[Example 3.1.2]

Ahk:
Run, sysdm. CPL
Winwait, system attributes
Winactivate, system attribute
Winwaitactive, system attribute
Sleep 3000
Winclose, system attribute
Winwaitclose, system attribute
Au3:
Run ("control sysdm. Cpl ")
Winwait ("system attributes ")
Winactivate ("system attributes ")
Winwaitactive ("system attributes ")
Sleep (3000)
Winclose ("system attributes ")
Winwaitclose ("System Properties ")

Suggestion: if these window titles are frequently used in the program, it may lead to a problem: the readability of the script. Maybe you will think, isn't it very intuitive? But what if the title of the repeated window is a long string? This will seriously affect the entireCodeBeautiful layout. In addition, we do not know how the title of these windows comes from. If we define a variable (assuming the variable name is "appwindow1") to save the window title, we can use the variable to represent it in the command/function, in this way, the intention of the Code is clearer. In addition, even if the target software changes its window title for some reason (such as upgrade), we can easily make modifications.

4. More Accurate window identification method (mainly for Ahk scripts)
The program has at least one process during running. If you can obtain this process ID, you can ensure accurate identification of the window to a certain extent. In addition, each window has a defined window class name (class, for example, notepad is the class name of the notepad window), so we can exclude other window classes different from the target window. In fact, we have another more accurate method:
Each window (including controls) is assigned a unique identifier (ID) that distinguishes it from other windows, which is called the window handle (hwnd ).
A disadvantage of directly specifying the window title to indicate the window is that it cannot be ensured that the window is always the operation target during script running, in this process, there may be other windows with the same name (or Windows Meeting the matching conditions, if we use this identifier to indicate the window, we can ensure that the operation window of the command/function is always the same window.
Let's take a look at the command/function to get the window handle:
Ahk:
Winget [, output variable, ID, window title, window text, exclude title, exclude text]
Au3:
Wingethandle ("window title" [, "window text"])
The window ID obtained by winget is returned through the "output variable", and the return value of wingethandle is the obtained window ID.

When performing automated operations, we need to run a program first. How can we obtain the window handle displayed after the program runs successfully? An insurance method is to first obtain the process ID of the program, and then obtain its window Handle Based on the process ID. Ahk supports using the process ID as the window title; however, au3 does not support this function. You can only obtain the Class Name of the window and then obtain the window handle based on the class name (not safe enough ):
[Example 3.1.3]
Ahk:
Run, notepad, thispid
Winwait, ahk_pid % thispid %

The ahk_pid indicates that the variable following is the process ID.
Winget, thisid, ID, ahk_pid % thispid %

; Thisid will save the obtained window handle
Au3:

Opt ("wintitlematchmode", 4)
Run ("Notepad ")
$ Handle = wingethandle ("classname = Notepad ")
Now we forget au3, because its window functions generally do not support the use of window handle as the window title parameter.
As for how to use a window handle in Ahk, simply put, any command with the "window title" parameter can be replaced by a window handle, for example:
[Example 3.1.4]
Ahk:
Run, notepad, thispid

First, obtain the process ID of the running Notepad program.
Winwait, untitled-Notepad ahk_pid % thispid %

; Wait for the process window to appear
Winget, thishwnd, ID, untitled-Notepad ahk_pid % thispid %

; Get the window handle
Winactivate, ahk_id % thishwnd %
The ahk_id indicates that the variable following is the window handle.
Winwaitactive, ahk_id % thishwnd %
Sleep 3000
Winclose, ahk_id % thishwnd %
Winwaitclose, ahk_id % thishwnd %

Detailed source reference: http://www.jb51.net/article/14870_3.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.