How to draw a simple filled rectangle, no frills

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
downstairs
Posts: 42
Joined: 17 Aug 2017, 11:42

How to draw a simple filled rectangle, no frills

26 Nov 2020, 11:53

I'm looking to draw a filled rectangle (black) onto the screen to cover up some things. Is there a very simple way to draw a filled rectangle with no frills, no other GUI elements? It would need to be:

1. Always on top
2. Be able to also be destroyed

I see so many complex GUI questions and ideas on this forum, but I'm looking for pretty much the simplest thing.
User avatar
mikeyww
Posts: 26944
Joined: 09 Sep 2014, 18:38

Re: How to draw a simple filled rectangle, no frills

26 Nov 2020, 12:28

Code: Select all

/* Draw a filled rectangle -----------------------------
https://www.autohotkey.com/boards/viewtopic.php?f=76&t=83702&p=366650#p366650
https://github.com/tariqporter/Gdip/blob/master/Gdip.Tutorial.2-Draw.Outlined.Shapes.ahk
https://jacks-autohotkey-blog.com/2020/08/31/how-to-draw-lines-with-autohotkey-using-windows-gdiplus-graphic/
https://autohotkey.com/board/topic/29197-drawing-a-rectangle/
--------------------------------------------------------
*/
#Include q:\vis2\lib\Gdip_All.ahk ; https://autohotkey.com/board/topic/29449-gdi-standard-library-145-by-tic/
OnExit, Exit

; Set rectangle's dimensions, transparency, and color
x := 250, y := 80, w := 300, h := 200    ; Dimensions of rectangle
color := 0xff000000                      ; Transparency (FF = solid) + RGB (black in this case = 000000)
canvasWidth := 600, canvasHeight := 400  ; Entire drawing area

; Set up the GUI
Gui, 1: -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs ; Layered window
Gui, 1: Show, NA
hwnd1 := WinExist() ; The created window's handle

; Set up the bitmap
pToken := Gdip_Startup()
hbm    := CreateDIBSection(canvasWidth, canvasHeight)  ; The GDI bitmap
hdc    := CreateCompatibleDC()                         ; Device context compatible with screen
obm    := SelectObject(hdc, hbm)                       ; Select bitmap into device context
G      := Gdip_GraphicsFromHDC(hdc)                    ; Get pointer to graphics of bitmap
Gdip_SetSmoothingMode(G, 4)                            ; Antialias mode to smooth the shapes

; Painting tools
pPen   := Gdip_CreatePen(color, 10)
hBrush := Gdip_BrushCreateSolid(color) ; For the fill

; Paint the rectangle = Draw outline + fill
Gdip_DrawRectangle(G, pPen,   x, y, w, h)
Gdip_FillRectangle(G, hBrush, x, y, w, h)
UpdateLayeredWindow(hwnd1, hdc, 0, 0, canvasWidth, canvasHeight)

; Clean up the paint
Gdip_DeletePen(pPen), Gdip_DeleteBrush(hBrush)
SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc), Gdip_DeleteGraphics(G)
Return

Esc::
ExitApp

Exit:
Gdip_Shutdown(pToken)
ExitApp
Return
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: How to draw a simple filled rectangle, no frills

26 Nov 2020, 12:52

You can’t get more simple than a text control with a colored background. Or maybe using a progress control. Then just take away the caption bar
User avatar
mikeyww
Posts: 26944
Joined: 09 Sep 2014, 18:38

Re: How to draw a simple filled rectangle, no frills

26 Nov 2020, 13:01

Absolutely, that works as well.

Code: Select all

Gui, Color, Black
Gui, +AlwaysOnTop -Caption +ToolWindow
Gui, Show, x100 y100 w200 h75
Return

GuiEscape:
GuiClose:
ExitApp
User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: How to draw a simple filled rectangle, no frills

26 Nov 2020, 13:09

Made with easy-to-use functions:

Code: Select all

