[V2] [Function] Select screen region with mouse

Post your working scripts, libraries and tools.
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

[V2] [Function] Select screen region with mouse

Post by SpeedMaster » 20 Mar 2023, 05:10

SelectScreenRegion(Key, Color, transparency)

The Function: (by FanaticGuru)

Code: Select all

SelectScreenRegion(Key, Color := "Lime", Transparent:= 80)
{
	CoordMode("Mouse", "Screen")
	MouseGetPos(&sX, &sY)
	ssrGui := Gui("+AlwaysOnTop -caption +Border +ToolWindow +LastFound -DPIScale")
	WinSetTransparent(Transparent)
	ssrGui.BackColor := Color
	Loop 
	{
		Sleep 10
		MouseGetPos(&eX, &eY)
		W := Abs(sX - eX), H := Abs(sY - eY)
		X := Min(sX, eX), Y := Min(sY, eY)
		ssrGui.Show("x" X " y" Y " w" W " h" H)
	} Until !GetKeyState(Key, "p")
	ssrGui.Destroy()
	Return { X: X, Y: Y, W: W, H: H, X2: X + W, Y2: Y + H }
}


Example :

Press: Ctrl + Win + Mouse or Press: L+ Mouse to select screen region.

Code: Select all

#Requires AutoHotkey v2

; Select Screen Region with Mouse
^#LButton:: ; Control+Win+Left Mouse to select with Red
{
	Area := SelectScreenRegion("LButton", "Red", 30)
	MsgBox "Top Left:		X " Area.X "`tY " Area.Y "`nBottom Right	X " Area.X2 "`tY " Area.Y2 "`n`nWidth	" Area.W "`tHeight " Area.H
}

L:: ; L to select with defaults
{
	Area := SelectScreenRegion("L")
	MsgBox "Top Left:		X " Area.X "`tY " Area.Y "`nBottom Right	X " Area.X2 "`tY " Area.Y2 "`n`nWidth	" Area.W "`tHeight " Area.H
}

Esc:: ExitApp

SelectScreenRegion(Key, Color := "Lime", Transparent:= 80)
{
	CoordMode("Mouse", "Screen")
	MouseGetPos(&sX, &sY)
	ssrGui := Gui("+AlwaysOnTop -caption +Border +ToolWindow +LastFound -DPIScale")
	WinSetTransparent(Transparent)
	ssrGui.BackColor := Color
	Loop 
	{
		Sleep 10
		MouseGetPos(&eX, &eY)
		W := Abs(sX - eX), H := Abs(sY - eY)
		X := Min(sX, eX), Y := Min(sY, eY)
		ssrGui.Show("x" X " y" Y " w" W " h" H)
	} Until !GetKeyState(Key, "p")
	ssrGui.Destroy()
	Return { X: X, Y: Y, W: W, H: H, X2: X + W, Y2: Y + H }
}



Example without function:
Converted from V1 to V2 by Speedmaster
Spoiler
Regards
Last edited by SpeedMaster on 28 Mar 2023, 18:22, edited 5 times in total.

User avatar
FanaticGuru
Posts: 1905
Joined: 30 Sep 2013, 22:25

Re: [V2] Selcet screen région and save it to clipboard

Post by FanaticGuru » 20 Mar 2023, 23:33

SpeedMaster wrote:
20 Mar 2023, 05:10
Selcet screen région and save it to clipboard (converted from V1)

Need to add -DPIScale to the Gui to have it work for screens at other than 100%.

I have seen this done several different ways and this using of a Gui seems the simplest.

Now you know the next thing people are going to ask for is for you to convert the entire Screen Clipper to v2. :dance:

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

User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: [V2] Selcet screen région and save it to clipboard

Post by SpeedMaster » 21 Mar 2023, 03:05

Thanks for the feedback and suggestion.
FanaticGuru wrote:
20 Mar 2023, 23:33
Now you know the next thing people are going to ask for is for you to convert the entire Screen Clipper to v2
Not for me... I'm still learning the new syntax. :roll:

User avatar
NPerovic
Posts: 30
Joined: 31 Dec 2022, 01:25
Contact:

Re: [V2] Selcet screen région and save it to clipboard

Post by NPerovic » 26 Mar 2023, 10:22

Microsoft: "Win+Shift+s"

elbitjusticiero
Posts: 75
Joined: 06 May 2017, 11:07

Re: [V2] Selcet screen région and save it to clipboard

Post by elbitjusticiero » 28 Mar 2023, 07:17

