Computer Graphics for AutoHotkey On-Screen Measurements

Inspired by a Doctor Taking On-Screen Measurements from Diagnostic Images with a Ruler, this Series of Blogs Builds a System for Calibrating and Measuring Digital Map Distances Using AutoHotkey Graphics

This image has an empty alt attribute; its file name is jackslibrarybookdeal250.png

Following list consists of a series of articles I wrote as blogs while developing the MouseMeasure.ahk script for calculating distances from an onscreen computer image. They appear in the order that I wrote and published each blog. Click the associated link to access the complete blog.

If you find this series useful, please consider purchasing one of my AutoHotkey books on a variety of other AutoHotkey topics.

Capturing Computer Screen Measurements (An AutoHotkey Tool)

Calibrate the MouseMeasure AutoHotkey Tool to Grab Calculated Lengths from Your Computer Monitor

“You can pick coordinates off the screen with the MouseGetPos command and save them. Then you can possibly use two clicks to calculate the difference between the two in pixels then convert it to your scale. There are a number of methods for sending data to documents.”

Pythagorean Theorem

Pythagoras gave every high school math student a reason to remember his name. He provided the method for calculating the hypotenuse of a right triangle. For most people, the formula fell into the toolbox of things-I’ll-never-do-again. But for anyone who wants to measure distances on a computer screen, the Pythagorean Theorem returns with a vengeance.

Measuring Spans on a Computer Screen

Computer screens use pixels in a matrix of rows and columns to create text and images. Each pinpoint can vary in color and pixel density (dots per inch) depends upon the monitor’s resolution, but the dot matrix sits stationary on the screen. Capturing the number of pixels in a row or column does not present much of a challenge. However, the length of a hypotenuse (C in the image) requires a special calculation:

While difficult to work out with pencil and paper, an AutoHotkey calculation can quickly return the distance in pixels between two sets of screen coordinates—(sx,sy) and (x,y). (Go to blog…)

Quick and Dirty Multi-Line Text Formatting (AutoHotkey Tip)

Formatting Text for Various AutoHotkey Tools Can Get Tedious—Use AutoHotkey Section Continuation to Simplify the Work

In the MouseMeasure.ahk script (discussed in the previous blog), I added a pop-up window displaying instructions on how to use the app. I formatted the text with a number of new lines and tabs. Usually, this would have required careful placement of the special escape characters, but not this time.

