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.

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.

This trick creates an unseen AutoHotkey GUI drawing surface on top of the computer screen for displaying and updating Windows graphic tools. After reviewing various on-screen graphics scripts, I found that most used some variation of the following:

Gui, 2:+LastFound +AlwaysOnTop +ToolWindow
Gui, 2:-Caption
Gui, 2:Color, 008000
WinSet, TransColor, 008000
Gui, 2:Show
Gui, 2:Maximize
GuiHwnd := WinExist() ; capture window handle
Gui, 2:Hide

I used the GUI number 2: to differentiate the screen cover GUI from the previously defined calibration GUI. For each respective line number in the snippet above AutoHotkey takes the following actions:

  1. Sets up GUI 2: to sit always-on-top and removes maximize and minimize buttons.
  2. Removes GUI 2: title bar and any window border/edge.
  3. Sets GUI 2: color to #008000.
  4. Makes GUI 2: color #008000 invisible and click-through.
  5. Activates (Gui, 2:Show) invisible GUI screen cover 2:.
  6. Maximizes GUI 2: to cover the entire screen.
  7. Grabs the window handle for use with DrawLine() function described below.
  8. Hides the GUI 2: screen cover until needed.

When added to the MouseMeasure.ahk script, the following example uses this unseen GUI screen cover to draw a line tracking the movement of the mouse cursor.

Call DrawLine Subroutine

To add the tracking line to the MouseMeasure.ahk script, I inserted a second SetTimer command to update the graphic line on any mouse movement (DrawLine):

Ctrl & LButton::
Shift & LButton::
    Gui, 2:Show
    MouseGetPos,sx,sy ; start position for measurement
    SetTimer UpdatePos, 50
    SetTimer, DrawLine, 10

The new SetTimer, DrawLine, 10 statement calls the DrawLine subroutine every 10 milliseconds.

The DrawLine Subroutine

The DrawLine subroutine grabs the current location and redraws the invisible GUI if the new mouse cursor coordinates don’t match their old location:

  MouseGetPos, M_x, M_y
  If (M_x != Old_M_x or M_y != Old_M_y)
    WinSet, Redraw,, ahk_id %GuiHwnd%
  Canvas_DrawLine(GuiHwnd, sx, sy, M_x, M_y, 2, "0x00FF00")
  Old_M_x := M_x
  Old_M_y := M_y

The Canvas_DrawLine() function adds/updates the Windows graphic line in the GUI.

The Canvas_DrawLine() Function

I copied the following function with a few changes:

Canvas_DrawLine(hWnd, p_x1, p_y1, p_x2, p_y2, p_w, p_color)

The parameters include:

  • hWnd — the window handle captured when setting up the cover GUI
  • p_x1 — the starting mouse cursor x coordinate
  • p_y1 — the starting mouse cursor y coordinate
  • p_x2 — the current mouse cursor x coordinate
  • p_y2 — the current mouse cursor y coordinate
  • p_w — the pixel width of the line
  • p_color — the color of the line

The entire function appears as follows:

Canvas_DrawLine(hWnd, p_x1, p_y1, p_x2, p_y2, p_w, p_color)
   p_x1 -= 1, p_y1 -= 1, p_x2 -= 1, p_y2 -= 1
   hDC := DllCall("GetDC", UInt, hWnd)
   hCurrPen := DllCall("CreatePen", UInt, 0, UInt, p_w, UInt, p_color)
   DllCall("SelectObject", UInt,hdc, UInt,hCurrPen)
   DllCall("gdi32.dll\MoveToEx", UInt, hdc, Uint,p_x1, Uint, p_y1, Uint, 0 )
   DllCall("gdi32.dll\LineTo", UInt, hdc, Uint, p_x2, Uint, p_y2 )
   DllCall("ReleaseDC", UInt, 0, UInt, hDC)  ; Clean-up.
   DllCall("DeleteObject", UInt,hCurrPen)

I don’t claim to know what each DllCall() line does, but, most importantly, it works. The green line initiates at the start point for either Shift+Left Mouse Button or Ctrl+Left Mouse Button and terminates at the current mouse cursor location.

I didn’t post a completed script because these changes use the older gdi32.dll graphics library. While it works, the results are a little choppy. If you want to test the code yourself, you can download it here:

I found another similar line generator that offers a cleaner look. It uses the more advanced GDIPlus DLLs and operates a bit more smoothly. The DllCall() functions appear in easier-to-use (and understand) AutoHotkey functions. I hope to figure out how to add this feature to the MouseMeasure.ahk script using these alternative graphics DLLs in the near future—maybe next time.

Implementing Windows graphics with AutoHotkey constitutes a more advanced topic. It takes a while to overcome the learning curve and I’ve only scratched the surface. I’ve seen a number of tricks that I would like to include in the MouseMeasure app. Even then, I’ll only scratch the surface. I plan to highlight any learning points as I continue my exploration. Hopefully, as I begin to understand how the techniques work, they’ll become clearer to you.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)


This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)

Find my AutoHotkey books at ComputorEdge E-Books!

Find quick-start AutoHotkey classes at “Robotic Desktop Automation with AutoHotkey“!

2 thoughts on “Drawing Lines on Screens with AutoHotkey (Graphics Tips)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s