AutoHotkey 语言 利用 AutoHotkey 调整窗口大小和位置

AutoHotkey阿木 发布于 3 天前 3 次阅读


AutoHotkey Language: Adjusting Window Size and Position

AutoHotkey is a powerful scripting language designed for automating the Windows GUI and general scripting. It is particularly useful for automating repetitive tasks, such as adjusting window size and position. In this article, we will delve into the intricacies of AutoHotkey scripting to explore various methods for manipulating window properties.

Introduction to AutoHotkey

AutoHotkey is an open-source scripting language for Microsoft Windows that allows users to automate any desktop task. It can simulate keyboard and mouse events, work with windows and menus, and interact with the clipboard. AutoHotkey scripts are written in a simple, easy-to-understand syntax and can be executed directly from the command line or compiled into an executable file.

Basic Concepts

Before we dive into the code, let's go over some basic concepts that will be useful in our scripting journey:

1. Variables: AutoHotkey supports various data types, including integers, floats, strings, and arrays. Variables are used to store data and can be manipulated throughout the script.
2. Functions: Functions are reusable blocks of code that perform a specific task. They can accept parameters and return values.
3. Hotkeys: Hotkeys are keyboard shortcuts that trigger a script or a specific command. They can be used to automate tasks with a single keystroke.
4. WinGet and WinSet: These are built-in functions used to retrieve and set window properties, respectively.

Adjusting Window Size

To adjust the size of a window, we can use the `WinSet` function with the `Size` option. Here's an example script that resizes the active window to 800x600 pixels:

ahk
WinSet, Size, 800x600, A

In this script, `WinSet` is called with three arguments: the property to set (`Size`), the new size (`800x600`), and the window to apply the change to (`A`, which represents the active window).

Adjusting Window Position

To change the position of a window, we can use the `WinSet` function with the `Position` option. Here's an example script that moves the active window to the center of the screen:

ahk
WinGet, WinWidth, Width, A
WinGet, WinHeight, Height, A
WinGet, DeskWidth, Width, ahk_class Desktop
WinGet, DeskHeight, Height, ahk_class Desktop
WinSet, Position, % (DeskWidth - WinWidth) / 2 . "x" . (DeskHeight - WinHeight) / 2, A

In this script, we first retrieve the width and height of the active window using `WinGet`. Then, we get the width and height of the desktop using the same function. Finally, we calculate the new position by subtracting the window dimensions from the desktop dimensions and dividing by 2 to center the window.

Combining Size and Position

To resize and position a window simultaneously, you can combine the `Size` and `Position` options in a single `WinSet` call:

ahk
WinSet, Size and Position, 800x600, A

This will resize and position the active window to 800x600 pixels at the center of the screen.

Dynamic Window Manipulation

In some cases, you may want to dynamically adjust the window size and position based on certain conditions. Here's an example script that resizes and positions the active window based on the current time:

ahk
Time := A_Hour . ":" . A_Minute
If (Time >= "09:00" && Time <= "17:00")
{
WinSet, Size and Position, 800x600, A
}

In this script, we first retrieve the current time. If the time is between 9:00 AM and 5:00 PM, the active window is resized and positioned to 800x600 pixels.

Advanced Techniques

AutoHotkey offers several advanced techniques for manipulating windows, such as:

1. Window States: You can use the `WinSet` function to change the state of a window, such as minimizing, maximizing, or restoring it.
2. Window Transparency: The `WinSet` function can also be used to adjust the transparency of a window.
3. Window Z-order: The `WinSet` function can change the Z-order of a window, bringing it to the front or sending it to the back.

Here's an example script that demonstrates some of these advanced techniques:

ahk
; Minimize the active window
WinMinimize, A

; Maximize the active window
WinMaximize, A

; Restore the active window
WinRestore, A

; Set the active window to be on top
WinSet, AlwaysOnTop, On, A

; Set the active window to be transparent (50%)
WinSet, TransColor, 0xFF0000, A

; Bring the active window to the front
WinSet, Top, , A

; Send the active window to the back
WinSet, Bottom, , A

Conclusion

In this article, we explored various methods for adjusting window size and position using AutoHotkey scripting. By utilizing the `WinSet` function and combining it with other built-in functions, you can create powerful scripts that automate window manipulation tasks. Whether you're looking to resize and position windows for productivity or to create custom UIs, AutoHotkey is a versatile tool that can help you achieve your goals.