NPerovic wrote:
26 Mar 2023, 10:22
Microsoft: "Win+Shift+s"
This is not for taking a screenshot. It's for retrieving the coordinates of a screen region, marking it with the mouse.

User avatar
FanaticGuru
Posts: 1905
Joined: 30 Sep 2013, 22:25

Re: [V2] Selcet screen région and save it to clipboard

Post by FanaticGuru » 28 Mar 2023, 12:11

Here is a simplified version as a function:

Code: Select all

#Requires AutoHotkey v2

; Select Screen Region with Mouse
^#LButton:: ; Control+Win+Left Mouse to select with Red
{
	Area := SelectScreenRegion("LButton", "Red", 30)
	MsgBox "Top Left:		X " Area.X "`tY " Area.Y "`nBottom Right	X " Area.X2 "`tY " Area.Y2 "`n`nWidth	" Area.W "`tHeight " Area.H
}

L:: ; L to select with defaults
{
	Area := SelectScreenRegion("L")
	MsgBox "Top Left:		X " Area.X "`tY " Area.Y "`nBottom Right	X " Area.X2 "`tY " Area.Y2 "`n`nWidth	" Area.W "`tHeight " Area.H
}

Esc:: ExitApp

SelectScreenRegion(Key, Color := "Lime", Transparent:= 80)
{
	CoordMode("Mouse", "Screen")
	MouseGetPos(&sX, &sY)
	ssrGui := Gui("+AlwaysOnTop -caption +Border +ToolWindow +LastFound -DPIScale")
	WinSetTransparent(Transparent)
	ssrGui.BackColor := Color
	Loop 
	{
		Sleep 10
		MouseGetPos(&eX, &eY)
		W := Abs(sX - eX), H := Abs(sY - eY)
		X := Min(sX, eX), Y := Min(sY, eY)
		ssrGui.Show("x" X " y" Y " w" W " h" H)
	} Until !GetKeyState(Key, "p")
	ssrGui.Destroy()
	Return { X: X, Y: Y, W: W, H: H, X2: X + W, Y2: Y + H }
}

At some point I would like to do a complete rewrite of Screen Clipper to get familiar with the v2 Gdip. This is my start.

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

User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: [V2] Selcet screen région and save it to clipboard

Post by SpeedMaster » 28 Mar 2023, 18:31

FanaticGuru wrote:
28 Mar 2023, 12:11
Here is a simplified version as a function:
At some point I would like to do a complete rewrite of Screen Clipper to get familiar with the v2 Gdip. This is my start.
FG
It's amazing. :D
Thanks for sharing. :thumbup:
I updated my previous post with your function...
viewtopic.php?p=513351#p513351
Cheers

Spitzi
Posts: 301
Joined: 24 Feb 2022, 03:45

Re: [V2] [Function] Select screen region with mouse

Post by Spitzi » 15 Jun 2023, 01:16

Thank you @FanaticGuru and @SpeedMaster .
Very efficient way to select a screen area. Will implement it into my script. Thank you both!!

Spitzi
Posts: 301
Joined: 24 Feb 2022, 03:45

Re: [V2] [Function] Select screen region with mouse

Post by Spitzi » 15 Jun 2023, 02:54

Hi again. I adapted the function for my needs, maybe it comes in handy for somebody else:

Variations:
  • function can be called and waits until user begins selecting the screen - Tooltip is displayed
  • escape will quit the screen selection process
  • option to add a second transparent gui under the mouse cursor. The application that I use for work changes the cursor position itself (to stay within a part of the screen) - while selecting the screen region, I want to protect the cursor from that - that's what the transparent gui is for
  • option to restrict the selection to a certain screen area

Code: Select all

	#Requires AutoHotkey v2

; Select Screen Region with Mouse
R:: ; R to select with Red
{
	Area := SelectScreenRegion("LButton", "Red", 30)
	MsgBox "Top Left:		X " Area.X "`tY " Area.Y "`nBottom Right	X " Area.X2 "`tY " Area.Y2 "`n`nWidth	" Area.W "`tHeight " Area.H
}

L:: ; L to select with defaults
{
	Area := SelectScreenRegion("L")
	MsgBox "Top Left:		X " Area.X "`tY " Area.Y "`nBottom Right	X " Area.X2 "`tY " Area.Y2 "`n`nWidth	" Area.W "`tHeight " Area.H
}

