Screen clipping

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: Screen clipping

Post by rommmcek » 27 Dec 2022, 02:17

Added to the code above cases if second monitor is on the left, top or bottom of the first one. Monitors should not be off set i.e.:
- top borders of both monitors should be in one line (for sec. mon. on left or right)
- left borders of both monitors should be in one line (for sec. mon. on top or bottom)

anhnha
Posts: 116
Joined: 08 Aug 2018, 02:46

Re: Screen clipping

Post by anhnha » 27 Dec 2022, 05:48

Added to the code above cases if second monitor is on the left, top or bottom of the first one.
Unfortunately, I couldn't make it work. Do you mean the first If with "the code above"?
This is my monitors' layout. My second monitor is rotated 90 degree so I tried swapping its width and height as well but it doesn't work. It is still offset.
Attachments
image.png
image.png (57.54 KiB) Viewed 2419 times
Last edited by anhnha on 28 Dec 2022, 04:40, edited 2 times in total.

User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: Screen clipping

Post by rommmcek » 27 Dec 2022, 05:57

anhnha wrote:
27 Dec 2022, 05:48
… Do you mean the first If with "the code above"? ...
The second if is for your layout, but you can copy all of them, it shouldn't affect the result.

Show your code!

anhnha
Posts: 116
Joined: 08 Aug 2018, 02:46

Re: Screen clipping

Post by anhnha » 27 Dec 2022, 06:27

I added to this function as mentioned in previous post.
I also tried to swap the width and height of the second monitor but it doesn't work as well.

Code: Select all

dip_BitmapFromScreen(Screen=0, Raster="")
{
	if (Screen = 0)
	{
		Sysget, x, 76
		Sysget, y, 77
		Sysget, w, 78
		Sysget, h, 79
	}
	else if (SubStr(Screen, 1, 5) = "hwnd:")
	{
		Screen := SubStr(Screen, 6)
		if !WinExist( "ahk_id " Screen)
			return -2
		WinGetPos,,, w, h, ahk_id %Screen%
		x := y := 0
		hhdc := GetDCEx(Screen, 3)
	}
	else if (Screen&1 != "")
	{
		Sysget, M, Monitor, %Screen%
		x := MLeft, y := MTop, w := MRight-MLeft, h := MBottom-MTop
	}
	else
	{
		StringSplit, S, Screen, |
		x := S1, y := S2, w := S3, h := S4
	}

	if (x = "") || (y = "") || (w = "") || (h = "")
		return -1

 firstMonScale := 150
 secondMonScale:= 100
 firstMonWidth := 3480
 firstMonHight := 2160 
 secondMonWidth:= 1920
 secondMonHight:= 1080
 if (x>firstMonWidth) {
    x:= round(((x-firstMonWidth)/(firstMonScale/secondMonScale))+firstMonWidth)
    y:= round(y/(firstMonScale/secondMonScale))
 }
 if (x<0 || y<0) {
    x:= round(x/(firstMonScale/secondMonScale))
    y:= round(y/(firstMonScale/secondMonScale))
 }
 if (y>firstMonHight) {
    x:= round(x/(firstMonScale/secondMonScale))
    y:= round(((y-firstMonHight)/(firstMonScale/secondMonScale))+firstMonHight)
 }

	chdc := CreateCompatibleDC(), hbm := CreateDIBSection(w, h, chdc), obm := SelectObject(chdc, hbm), hhdc := hhdc ? hhdc : GetDC()
	BitBlt(chdc, 0, 0, w, h, hhdc, x, y, Raster)
	ReleaseDC(hhdc)

	pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
	SelectObject(chdc, obm), DeleteObject(hbm), DeleteDC(hhdc), DeleteDC(chdc)
	return pBitmap
}

User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: Screen clipping

Post by rommmcek » 27 Dec 2022, 06:34

We should scale width and hight as well! Just a sec...

User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: Screen clipping

Post by rommmcek » 27 Dec 2022, 06:39

I edited the code above again. Try it and if it doesn't work make PrintScreen (after capturing) and post it here.

anhnha
Posts: 116
Joined: 08 Aug 2018, 02:46

Re: Screen clipping

Post by anhnha » 27 Dec 2022, 07:25

It doesn't work.
Here is the image that I recorded.
https://gifyu.com/image/SkXJ2

User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: Screen clipping

Post by rommmcek » 27 Dec 2022, 09:07

Thanks for your patience!
As much as I can estimate from your gif, scaling of the dimensions is correct especially x and y coordinats, but capture itself is too small so we have to enlarge it. I'll try to do it tomorrow!

bye for now!

anhnha
Posts: 116
Joined: 08 Aug 2018, 02:46

Re: Screen clipping

Post by anhnha » 27 Dec 2022, 09:17

I should be thanking you instead.
The x and y coordinates are correct but the region is expanding to the left and bottom so the capture region is larger than needed.
I tried it for both small and large capture regions and they're expanding the same way.

User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: Screen clipping

Post by rommmcek » 27 Dec 2022, 10:01

From now on this cannot be fixed by coordinats. It is internal Gdi+ issue.

User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: Screen clipping

Post by rommmcek » 27 Dec 2022, 11:54

I edited the code above again. Try it and if it doesn't work pleas make gif again and post it here.

anhnha
Posts: 116
Joined: 08 Aug 2018, 02:46

Re: Screen clipping

Post by anhnha » 28 Dec 2022, 02:53

The capture region was bigger than drawn region last time but now it's smaller.
https://gifyu.com/image/SvS7T

