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.
How to draw a simple filled rectangle, no frills
-
- Posts: 42
- Joined: 17 Aug 2017, 11:42
Re: How to draw a simple filled rectangle, no frills
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
Re: How to draw a simple filled rectangle, no frills
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
Re: How to draw a simple filled rectangle, no frills
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
Re: How to draw a simple filled rectangle, no frills
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.
-
- Posts: 42
- Joined: 17 Aug 2017, 11:42
Re: How to draw a simple filled rectangle, no frills
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?mikeyww wrote: ↑26 Nov 2020, 13:01Absolutely, that works as well.
Code: Select all
Gui, Color, Black Gui, +AlwaysOnTop -Caption +ToolWindow Gui, Show, x100 y100 w200 h75 Return GuiEscape: GuiClose: ExitApp
Otherwise this is perfect, I'll play around with it as an addition to my running script.
Re: How to draw a simple filled rectangle, no frills
The demo of the functions I posted show how you can create and destroy them as you wish.downstairs wrote: ↑26 Nov 2020, 13:10I'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.
-
- Posts: 42
- Joined: 17 Aug 2017, 11:42
Re: How to draw a simple filled rectangle, no frills
Yes, you posted that just as I asked this question... this works great! Thank you! So simple!boiler wrote: ↑26 Nov 2020, 13:18The demo of the functions I posted show how you can create and destroy them as you wish.downstairs wrote: ↑26 Nov 2020, 13:10I'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.
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: How to draw a simple filled rectangle, no frills
What if Gui, 1: is already created?
Re: How to draw a simple filled rectangle, no frills
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
}
Re: How to draw a simple filled rectangle, no frills
Just return the HWND instead. That can then be used as the “name”.boiler wrote: ↑26 Nov 2020, 18:51Code: 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 }
Who is online
Users browsing this forum: No registered users and 301 guests