Q:: ExitApp


	; Idea by FanaticGuru  https://www.autohotkey.com/boards/viewtopic.php?f=83&t=115226
	; Key can be LButton, or M or whatever, e.g:  Area := SelectScreenRegion("LButton", "Lime", 30)
	; Lets the user select a portion of the screen
	; - key: the key to be held while selecting the screen
	; - color: the color the selection rectangle will have
	; - transaprent: 0 is invisible, 255 is fully visible
	; - TTinfo: Tooltip to show before selection begins
	; - cpSize: size of a transparent gui to drag along, that protects the cursor from the underlying app (usefull when selecting something in PACS)
	; - subA: subarea, the selection process is restricted to (Map with x,y,w,h)
	; returns the selected area as a map, or false if escaped
	SelectScreenRegion(Key, Color := "Lime", Transparent:= 80, TTinfo:="Please select screen area. Hit Esc to chancel", cpSize:=0, subA:=false)
	{
		CoordMode("Mouse", "Screen")
		Loop {
			Sleep(20)
			Tooltip(TTinfo)
			if GetKeyState("Esc", "p") {																				; cancel if escape was pressed
				Tooltip()
				return false
			}
			MouseGetPos(&mX, &mY)
			if subA {																									; restrict to subarea, if one is given
				new_mX := (mX < subA.X) ? subA.X : mX																	; new coordinates of mouse in subarea
				new_mX := (mX > subA.X+subA.W) ? subA.X+subA.W : new_mX
				new_mY := (mY < subA.Y) ? subA.Y : mY
				new_mY := (mY > subA.Y+subA.H) ? subA.Y+subA.H : new_mY

				if !((mX == new_mX) && (mY == new_mY)) 																	; set the new coordinates if they are not the same
					MouseMove(new_mX, new_mY)
			}
		} Until GetKeyState(Key, "p")
		ToolTip

		if (cpSize > 0) { 																								; create cursor protection gui if needed
			cpGui := Gui("+AlwaysOnTop -caption +Border +ToolWindow +LastFound -DPIScale", "CursorProtection")
			cpGui.Show("w" cpSize*2 " h" cpSize*2)
			WinSetTransparent(1, cpGui.Hwnd)
		}

		CoordMode("Mouse", "Screen")
		MouseGetPos(&sX, &sY)
		ssrGui := Gui("+AlwaysOnTop -caption +Border +ToolWindow +LastFound -DPIScale")
		WinSetTransparent(Transparent)
		ssrGui.BackColor := Color
		Loop
		{
			Sleep 20
			MouseGetPos(&eX, &eY)
			if (cpSize > 0) {
				WinMove(eX-cpSize,eY-cpSize,cpSize*2,cpSize*2,cpGui.Hwnd)												; moves the cursor protection gui
			}
			W := Abs(sX - eX), H := Abs(sY - eY)
			X := Min(sX, eX), Y := Min(sY, eY)
			ssrGui.Show("x" X " y" Y " w" W " h" H)
		} Until !GetKeyState(Key, "p")
		ssrGui.Destroy()
		try cpGui.Destroy()
		Return { X: X, Y: Y, W: W, H: H, X2: X + W, Y2: Y + H }
	}
Greets Spitzi

aliztori
Posts: 117
Joined: 19 Jul 2022, 12:44

Re: [V2] [Function] Select screen region with mouse

Post by aliztori » 01 Jul 2023, 05:33

@Spitzi
It would be good if we could lock the the Lbutton, so that it does not actually click on the place we want to select.

Spitzi
Posts: 301
Joined: 24 Feb 2022, 03:45

Re: [V2] [Function] Select screen region with mouse

Post by Spitzi » 01 Jul 2023, 15:19

@aliztori, you are right, and we can.... I created a new version of the skript, shorter, and without the transparent gui that was used to take the LButton-Event...
Using #HotIf to intercept LButton if the selection window exists...
Is that what you are looking for?

Code: Select all

#Requires AutoHotkey v2

; Select Screen Region with left button
R:: ; R to select with Red inside a predefined area on screen
{
	; allowSubarea := { X: 100, Y: 100, W: 200, H: 200 }
	allowSubarea := false
	Area := SelectScreenRegion("LButton", "Red", 30, "Please select something inside the area", allowSubarea )
	MsgBox "Top Left:		X " Area.X "`tY " Area.Y "`nBottom Right	X " Area.X2 "`tY " Area.Y2 "`n`nWidth	" Area.W "`tHeight " Area.H
}