User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: Screen clipping

Post by rommmcek » 28 Dec 2022, 03:26

Try to replace hbm := CreateDIBSection(w, h, chdc) with hbm := CreateDIBSection(ws, hs, chdc).

anhnha
Posts: 116
Joined: 08 Aug 2018, 02:46

Re: Screen clipping

Post by anhnha » 28 Dec 2022, 04:37

rommmcek wrote:
28 Dec 2022, 03:26
Try to replace hbm := CreateDIBSection(w, h, chdc) with hbm := CreateDIBSection(ws, hs, chdc).
Perfect, it works now! Would this still work if the second monitor is on the right, top, or bottom of the first monitor (just for possible case in the future)?

User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: Screen clipping

Post by rommmcek » 28 Dec 2022, 05:11

Good news and thanks we finally did it, although partially hardcoded (first monitor size & scales)!
anhnha wrote:
28 Dec 2022, 04:37
Would this still work if the second monitor is on the right, top, or bottom of the first monitor (just for possible case in the future)?
rommmcek wrote:
27 Dec 2022, 02:17
Added to the code above cases if second monitor is on the left, top or bottom of the first one. Monitors should not be off set i.e.:
- top borders of both monitors should be in one horizontal line (for sec. mon. on left or right)
- left borders of both monitors should be in one vertical line (for sec. mon. on top or bottom)
So, yes it should work under listed conditions, however not tested (that's why it took so long - didn't have testing opportunity). Remains to test if it would work for e.g.: firstMonScale:= 150, secondMonScale:= 200

P.s.: It would be interesting to see the result if capturing rectangle would extend through border of both monitors!

anhnha
Posts: 116
Joined: 08 Aug 2018, 02:46

Re: Screen clipping

Post by anhnha » 28 Dec 2022, 05:34

Remains to test if it would work for e.g.: firstMonScale:= 150, secondMonScale:= 200
I tried firstMonScale:= 150, secondMonScale:= 100 and firstMonScale:= 150, secondMonScale:= 125 and both worked well.
I don't see option to set to 200 in my monitor setting.
P.s.: It would be interesting to see the result if capturing rectangle would extend through border of both monitors!
It does but doesn't seem to work properly in my monitors as I can't draw rectagle region across both monitors with same height.

User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: Screen clipping

Post by rommmcek » 28 Dec 2022, 05:59

anhnha wrote:
28 Dec 2022, 05:34
I don't see option to set to 200 in my monitor setting.
Never mind, if somebody encounters problem will post it or even fix it!
anhnha wrote:
28 Dec 2022, 05:34
It does but doesn't seem to work properly in my monitors as I can't draw rectagle region across both monitors with same height.
Yeh, I can imagine. To fix it would be to much hassle and little gain.

bye and Happy New Year!

anhnha
Posts: 116
Joined: 08 Aug 2018, 02:46

Re: Screen clipping

Post by anhnha » 28 Dec 2022, 08:11

Thanks for the help. I was happy that setting both monitors with the same scale works but now I can set it any scale I want.

User avatar
Gorgrak
Posts: 23
Joined: 07 Dec 2015, 16:34

Re: Screen clipping

Post by Gorgrak » 29 Dec 2022, 02:14

SpeedMaster wrote:
21 Nov 2021, 09:44
I added a new feature to hide or show the borders. 8-)
Thanks for this!! I have condensed your code a bit to make the hotkey section more readable and only use a single hotkey!!:

Code: Select all

; use the following code instead of SpeedMaster's code
; insert this line inside the #IfWinActive section
^b::(Btoggle:= !Btoggle) ? SpeedMaster_RemoveBorders() : SpeedMaster_RestoreAllClipWinsBorders() ; fancy toggle to turn off and on the clipping's border

; insert the following functions somewhere in the function section
SpeedMaster_RemoveBorders() {
	WinGet, Hwnd, ID, A
	WinGet, Style, Style, ahk_id %Hwnd%
	if (Style & 0x2000)
	{
		WinSet, Style, -0x2000, A
		WinSet, Region, , A 
	}
	else
	{
		WinGetPos, X, Y, W, H,  ahk_id %Hwnd%
		W -= 6, H -= 6
		WinSet, Style, +0x2000, A
		WinSet, Region, 3-3 W%W% H%H%, A
	}	
} ; contributed by SpeedMaster @ https://www.autohotkey.com/boards/viewtopic.php?f=6&t=12088&start=180#p430673

SpeedMaster_RestoreAllClipWinsBorders() {
	MaxGuis:= SCW_Reg("MaxGuis"), StartAfter:= SCW_Reg("StartAfter")
	Loop, %MaxGuis%
	{
		StartAfter++
		Gui %StartAfter%: +lastfound
		WinSet, Region,
	}
} ; contributed by SpeedMaster @ https://www.autohotkey.com/boards/viewtopic.php?f=6&t=12088&start=180#p430673


one1tick
Posts: 32
Joined: 04 Mar 2022, 04:26

Re: Screen clipping

Post by one1tick » 27 Jan 2023, 11:01

anhnha wrote:
28 Dec 2022, 08:11
Thanks for the help. I was happy that setting both monitors with the same scale works but now I can set it any scale I want.
Hi @anhnha, have the same issue you had... Try to follow all you posts but impossible, I am messed up. Could you summarize what you exactly change in your code ?
THanks a lot for help.

Post Reply

Return to “Scripts and Functions (v1)”