PixelChecksum Coordinates.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Fulminare
Posts: 369
Joined: 26 Jun 2021, 20:15

Re: PixelChecksum Coordinates.

16 Sep 2021, 23:16

The x y w h values must be entered in the script right ?

So that the search area is defined.

What does the message box do ?

The confusion started from this point

Code: Select all

^d::
ChkSum := PixelChecksum( 0,0,32,32 )

While % ( ChkSum = PixelChecksum( 0,0,32,32 ) )
   Sleep, 100
MsgBox, Screen Region Change Detected!



PixelCheckSum( X, Y, W, H, Title := "" ) { ;               By SKAN,   http://goo.gl/X5dfvn
;                                                          CD:08/Jun/2009 | MD:01/Sep/2014

  Static DIBSECTION, SRCCOPY := 0x00CC0020, CAPTUREBLT := 0x40000000, BPP := 32
  Local hWnd, hDC, mDC, hBM, oBM, nSize, pBITMAPINFOHEADER, ppvBits := 0
  
  If not VarSetCapacity( DIBSECTION )
         VarSetCapacity( DIBSECTION, 104, 0 ) 
  
  pBITMAPINFOHEADER := &DIBSECTION + ( A_PtrSize = 4 ? 24 : 32 )
  
  NumPut(  40, pBITMAPINFOHEADER +  0, "UInt"   ) 
  NumPut(   W, pBITMAPINFOHEADER +  4, "UInt"   )
  NumPut(   H, pBITMAPINFOHEADER +  8, "UInt"   )
  NumPut(   1, pBITMAPINFOHEADER + 12, "UShort" )
  NumPut( BPP, pBITMAPINFOHEADER + 14, "UShort" )

  hWnd  := WinExist( Title )
  hDC   := DllCall( "GetDC", "Ptr",hWnd, "UPtr" )
  mDC   := DllCall( "CreateCompatibleDC", UInt,hDC, "UPtr" )

  hBM   := DllCall( "CreateDIBSection", "Ptr", mDC, "Ptr", pBITMAPINFOHEADER, "Int",0
                                      , "PtrP",ppvBits, "Ptr",0, "Int",0, "UPtr" )
  
  oBM   := DllCall( "SelectObject", "Ptr",mDC, "Ptr",hBM, "UPtr" )
  
  DllCall( "BitBlt", "Ptr",mDC, "Int",0, "Int",0, "Int",W, "Int",H, "Ptr",hDC
                   , "Int",X, "Int",Y, "UInt",SRCCOPY | CAPTUREBLT )

  DllCall( "SelectObject", "Ptr",mDC, "Ptr",oBM )
  DllCall( "DeleteDC",  "Ptr",mDC )
  DllCall( "ReleaseDC", "Ptr",hWnd, "Ptr",hDC )

  DllCall( "GetObject", "Ptr",hBM, "Int",( A_PtrSize = 4 ? 84 : 104 ), "Ptr",&DIBSECTION )
  nSize := NumGet( pBITMAPINFOHEADER + 20, "UInt" ) 
  
Return DllCall( "NTDLL\RtlComputeCrc32", "UInt",0, "Ptr",ppvBits, "UInt",nSize, "UInt" )
     , DllCall( "DeleteObject", "Ptr",hBM )    
}
That script shows the following message box (ref img 1)


So I thought, the message box was just an action that gets triggered when screen change is detected, and it can be replaced with any other action of my choice.

Now in this script when screen change is detected,

Code: Select all

#NoEnv
#SingleInstance, Force
SetBatchLines, -1
CoordMode, Mouse, Screen

;note: 'CoordMode, Mouse, Screen' must be used in the auto-execute section

#LButton::
InputRect(vX1, vY1, vX2, vY2)
vW := vX2-vX1, vH := vY2-vY1
if (vInputRectState = -1)
	return
sleep, 200
ScrCmp(vX1, vY1, vW, vH)
MsgBox,% "Screen Region Change Detected!`nX: " A_Args.ScrCmp.CX " Y: " A_Args.ScrCmp.CY
Return
;==================================================
;thx jeeswg
;https://www.autohotkey.com/boards/viewtopic.php?t=42810

;based on LetUserSelectRect by Lexikos:
;LetUserSelectRect - select a portion of the screen - Scripts and Functions - AutoHotkey Community
;https://autohotkey.com/board/topic/45921-letuserselectrect-select-a-portion-of-the-screen/

