GDI+ - Create Path to clip rectangle

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Chake
Posts: 2
Joined: 21 Jun 2018, 10:46

GDI+ - Create Path to clip rectangle

21 Jun 2018, 10:59

Hi there,
I want to create a gray overlay with a rectangle inside to highlight whatever is in the rectangle. I've got it working with an eclipse:
OverlayTest.png
OverlayTest.png (160.27 KiB) Viewed 1194 times
But I fail to create a rectangle cut out.

Here's my code:

Code: Select all

#SingleInstance, Force
#NoEnv
SetBatchLines, -1

#include %A_ScriptDir%
#Include, lib\Gdip_All.ahk

If !pToken := Gdip_Startup()
{
	MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
	ExitApp
}
OnExit, Exit

Width :=1400, Height := 1050
Gui, 1: -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs
Gui, 1: Show, NA
hwnd1 := WinExist()
hbm := CreateDIBSection(Width, Height)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)
Gdip_SetSmoothingMode(G, 4)

; Create Overlay
pBrush := Gdip_BrushCreateSolid(0x70c3c3c3)
Gdip_FillRectangle(G, pBrush, 0, 0, 1000, 1000)

; Create clip path
pPath := Gdip_CreatePath(1)
Gdip_AddPathEllipse(pPath, 100, 100, 100, 100)

; Apply clip path
Gdip_SetClipPath(G, pPath, 0)
Gdip_GraphicsClear(G)

; Show
UpdateLayeredWindow(hwnd1, hdc, 0, 0, Width, Height)
SelectObject(hdc, obm)

; Clear
DeleteObject(hbm)
Gdip_DeleteGraphics(G)

Return

ESC::
Exit:
Gdip_Shutdown(pToken)
ExitApp
Return
Can anybody give me a hint how to solve this? I've tried to use Gdip_AddPathPolygon - but am not shure how tu use it correctly. I've used the Gdip.ahl from https://autohotkey.com/boards/viewtopic.php?t=6517
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: GDI+ - Create Path to clip rectangle

21 Jun 2018, 13:44

Gdip_AddPathPolygon(pPath, "100,100 | 100,200 | 200,200 | 200,100")
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: GDI+ - Create Path to clip rectangle

21 Jun 2018, 14:28

Chake wrote:I want to create a gray overlay with a rectangle inside to highlight whatever is in the rectangle.[/code]
It is possible to create a shaded rectangle selection area using just a Gui.

https://autohotkey.com/board/topic/5764 ... electarea/

This is your basic drag to create a colored rectangle selection seen in many programs.

You could use the basic idea in here just to create a predefined colored rectangle if wanted with no dragging to create.

This is quick and simple without a large library, if you don't need the full versitility that GDI+ brings.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
imustbeamoron
Posts: 44
Joined: 18 Aug 2016, 22:56

Re: GDI+ - Create Path to clip rectangle

21 Jun 2018, 20:08

i remember doing something similar to this awhile back. this doesnt require gdip. control s to move the 'view window'. change size/transparency, etc to suite your needs.

Code: Select all

SetBatchLines -1
#NoEnv
#SingleInstance force
CoordMode, mouse, client

windowWidth := A_ScreenWidth /2, windowHeight := A_ScreenHeight
transparency := 192
viewWidth := 200, viewHeight := 100, selectOn := 0

gui, main: -caption +AlwaysOnTop
gui, main: show, % "w" windowWidth " h" windowHeight
hwndWin := WinExist("A")
WinSet, Trans,% transparency, ahk_id %hwndWin%

;create src/dest rgns
srcRgn	:= DllCall("CreateRectRgn", "Int", 0, "Int", 0, "Int", windowWidth, "Int", windowHeight)
destRgn	:= DllCall("CreateRectRgn", "Int", 0, "Int", 0, "Int", windowWidth, "Int", windowHeight)
return

;hold control s to move 'selection window'
^s::
while(GetKeyState("s", "P"))
{
	DllCall("DeleteObject", "Ptr", holeRgn)
	MouseGetPos, mx, my
	holeRgn := DllCall("CreateRectRgn", "Int", mx - viewWidth /2, "Int", my - viewHeight /2, "Int", mx + viewWidth /2, "Int", my + viewHeight /2)
	DllCall("CombineRgn", "Ptr", destRgn, "Ptr", srcRgn, "Ptr", holeRgn, "UInt", RGN_XOR := 3)
	DllCall("SetWindowRgn", "Ptr", hwndWin, "Ptr", destRgn, "Int", 1)
}
return


;cleanup
esc::
DllCall("DeleteObject", "Ptr", srcRgn)
DllCall("DeleteObject", "Ptr", destRgn)
DllCall("DeleteObject", "Ptr", holeRgn)
exitapp
Chake
Posts: 2
Joined: 21 Jun 2018, 10:46

Re: GDI+ - Create Path to clip rectangle

22 Jun 2018, 13:09

Wow, you guys are fast! Thanks alot!

I went with wolf_II's solution which works like a charm.
wolf_II wrote:Gdip_AddPathPolygon(pPath, "100,100 | 100,200 | 200,200 | 200,100")

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, mikeyww, roysubs, teadrinker and 159 guests