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.

Continue reading

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.

Continue reading

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 blog “Capturing Computer Screen Measurements (An AutoHotkey Tool)“, 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.

Continue reading

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 last time), 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).

Continue reading

Capturing Computer Screen Measurements (An AutoHotkey Tool)

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

Recently, a reader asked, “Do you think it is conceivable to create a screen ruler in AHK that can be calibrated to my native application ruler. The problem I have now is that I take tons of measurements off the screen and then I have to type that number back into a document. I would love to make a ruler that can basically calibrate with the native app ruler once and make the data from the AHK ruler transfer automatically to the clipboard or better yet straight to the document.”

I responded, “The application you’re looking at is quite conceivable. 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. It is certainly within the realm of possibilities.”

I then searched the AutoHotkey board only to find that he had already posted the same Ruler question in the “Ask for Help” forum. Fortunately, AutoHotkey Forum user colt had already posted a response. With the heart of the work completed by colt, I decided to add an onscreen calibration method.

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.

Continue reading