;note: 'CoordMode, Mouse, Screen' must be used in the auto-execute section

;e.g.
;InputRect(vWinX, vWinY, vWinR, vWinB)
;vWinW := vWinR-vWinX, vWinH := vWinB-vWinY
;if (vInputRectState = -1)
;	return

InputRect(ByRef vX1, ByRef vY1, ByRef vX2, ByRef vY2)
{
	global vInputRectState := 0
	DetectHiddenWindows, On
	Gui, 1: -Caption +ToolWindow +AlwaysOnTop +hWndhGuiSel
	Gui, 1: Color, Red
	WinSet, Transparent, 128, % "ahk_id " hGuiSel
	Hotkey, *LButton, InputRect_Return, On
	Hotkey, *RButton, InputRect_End, On
	Hotkey, Esc, InputRect_End, On
	KeyWait, LButton, D
	MouseGetPos, vX0, vY0
	SetTimer, InputRect_Update, 10
	KeyWait, LButton
	Hotkey, *LButton, Off
	Hotkey, Esc, InputRect_End, Off
	SetTimer, InputRect_Update, Off
	Gui, 1: Destroy
	return

	InputRect_Update:
	if !vInputRectState
	{
		MouseGetPos, vX, vY
		(vX < vX0) ? (vX1 := vX, vX2 := vX0) : (vX1 := vX0, vX2 := vX)
		(vY < vY0) ? (vY1 := vY, vY2 := vY0) : (vY1 := vY0, vY2 := vY)
		Gui, 1:Show, % "NA x" vX1 " y" vY1 " w" (vX2-vX1) " h" (vY2-vY1)
		return
	}
	vInputRectState := 1

	InputRect_End:
	if !vInputRectState
		vInputRectState := -1
	Hotkey, *LButton, Off
	Hotkey, *RButton, Off
	Hotkey, Esc, Off
	SetTimer, InputRect_Update, Off
	Gui, 1: Destroy

	InputRect_Return:
	return
}

;==================================================

