Convert Script using Var SetCapacity and Advanced Window Snapping in AutoHotkey v2
AutoHotkey (AHK) is a powerful scripting language for automating tasks in Windows. With the release of AutoHotkey v2, there have been several improvements and changes to the language, including the addition of new functions and methods for working with windows. This article will focus on how to use the Var SetCapacity and Advanced Window Snapping functions in AutoHotkey v2 to create a script that snaps windows to various places on a multi-monitor setup.
Prerequisites
Before diving into the script, it is important to note that this article assumes you have a basic understanding of AutoHotkey and the AutoHotkey v2 syntax. Additionally, you will need to have a multi-monitor setup in order to fully test and utilize the script.
Getting Started
The first step in creating our script is to include the necessary libraries. In this case, we will be using the ComObjActive library to interact with the Windows API and the WinAPI library to access advanced window manipulation functions. To include these libraries, add the following lines at the beginning of your script:
#Include ComObjActive.ahk
#Include WinAPI.ahkVar SetCapacity
In AutoHotkey v2, the Var SetCapacity function allows you to pre-allocate memory for a variable. This can be useful when working with large data sets or in situations where you need to quickly allocate memory for a variable. In our script, we will use Var SetCapacity to pre-allocate memory for a structure that will hold information about each monitor in our multi-monitor setup.
Var SetCapacity(monitorInfo, 10, "uint")In this example, we are pre-allocating memory for an array of 10 unsigned integers. This array will be used to store the width, height, and position of each monitor in our setup.
Advanced Window Snapping
The Advanced Window Snapping function in AutoHotkey v2 allows you to easily snap windows to the edges of the screen or to specific positions on the screen. This can be useful for creating a more organized and efficient workspace. In our script, we will use Advanced Window Snapping to snap windows to various places on our multi-monitor setup.
WinMove, A, , 0, 0, monitorInfo[A_Index].width, monitorInfo[A_Index].heightIn this example, we are using the WinMove function to move the active window to the top-left corner of the current monitor. The position and size of the window are determined by the width and height of the current monitor, which are stored in the monitorInfo array.
GetMonitorIndexFromWindow
In order to determine which monitor a window is currently on, we will use the GetMonitorIndexFromWindow function. This function takes the handle of a window as an argument and returns the index of the monitor that the window is currently on.
GetMonitorIndexFromWindow(WinExist("A"))In this example, we are using the WinExist function to get the handle of the active window and then passing that handle to the GetMonitorIndexFromWindow function. The function will return the index of the monitor that the active window is currently on.
Putting it all Together
Now that we have covered the key concepts and functions, let's put it all together and create our script. The following script will snap the active window to the top-left corner of the current monitor:
#Persistent
#SingleInstance, Force
; Include necessary libraries
#Include ComObjActive.ahk
#Include WinAPI.ahk
; Pre-allocate memory for monitor information array
Var SetCapacity(monitorInfo, 10, "uint")
; Get the number of monitors and populate the monitor information array
numMonitors := ComObjActive("WmiMonitorConnectionParams").Instances.Count
Loop, %numMonitors% {
monitorInfo[A_Index - 1].width := ComObjActive("WmiMonitorConnectionParams").Instances[A_Index - 1].Width
monitorInfo[A_Index - 1].height := ComObjActive("WmiMonitorConnectionParams").Instances[A_Index - 1].Height
}
; Define hotkey for snapping window
^#Space::
; Get the index of the current monitor
currentMonitor := GetMonitorIndexFromWindow(WinExist("A"))
; Move the active window to the top-left corner of the current monitor
WinMove, A, , 0, 0, monitorInfo[currentMonitor].width, monitorInfo[currentMonitor].height
return
- AutoHotkey v2 introduces the Var SetCapacity function for pre-allocating memory for variables.
- The Advanced Window Snapping function in AutoHotkey v2 allows you to easily snap windows to the edges of the screen or to specific positions on the screen.
- The GetMonitorIndexFromWindow function can be used to determine which monitor a window is currently on.
- By combining these functions, you can create a script that snaps windows to various places on a multi-monitor setup.