br1 := BlackRect(20, 20, 100, 100)
Sleep, 1000
br2 := BlackRect(80, 200, 200, 50)
Sleep, 2000
DestroyBR(br1)
Sleep, 2000
DestroyBR(br2)
ExitApp
return

BlackRect(x, y, w, h) {
	static n := 0
	n++
	Gui, %n%:-Caption +AlwaysOnTop +ToolWindow
	Gui, %n%:Color, Black
	Gui, %n%:Show, x%x% y%y% w%w% h%h%
	return n
}

DestroyBR(n) {
	Gui, %n%:Destroy
}
Last edited by boiler on 26 Nov 2020, 13:10, edited 1 time in total.
downstairs
Posts: 42
Joined: 17 Aug 2017, 11:42

Re: How to draw a simple filled rectangle, no frills

26 Nov 2020, 13:10

mikeyww wrote:
26 Nov 2020, 13:01
Absolutely, that works as well.

Code: Select all

Gui, Color, Black
Gui, +AlwaysOnTop -Caption +ToolWindow
Gui, Show, x100 y100 w200 h75
Return

GuiEscape:
GuiClose:
ExitApp
This is perfect! I'm curious how I can add the creation and (most importantly) the destruction of this element to a continually running script that itself won't close. I can't really have the ExitApp thing in there?

Otherwise this is perfect, I'll play around with it as an addition to my running script.
User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: How to draw a simple filled rectangle, no frills

26 Nov 2020, 13:18

downstairs wrote:
26 Nov 2020, 13:10
I'm curious how I can add the creation and (most importantly) the destruction of this element to a continually running script that itself won't close.
The demo of the functions I posted show how you can create and destroy them as you wish.
downstairs
Posts: 42
Joined: 17 Aug 2017, 11:42

Re: How to draw a simple filled rectangle, no frills

26 Nov 2020, 13:25

boiler wrote:
26 Nov 2020, 13:18
downstairs wrote:
26 Nov 2020, 13:10
I'm curious how I can add the creation and (most importantly) the destruction of this element to a continually running script that itself won't close.
The demo of the functions I posted show how you can create and destroy them as you wish.
Yes, you posted that just as I asked this question... this works great! Thank you! So simple!
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to draw a simple filled rectangle, no frills

26 Nov 2020, 18:01

boiler wrote:

Code: Select all

BlackRect(x, y, w, h) {
	static n := 0
	n++
What if Gui, 1: is already created? :)
User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: How to draw a simple filled rectangle, no frills

26 Nov 2020, 18:51

Code: Select all

br1 := BlackRect(20, 20, 100, 100)
Sleep, 1000
br2 := BlackRect(80, 200, 200, 50)
Sleep, 2000
DestroyBR(br1)
Sleep, 2000
DestroyBR(br2)
ExitApp
return

BlackRect(x, y, w, h) {
	static n := 0
	n++
	Gui, br%n%:-Caption +AlwaysOnTop
	Gui, br%n%:Color, Black
	Gui, br%n%:Show, x%x% y%y% w%w% h%h%
	return "br" n
}

DestroyBR(n) {
	Gui, %n%:Destroy
}
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: How to draw a simple filled rectangle, no frills

26 Nov 2020, 19:40

boiler wrote:
26 Nov 2020, 18:51

Code: Select all

br1 := BlackRect(20, 20, 100, 100)
Sleep, 1000
br2 := BlackRect(80, 200, 200, 50)
Sleep, 2000
DestroyBR(br1)
Sleep, 2000
DestroyBR(br2)
ExitApp
return

BlackRect(x, y, w, h) {
	static n := 0
	n++
	Gui, br%n%:-Caption +AlwaysOnTop
	Gui, br%n%:Color, Black
	Gui, br%n%:Show, x%x% y%y% w%w% h%h%
	return "br" n
}

DestroyBR(n) {
	Gui, %n%:Destroy
}
Just return the HWND instead. That can then be used as the “name”.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 301 guests