ScrCmp( X, Y, W, H, Hwnd:=0x0, Sleep* )  {                                        ; v0.66 By SKAN on D3B3/D3B6 @ tiny.cc/scrcmp
Local
Global A_Args
  Sleep[1] := Sleep[1]="" ? 100 : Format("{:d}", Sleep[1])
  Sleep[2] := Sleep[2]="" ? 100 : Format("{:d}", Sleep[2])

  VarSetCapacity(BITMAPINFO, 40, 0)
  NumPut(32, NumPut(1, NumPut(0-H*2, NumPut(W, NumPut(40,BITMAPINFO,"Int"),"Int"),"Int"),"Short"),"Short")

  hBM := DllCall("Gdi32.dll\CreateDIBSection", "Ptr",0, "Ptr",&BITMAPINFO, "Int",0, "PtrP",pBits := 0, "Ptr",0, "Int",0, "Ptr")
  sDC := DllCall("User32.dll\GetDC", "Ptr",(Hwnd := WinExist("ahk_id" . Hwnd)), "Ptr")
  mDC := DllCall("Gdi32.dll\CreateCompatibleDC", "Ptr",sDC, "Ptr")
  DllCall("Gdi32.dll\SaveDC", "Ptr",mDC)
  DllCall("Gdi32.dll\SelectObject", "Ptr",mDC, "Ptr",hBM)
  DllCall("Gdi32.dll\BitBlt", "Ptr",mDC, "Int",0, "Int",H, "Int",W, "Int",H, "Ptr",sDC, "Int",X, "Int",Y, "Int",0x40CC0020)

  A_Args.ScrCmp := {"Wait": 1},   Bytes := W*H*4,  Count := 0
  hMod := DllCall("Kernel32.dll\LoadLibrary", "Str","ntdll.dll", "Ptr")
  While ( A_Args.ScrCmp.Wait && (Count<2) )
  {
      DllCall("Gdi32.dll\BitBlt", "Ptr",mDC, "Int",0, "Int",0, "Int",W, "Int",H, "Ptr",sDC, "Int",X, "Int",Y, "Int",0x40CC0020)
      Count := ( (Byte := DllCall("ntdll.dll\RtlCompareMemory", "Ptr",pBits, "Ptr",pBits+Bytes, "Ptr",Bytes) ) != Bytes )
               ? (Count + 1) : 0
      Sleep % (Count ? Sleep[2] : Sleep[1])
  }   Byte +=1
  DllCall("Kernel32.dll\FreeLibrary", "Ptr",hMod)

  SX := (CX := Mod((Byte-1)//4, W) + X),    SY := (CY := (Byte-1) // (W*4)   + Y)
  If (Hwnd)
    VarsetCapacity(POINT,8,0), NumPut(CX,POINT,"Int"), NumPut(CY,POINT,"Int")
  , DllCall("User32.dll\ClientToScreen", "Ptr",Hwnd, "Ptr",&POINT)
  , SX := NumGet(POINT,0,"Int"),  SY := NumGet(POINT,4,"Int")

  If (Wait := A_Args.ScrCmp.Wait)
      A_Args.ScrCmp := { "Wait":0, "CX":CX, "CY":CY, "SX":SX, "SY":SY }
  DllCall("Gdi32.dll\RestoreDC", "Ptr",mDC, "Int",-1)
  DllCall("Gdi32.dll\DeleteDC", "Ptr",mDC)
  DllCall("User32.dll\ReleaseDC", "Ptr",Hwnd, "Ptr",sDC)
  DllCall("Gdi32.dll\DeleteObject", "Ptr",hBM)
Return ( !!Wait )
}


A. a message box is shown (which is the "action" when screen change is detected)


B. It also contains x y info. (ref img 2)


Because the script I use for getting coordinates also shows it in a message box

Code: Select all

!c::
MouseGetPos, xpos, ypos 
MsgBox, %xpos% %ypos%.
when the message box showed the coordinates (ref img 2) I completely got confused.

I thought the x y info shown in the msg box from img2 should be entered somewhere in the script
Attachments
Screenshot 2021-09-17 093706.png
img 2
Screenshot 2021-09-17 093706.png (3.74 KiB) Viewed 215 times
Screen change detected.png
img 1
Screen change detected.png (2.75 KiB) Viewed 215 times
User avatar
boiler
Posts: 16965
Joined: 21 Dec 2014, 02:44

Re: PixelChecksum Coordinates.

16 Sep 2021, 23:40

No, MsgBox doesn’t get triggered by the screen change being detected. MsgBox is just a mechanism for displaying information, and in your example it just happened to be used to display the notice that the script detected a change. The MsgBox has nothing to do with detecting a change or doing anything else other than showing a small window with the text you tell it to show.

I’m not sure how to answer your question about the x y w h values being entered in the script. They must of course get their values somehow. I think trying to troubleshoot scripts of this complexity is above what you’re ready for. It’s difficult to help until you have a better grasp of AHK and programming concepts in general. Perhaps others can walk you through it, though.

I again suggest working through the documentation and especially the beginner’s tutorial and slowly create some small scripts based on the examples without moving on until you understand every detail of a script you created. Otherwise, we just aren’t sharing the same context to have a meaningful discussion.
Fulminare
Posts: 369
Joined: 26 Jun 2021, 20:15

Re: PixelChecksum Coordinates.

16 Sep 2021, 23:44

exactly. most of them are above my understanding.

I usually understand them with trial and error

Some I can manage to adapt to my needs.

If I can't, I try to use some alternative method( which can be long and twisted, but it gets the job done)

That will give me some time to read and understand the documentation.
Last edited by Fulminare on 16 Sep 2021, 23:51, edited 1 time in total.
Fulminare
Posts: 369
Joined: 26 Jun 2021, 20:15

Re: PixelChecksum Coordinates.

16 Sep 2021, 23:45

boiler wrote:
16 Sep 2021, 23:40
No, MsgBox doesn’t get triggered by the screen change being detected. MsgBox is just a mechanism for displaying information, and in your example it just happened to be used to display the notice that the script detected a change. The MsgBox has nothing to do with detecting a change or doing anything else other than showing a small window with the text you tell it to show.

I’m not sure how to answer your question about the x y w h values being entered in the script. They must of course get their values somehow. I think trying to troubleshoot scripts of this complexity is above what you’re ready for. It’s difficult to help until you have a better grasp of AHK and programming concepts in general. Perhaps others can walk you through it, though.

I again suggest working through the documentation and especially the beginner’s tutorial and slowly create some small scripts based on the examples without moving on until you understand every detail of a script you created. Otherwise, we just aren’t sharing the same context to have a meaningful discussion.

Thank you for clarifying.

I will give this another shot

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 185 guests