L:: ; L to select with defaults, instantly beginning when L is pressed down
{
	Area := SelectScreenRegion("L")
	MsgBox "Top Left:		X " Area.X "`tY " Area.Y "`nBottom Right	X " Area.X2 "`tY " Area.Y2 "`n`nWidth	" Area.W "`tHeight " Area.H
}

T:: ; T to start, L to select
{
	Area := SelectScreenRegion("L")
	MsgBox "Top Left:		X " Area.X "`tY " Area.Y "`nBottom Right	X " Area.X2 "`tY " Area.Y2 "`n`nWidth	" Area.W "`tHeight " Area.H
}

Q:: ExitApp



; this must be the top Hotkey using LButton in the skript , for skript to always work
#HotIf WinExist("SSR-GUI")
	LButton:: {
		; intercepts LButton click, does nothing
	}
#HotIf


; initially by FanaticGuru  https://www.autohotkey.com/boards/viewtopic.php?f=83&t=115226
; Key can be LButton, or M or whatever, e.g:  Area := SelectScreenRegion("LButton", "Lime", 30)
; Lets the user select a portion of the screen
; - key: the key to be held while selecting the screen
; - color: the color the selection rectangle will have
; - transaprent: 0 is invisible, 255 is fully visible
; - TTinfo: Tooltip to show before selection begins
; - subA: subarea, the selection process is restricted to (Map with x,y,w,h), false to turn that feature off
; returns the selected area as a map, or false if escaped
SelectScreenRegion(Key, Color := "Lime", Transparent:= 80, TTinfo:="Please select screen area. Hit Esc to chancel", subA:=false)
{
	local X:=0, Y:=0, W:=0, H:=0, X2:=0, Y2:=0																; set return variables to an initial value
	CoordMode("Mouse", "Screen")

	; 1) create GUI
	ssrGui := Gui("+AlwaysOnTop -caption +Border +ToolWindow +LastFound -DPIScale", "SSR-GUI")
	WinSetTransparent(Transparent)
	ssrGui.BackColor := Color
	ssrGui.Show("x1 y1 w1 h1")

	; 2) show tooltip until selection begins
	Loop {
		Sleep(30)
		Tooltip(TTinfo)
		if GetKeyState("Esc", "p") {
			Tooltip()
			return
		}
		MouseGetPos(&mX, &mY)
		if subA {																									; restrict to subarea, if one is given
			new_mX := (mX < subA.X) ? subA.X : mX																	; new coordinates of mouse in subarea
			new_mX := (mX > subA.X+subA.W) ? subA.X+subA.W : new_mX
			new_mY := (mY < subA.Y) ? subA.Y : mY
			new_mY := (mY > subA.Y+subA.H) ? subA.Y+subA.H : new_mY
			if !((mX == new_mX) && (mY == new_mY)) 																	; set the new coordinates if they are not the same
				MouseMove(new_mX, new_mY)
		}
	} Until GetKeyState(Key, "p")
	ToolTip

	; 3) show gui where mouse is
	CoordMode("Mouse", "Screen")
	MouseGetPos(&sX, &sY)
	Loop
	{
		Sleep 30
		MouseGetPos(&eX, &eY)
		W := Abs(sX - eX), H := Abs(sY - eY)
		X := Min(sX, eX), Y := Min(sY, eY)
		ssrGui.Show("x" X " y" Y " w" W " h" H)
	} Until !GetKeyState(Key, "p")

	; 4) clean up
	ssrGui.Destroy()
	Return { X: X, Y: Y, W: W, H: H, X2: X + W, Y2: Y + H }
}



elbitjusticiero
Posts: 75
Joined: 06 May 2017, 11:07

Re: [V2] [Function] Select screen region with mouse

Post by elbitjusticiero » 10 Aug 2023, 06:56

Pressing Escape while the message box is showing gives me an error (This value of type "String" has no property named "X") but still disables mouse clicks systemwide.

Spitzi
Posts: 301
Joined: 24 Feb 2022, 03:45

Re: [V2] [Function] Select screen region with mouse

Post by Spitzi » 10 Aug 2023, 16:57

@elbitjusticiero , you are right, there were a few issues with the code... this should work better:

Code: Select all

#Requires AutoHotkey v2

; examples how to use

; Select Screen Region with left button
R:: ; R to select with Red inside a predefined area on screen
{
	allowSubarea := { X: 100, Y: 100, W: 200, H: 200 }
	; allowSubarea := false
	if Area := SelectScreenRegion("LButton", "Red", 30, "Please select something inside the area", allowSubarea )
		MsgBox "Top Left:		X " Area.X "`tY " Area.Y "`nBottom Right	X " Area.X2 "`tY " Area.Y2 "`n`nWidth	" Area.W "`tHeight " Area.H
}