When adding instructions to a MsgBox, we often resort to adding returns (`r), newlines (`n), and tabs (`t) to create the desired layout. While this works, it often produces perverse effects—especially when using individual line continuation techniques to wrap the code for display purposes. However, AutoHotkey offers an alternative type of code continuation which makes text formatting quick and easy— continuation sections (Method #2). (Go to blog…)

Drawing Lines on Screens with AutoHotkey (Graphics Tips)

Although a Little Tricky, You Can Add and Manipulate Windows Graphics on Your Computer Screen with AutoHotkey

In the previous blog, I added a calibration method to an on-screen ruler for extracting distances from any image. It works well for capturing straight-line measurement from a computer display. However, when following the mouse cursor, it lacked a discernible tracking line between the start and stop points.

The green line anchors at the start point and moves with the mouse cursor.

With a search, I found an old post about how to generate a line on-screen. I copied the code and turned it into a function for displaying the green line shown in the image above.

An AutoHotkey GUI Trick

I don’t know who first developed the technique, but this line generating function uses a GUI trick to draw the line (or other graphics) on your computer screen. First, the script sets up an invisible, always-on-top GUI covering the entire screen. Next, AutoHotkey sets that GUI to transparent—both unseen and click-through. Then, using Windows DLLs, the routine draws graphics on that transparent GUI. (Go to blog…)

Replace Hotkeys with the AutoHotkey GetKeyState() Function

While Digging into On-Screen Graphic Line Generation, I Discovered a Number of New AutoHotkey Techniques to Add to the MouseMeasure.ahk Distance Capturing Script

Last time, I looked into adding a line drawn on-screen to visually represent a linear measurement (“Drawing Lines on Screens with AutoHotkey“). However, I didn’t feel that the drivers I used presented the result I wanted for the MouseMeasure.ahk script. The line jittered too erratically and I found holding down the left-mouse button while dragging awkward and lacking precision. Therefore, I didn’t post the final product—although I did provide a download for the curious.

I have since drawn upon the expertise of other AutoHotkey Forum users to revise the script and create a much more robust app. The new script includes the following improvements:

  1. Only one Hotkey combination (Ctrl+LButton) activates both the calibration and measuring subroutine.
  2. The script no longer requires holding down the left mouse button while positioning the end point of the measurement. The end-point remains attached to the moving mouse cursor.
  3. The more advanced GDIPlus graphics used to draw the line make the line smoother and more robust.
The MouseMeasure.ahk script now uses Windows GDIPlus graphics to draw a red line between the start and end points of the on-screen ruler. The end point of the line moves with the mouse cursor until pinned with either the Shift or Alt key.

I pulled these improvements from a number of sources. (Go to blog…)

How to Draw Lines with AutoHotkey Using Windows GDIPlus Graphics

After Laying an Invisible Graphical User Interface (GUI) over Your Computer Screen, You Can Use Windows GDIPlus to Draw Smooth Lines and Shapes

In my last blog (“Replace Hotkeys with the AutoHotkey GetKeyState() Function“), I explained a Hotstring replacement trick as well as introducing GDIPlus graphics drawing functions from an AutoHotkey post by Hellbent. After comparing the functions in the GDIP_All.ahk library with those from Hellbent, I noted that while many were identical, Hellbent added a few more functions which seemed to make the process easier. Although, I haven’t analyzed the details of each function, I have made them work in the MouseMeasure.ahk script. Here I offer an overview to guide you in how to add an invisible GDIPlus graphics drawing overlay to your computer screen.

Since these GDIPlus functions (included in the script) contain the individual DLLCall() functions, they make it easier to implement on-screen graphics without understanding each enigmatic DLL call. A general understanding of how the MouseMeasure.ahk script works with GDIPlus will help you to implement your own AutoHotkey graphics. (Go to blog…)

Pass-Through Hotkey Combinations to Prevent Shortcut Blocking (AutoHotkey Tip)

Sometimes You Want AutoHotkey Hotkeys to Block Other Program Shortcuts While Other Times You Want Both to Work

One of the effects of AutoHotkey Hotkeys includes blocking action for key combinations in Windows and other programs. While often desirable, occasionally you want both the Hotkey and the program shortcut to work. To do this we put a squiggle (tilde prefix ~) in front of the Hotkey combination.

In the MouseMeasure.ahk app, use the cursor keys (RightLeftUpDown) to move the mouse cursor one pixel at a time for accurate placement of both ends of the graphic measurement line. Delete to clear the line and ToolTip.

When I added the Arrow keys to the MouseMeasure.ahk script as Hotkeys for precisely locating the mouse cursor onscreen, it triggered the undesirable side effect of blocking the text cursor movement associated with those same keys in editing screens. By placing a tilde (~) in front of each Hotkey, I can accomplish both accurate mouse cursor placement in the invisible GUI and continue using text cursor movement in an editing window without disabling or closing the MouseMeasure.ahk app. (See “Replace Hotkeys with the AutoHotkey GetKeyState() Function” for an introduction to the GDIPlus version of the script. See “How to Draw Lines with AutoHotkey Using Windows GDIPlus Graphics” for information on the GDIPlus functions used in the script.) (Go to blog…)

Measure Multiple Line Segments with an AutoHotkey On-Screen Ruler

Taking the MouseMeasure.ahk Script to the Next Level, We Add Multiple Calculations for Going Around Corners

The original MouseMeasure.ahk script captures a single-length in a straight line—as a crow flies. While this works great for many applications, roads and highways generally wind over travel distances. Depending upon where you’re going, this can cause a significant variation in the total calculation. To return a more accurate overall estimate, we must break the measurement line into shorter segments.

Start the measurement with the Ctrl+LButton Hotkey, then click the left mouse button for each new leg of the journey. Press the Shift key to terminate the last leg and display the total distance.

The original form of the MouseMeasure.ahk script only allows for a sole straight line. To add more legs to our journey (at different angles), we must implement AutoHotkey techniques for:

  1. Terminating one segment and starting a new one.
  2. Tracking the position of each segment, its length, and the total distance traveled.
  3. Refreshing the screen to include all past legs as well as the new leg.
  4. Sending multiple saved data items for each leg to documents and forms.

You’ll find these changes in the new MouseMeasureMultiLine.ahk script. There’s a lot to unpack in these improvements and I plan to continue discussing the rationale and details of these techniques in upcoming blogs. (Go to blog…)

Track Graphic Line Measurement Segments Using AutoHotkey Arrays

When Refreshing the MouseMeasure.ahk Invisible GUI Graphics Layer, AutoHotkey Uses a Simple Array of Associative Arrays to Track the Data

In my last blog (“Measure Multiple Line Segments with an AutoHotkey On-Screen Ruler“), I introduced multi-segment lines for estimating distances of non-linear routes. When refreshing the graphics to animate the moving line, all previously fixed segments need redrawing. Objects-based arrays provide the best method for tracking and regenerating these lines.

Each leg of the journey corresponds to a simple array element containing an associative array of data. The white box displays the key:value data saved in MyArray[4].

The difference between pseudo-arrayssimple arrays, and associative arrays can get confusing. For the novice AutoHotkey scriptwriter, unfamiliar Object-Oriented Programming (OOP) notation can make understanding the code even more difficult—especially if you attempt to learn OOP from online documentation. (Go to blog…)

Sending Multiple Saved Data Items to Documents and Forms in AutoHotkey (Temporary Hotkeys)

Sometimes We Want Single-key Hotkeys to Work Only for Short Periods of Time

The press of a single key provides the easiest method for inserting data into an edit field or document. In AutoHotkey, you can activate any key as that quick action single-key with either a Hotkey or Hotstring. However, in the normal course of work, that technique renders that keyboard action useless for anything else. To get the convenience of one-key instant activation, we must activate that Hotkey when needed—only in specific circumstances and for short periods of time.

AutoHotkey offers a number of different methods for accomplishing this instant key action. Which we choose depends upon what we want to do. In this blog, I look at three different methods:

  1. Use the Hotkey command to temporarily turn Hotkeys on then off again.
  2. Use the #If directive to designate conditional Hotkeys.
  3. Temporarily pause the script after Hotkey activation, then deactivate Hotkeys upon resuming.

While each temporary Hotkey works in a similar manner—activating only when needed—each has its advantages and disadvantages. (Go to blog…)