Draw multiple rectangles - This is driving me nuts!

Get help with using AutoHotkey and its commands and hotkeys
Snowy42
Posts: 12
Joined: 03 Jul 2017, 18:32

Draw multiple rectangles - This is driving me nuts!

21 Jan 2020, 19:57

I've sourced some code from another user. I've lost the original post now but respect to whoever it was.

I've tried modifying the code so that it will retain the previous rectangles that get drawn but for the life of me I just cannot get it to work!

There's a few errors that I have to fix if drawing right to left or bottom to top... Just for the purpose of diagnosing this one issue, please attempt drawing from topleft>down/right. Run the script, hold shift, click and drag.

Code: Select all

; Should work with all current AHK versions/builds
#NoEnv
#Persistent
SetBatchLines, -1 	; For speed in general
SetWinDelay, -1   	; For speed of WinMove
BW := 3           	; Border width (and height) in pixels
BC := "Red"       	; Border color
snapgrid:= 8		; Grid size for snapping 
i=1					; Iterator
x1Array := []		; Declaring Arrays for retaining rectangles
x2Array := []
y1Array := []
y2Array := []
xsnap:= A_ScreenWidth/snapgrid	;making the snap relative to the screen size
ysnap:= A_ScreenHeight/snapgrid	;making the snap relative to the screen size
; ______________________________________________________________________________________________________________________
CoordMode, Mouse, Screen
Gui, f:-Caption +ToolWindow +LastFound +AlwaysOnTop
Gui, f:Color, %BC%
Return
; ______________________________________________________________________________________________________________________
Esc::
ExitApp
; ______________________________________________________________________________________________________________________
+LButton::
    MouseGetPos, OriginX, OriginY
	OriginX := Round(OriginX/xsnap,0)*xsnap
	OriginY := Round(OriginY/ysnap,0)*ysnap
    SetTimer, DrawRectangle, 10
Return
; ______________________________________________________________________________________________________________________
+LButton Up::
   SetTimer, DrawRectangle, Off
   x1Array[i] := X1
   x2Array[i] := X2
   y1Array[i] := Y1
   y2Array[i] := Y2
   gosub KeepRect
   i++
   ;Gui, i:Cancel
   ToolTip
Return
; ______________________________________________________________________________________________________________________
DrawRectangle:
   Gui, f:+LastFound
   MouseGetPos, X2, Y2
   X2 := Round(X2/xsnap,0)*xsnap
   Y2 := Round(Y2/ysnap,0)*ysnap
   ; Has the mouse moved?
   If (XO = X2) And (YO = Y2)
      Return
   XO := X2, YO := Y2
   ; Allow dragging to the left of the click point.
   If (X2 < OriginX)
      X1 := X2, X2 := OriginX
   Else
      X1 := OriginX
   ; Allow dragging above the click point.
   If (Y2 < OriginY)
      Y1 := Y2, Y2 := OriginY
   Else
      Y1 := OriginY
   ; Draw the rectangle
   W1 := X2 - X1, H1 := Y2 - Y1
   W2 := W1 - BW, H2 := H1 - BW
   WinSet, Region, 0-0 %W1%-0 %W1%-%H1% 0-%H1% 0-0  %BW%-%BW% %W2%-%BW% %W2%-%H2% %BW%-%H2% %BW%-%BW%
      Gui, f:Show, NA x%X1% y%Y1% w%W1% h%H1%
   SetFormat, float, 0
   WinMove, , , X1, Y1, W1, H1
   ToolTip, %X1% - %Y1% - %X2% - %Y2%
Return

KeepRect:
	Loop % i
	{
		j := A_Index
		ax := x1Array[j]
		ay := y1Array[j]
		if (x2Array[j] > x1Array[j])
			aw := x2Array[j] - x1Array[j]
		else if (x2Array[j] < x1Array[j])
			aw := x1Array[j] - x2Array[j]
		if (y2Array[j] > y1Array[j])
			ah := y2Array[j] - y1Array[j]
		else if (y2Array[j] < y1Array[j])
			ah := y1Array[j] - y2Array[j]
		Gui, j:-Caption +ToolWindow +LastFound +AlwaysOnTop
		Gui, j:Color, %BC%
		Gui, j: Show, NoActivate x%ax% y%ay% w%aw% h%ah%
	}
return
I've narrowed the issue down to the last line of the loop in KeepRect:

Code: Select all

Gui, j: Show, NoActivate x%ax% y%ay% w%aw% h%ah%
For some reason, when this is creating/showing each gui it will get rid of the last and for the life of me I CANNOT figure out why.

The script should be able to draw the rectangle, retain it, then draw more and retain every rectangle.

This idea is going to be built into a much larger project but this one little part I just cannot figure out. Please Help!!
TravisQ
Posts: 21
Joined: 17 May 2015, 23:51

Re: Draw multiple rectangles - This is driving me nuts!

21 Jan 2020, 21:55

Not sure, but this might be what you need or at least a start

Code: Select all

			ah := y1Array[j] - y2Array[j]
		Gui, %j%: new
		Gui, %j%:-Caption +ToolWindow +LastFound +AlwaysOnTop
		Gui, %j%:Color, %BC%
		Gui, %j%: Show, NoActivate x%ax% y%ay% w%aw% h%ah%
Snowy42
Posts: 12
Joined: 03 Jul 2017, 18:32

Re: Draw multiple rectangles - This is driving me nuts!

22 Jan 2020, 15:56

I knew it was something simple lol. The script was making the gui called "j", not numbered... :headwall:

Thanks heaps TravisQ. Hit the nail on the head

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 89 guests