L:: ; L to select with defaults, instantly beginning when L is pressed down
{
	if Area := SelectScreenRegion("L")
		MsgBox "Top Left:		X " Area.X "`tY " Area.Y "`nBottom Right	X " Area.X2 "`tY " Area.Y2 "`n`nWidth	" Area.W "`tHeight " Area.H
}

T:: ; T to start, W to select
{
	if Area := SelectScreenRegion("W", , , "Press W to select")
		MsgBox "Top Left:		X " Area.X "`tY " Area.Y "`nBottom Right	X " Area.X2 "`tY " Area.Y2 "`n`nWidth	" Area.W "`tHeight " Area.H
}

X:: ; X to start, LButton to select
{
	if Area := SelectScreenRegion("LButton", "Red", 30, "Please select something with the left mouse Button")
		MsgBox "Top Left:		X " Area.X "`tY " Area.Y "`nBottom Right	X " Area.X2 "`tY " Area.Y2 "`n`nWidth	" Area.W "`tHeight " Area.H
}

Q:: ExitApp



; this must be the top Hotkey using LButton in the skript , for skript to always work
#HotIf WinExist("SSR-GUI")
	LButton:: { ; intercepts LButton click, does nothing exept showing a warning
		ToolTip("Left mouse button`nignored by script", , , 2)
		Sleep(100)
		ToolTip( , , , 2)
	}
#HotIf


; initially by FanaticGuru  https://www.autohotkey.com/boards/viewtopic.php?f=83&t=115226
; Key can be LButton, or M or whatever, e.g:  Area := SelectScreenRegion("LButton", "Lime", 30)
; Lets the user select a portion of the screen
; - key: the key to be held while selecting the screen
; - color: the color the selection rectangle will have
; - transaprent: 0 is invisible, 255 is fully visible
; - TTinfo: Tooltip to show before selection begins
; - subA: subarea, the selection process is restricted to (Map with x,y,w,h), false to turn that feature off
; returns the selected area as a map, or false if escaped
SelectScreenRegion(Key, Color := "Lime", Transparent:= 80, TTinfo:="Please select screen area. Hit Esc to chancel", subA:=false) {
	local X:=0, Y:=0, W:=0, H:=0, X2:=0, Y2:=0																; set return variables to an initial value
	CoordMode("Mouse", "Screen")

	; 0) create Gui first to intercept left mouse button
	ssrGui := Gui("+AlwaysOnTop -caption +Border +ToolWindow +LastFound -DPIScale", "SSR-GUI")
	WinSetTransparent(Transparent)
	ssrGui.BackColor := Color
	ssrGui.Show("x1 y1 w1 h1")

	; 1) show tooltip until selection begins
	Loop {
		Sleep(30)
		Tooltip(TTinfo)
		if GetKeyState("Esc", "p") {
			Tooltip()
			ssrGui.Destroy()
			return false
		}
		MouseGetPos(&mX, &mY)
		if subA {																									; restrict to subarea, if one is given
			new_mX := (mX < subA.X) ? subA.X : mX																	; new coordinates of mouse in subarea
			new_mX := (mX > subA.X+subA.W) ? subA.X+subA.W : new_mX
			new_mY := (mY < subA.Y) ? subA.Y : mY
			new_mY := (mY > subA.Y+subA.H) ? subA.Y+subA.H : new_mY
			if !((mX == new_mX) && (mY == new_mY)) 																	; set the new coordinates if they are not the same
				MouseMove(new_mX, new_mY)
		}
	} Until GetKeyState(Key, "p")
	ToolTip()

	; 2) show gui where mouse is
	MouseGetPos(&sX, &sY)
	Loop
	{
		if GetKeyState("Esc", "p") {
			Tooltip()
			ssrGui.Destroy()
			return false
		}
		Sleep(30)
		MouseGetPos(&eX, &eY)
		W := Abs(sX - eX), H := Abs(sY - eY)
		X := Min(sX, eX), Y := Min(sY, eY)
		ssrGui.Move(X, Y, W, H)
	} Until !GetKeyState(Key, "p")

	; 3) clean up
	ssrGui.Destroy()
	return { X: X, Y: Y, W: W, H: H, X2: X + W, Y2: Y + H }
}


Post Reply

Return to “Scripts and Functions (v2)”