How to get color of cursor in game? Topic is solved

Ask gaming related questions (AHK v1.1 and older)
emanate22
Posts: 52
Joined: 11 May 2021, 00:03

How to get color of cursor in game?  Topic is solved

11 May 2021, 00:24

I try PixelGetColor and imagesearch but they just get the color under cursor not itself. i search past post seems noway to achieve this. Can ahk get cursor color in game? I need this funtion in my script,please tell me if anyway to make this ,thanks

Code: Select all

~+a::
MouseGetPos,x,y,
a := x-3
b := y-3
c := x+3
d := y+3
PixelSearch,Lx,Ly,a,b,c,d,0x4544A0,0,Fast
if ErrorLevel=0 
MouseClick,left,
else
return
[Mod edit: [code][/code] tags added.]
User avatar
Onimuru
Posts: 107
Joined: 08 Sep 2018, 18:35
Contact:

Re: How to get color of cursor in game?

11 May 2021, 05:21

This is how I would do it:

Code: Select all

DllCall("Kernel32\LoadLibrary", "Str", "Gdiplus", "Ptr")

VarSetCapacity(input, 8 + A_PtrSize*2)
	, NumPut(0x1, &input + 0, "UInt")

DllCall("Gdiplus\GdiplusStartup", "Ptr*", pToken := 0, "Ptr", &input, "Ptr", 0, "Int")



VarSetCapacity(info, cbSize := 16 + A_PtrSize)
	, NumPut(cbSize, &info + 0, "UInt")

if (!DllCall("User32\GetCursorInfo", "Ptr", &info, "UInt")) {
	throw (Exception(Format("0x{:X}", A_LastError)))
}

hCursor := NumGet(&info + 8, "Ptr")

if (!hDC := DllCall("Gdi32\CreateCompatibleDC", "Ptr", 0, "Ptr")) {
	throw (Exception(Format("0x{:X}", A_LastError)))
}

;* I'm not sure if you can determine the width and height of a cursor/icon so I just went with 20x20 here:
VarSetCapacity(info, 40)
	, NumPut(40, &info + 0, "UInt"), NumPut(20, &info + 4, "UInt"), NumPut(-20, &info + 8, "UInt"), NumPut(1, &info + 12, "UShort"), NumPut(32, &info + 14, "UShort"), NumPut(0, &info + 16, "UInt")

if (!hBitmap := DllCall("Gdi32\CreateDIBSection", "Ptr", hDC, "Ptr", &info, "UInt", 0, "Ptr*", 0, "Ptr", 0, "UInt", 0, "Ptr")) {
	throw (Exception(Format("0x{:X}", A_LastError)))
}

originalBitmap := DllCall("Gdi32\SelectObject", "Ptr", hDC, "Ptr", hBitmap, "Ptr")

if (!DllCall("User32\DrawIconEx", "Ptr", hDC, "Int", 0, "Int", 0, "Ptr", hCursor, "Int", 0, "Int", 0, "UInt", 0, "Ptr", 0, "UInt", 0x0003)) {
	throw (Exception(Format("0x{:X}", A_LastError)))
}

if (status := DllCall("Gdiplus\GdipCreateBitmapFromHBITMAP", "Ptr", hBitmap, "Ptr", 0, "Ptr*", pBitmap := 0, "Int")) {  ;* Creates a GDI+ bitmap object from a GDI bitmap handle.
	throw (Exception(status))
}

;* Cleanup GDI objects:
DllCall("Gdi32\SelectObject", "Ptr", hDC, "Ptr", originalBitmap, "Ptr")
DllCall("Gdi32\DeleteDC", "Ptr", hDC)

DllCall("Gdi32\DeleteObject", "Ptr", hBitmap, "UInt")

loop, % (20, y := 0) {
	loop, % (20, x := 0) {
		DllCall("Gdiplus\GdipBitmapGetPixel", "Ptr", pBitmap, "Int", x++, "Int", y, "UInt*", color)

		color := Format("{:08X}", color)
	}
}

;* Cleanup GDIp objects:
DllCall("Gdiplus\GdipDisposeImage", "Ptr", pBitmap)

;* Shutdown and free library:
DllCall("Gdiplus\GdiplusShutdown", "Ptr", pToken)
DllCall("Kernel32\FreeLibrary", "Ptr", DllCall("Kernel32\GetModuleHandle", "Str", "Gdiplus", "Ptr"), "UInt")
You have to do a little dance with GDI and GDIp (there may be a way to do it with GDIp alone, idk)
emanate22
Posts: 52
Joined: 11 May 2021, 00:03

Re: How to get color of cursor in game?

11 May 2021, 09:29

Onimuru wrote:
11 May 2021, 05:21
This is how I would do it:

Code: Select all

DllCall("Kernel32\LoadLibrary", "Str", "Gdiplus", "Ptr")

VarSetCapacity(input, 8 + A_PtrSize*2)
	, NumPut(0x1, &input + 0, "UInt")

DllCall("Gdiplus\GdiplusStartup", "Ptr*", pToken := 0, "Ptr", &input, "Ptr", 0, "Int")



VarSetCapacity(info, cbSize := 16 + A_PtrSize)
	, NumPut(cbSize, &info + 0, "UInt")

if (!DllCall("User32\GetCursorInfo", "Ptr", &info, "UInt")) {
	throw (Exception(Format("0x{:X}", A_LastError)))
}

hCursor := NumGet(&info + 8, "Ptr")

if (!hDC := DllCall("Gdi32\CreateCompatibleDC", "Ptr", 0, "Ptr")) {
	throw (Exception(Format("0x{:X}", A_LastError)))
}

;* I'm not sure if you can determine the width and height of a cursor/icon so I just went with 20x20 here:
VarSetCapacity(info, 40)
	, NumPut(40, &info + 0, "UInt"), NumPut(20, &info + 4, "UInt"), NumPut(-20, &info + 8, "UInt"), NumPut(1, &info + 12, "UShort"), NumPut(32, &info + 14, "UShort"), NumPut(0, &info + 16, "UInt")

if (!hBitmap := DllCall("Gdi32\CreateDIBSection", "Ptr", hDC, "Ptr", &info, "UInt", 0, "Ptr*", 0, "Ptr", 0, "UInt", 0, "Ptr")) {
	throw (Exception(Format("0x{:X}", A_LastError)))
}

originalBitmap := DllCall("Gdi32\SelectObject", "Ptr", hDC, "Ptr", hBitmap, "Ptr")

if (!DllCall("User32\DrawIconEx", "Ptr", hDC, "Int", 0, "Int", 0, "Ptr", hCursor, "Int", 0, "Int", 0, "UInt", 0, "Ptr", 0, "UInt", 0x0003)) {
	throw (Exception(Format("0x{:X}", A_LastError)))
}

if (status := DllCall("Gdiplus\GdipCreateBitmapFromHBITMAP", "Ptr", hBitmap, "Ptr", 0, "Ptr*", pBitmap := 0, "Int")) {  ;* Creates a GDI+ bitmap object from a GDI bitmap handle.
	throw (Exception(status))
}

;* Cleanup GDI objects:
DllCall("Gdi32\SelectObject", "Ptr", hDC, "Ptr", originalBitmap, "Ptr")
DllCall("Gdi32\DeleteDC", "Ptr", hDC)

DllCall("Gdi32\DeleteObject", "Ptr", hBitmap, "UInt")

loop, % (20, y := 0) {
	loop, % (20, x := 0) {
		DllCall("Gdiplus\GdipBitmapGetPixel", "Ptr", pBitmap, "Int", x++, "Int", y, "UInt*", color)

		color := Format("{:08X}", color)
	}
}

;* Cleanup GDIp objects:
DllCall("Gdiplus\GdipDisposeImage", "Ptr", pBitmap)

;* Shutdown and free library:
DllCall("Gdiplus\GdiplusShutdown", "Ptr", pToken)
DllCall("Kernel32\FreeLibrary", "Ptr", DllCall("Kernel32\GetModuleHandle", "Str", "Gdiplus", "Ptr"), "UInt")
You have to do a little dance with GDI and GDIp (there may be a way to do it with GDIp alone, idk)
this is so complicated but seems working.
NumPut(20, &info + 4, "UInt"), NumPut(-20, &info + 8, "UInt"),
those 20 and -20 are width and height too?
emanate22
Posts: 52
Joined: 11 May 2021, 00:03

Re: How to get color of cursor in game?

11 May 2021, 09:48

@Onimuru
confusing now how to set this if my cursor turn into a red cross.I cant find this color always output 000000 but find the regurlar one.Great script thank you so much
emanate22
Posts: 52
Joined: 11 May 2021, 00:03

Re: How to get color of cursor in game?

06 Jun 2021, 03:47

@Onimuru
May I ask Why sometimes it report errors?Is something wrong?or did I pressed too fast?is there any way to shut down the pop-up window?
it says error : Unhandled exception :0x57A
the error points to: throw (Exception(Format("0x{:X}", A_LastError)))
below:
if (!DllCall("User32\DrawIconEx", "Ptr", hDC, "Int", 0, "Int", 0, "Ptr", hCursor, "Int", 0, "Int", 0, "UInt", 0, "Ptr", 0, "UInt", 0x0003)) {
throw (Exception(Format("0x{:X}", A_LastError)))
}
User avatar
Onimuru
Posts: 107
Joined: 08 Sep 2018, 18:35
Contact:

Re: How to get color of cursor in game?

06 Jun 2021, 04:36

Error handling is important! You can get a more descriptive message from User32 functions with the following function:

Code: Select all

ErrorFromMessage(messageID) {
	if (!(length := DllCall("Kernel32\FormatMessage", "UInt", 0x1100  ;? 0x1100 = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER
		, "Ptr", 0, "UInt", messageID, "UInt", 0, "Ptr*", buffer := 0, "UInt", 0, "Ptr", 0, "Int"))) {
		return (ErrorFromMessage(DllCall("Kernel32\GetLastError")))
	}

	message := StrGet(buffer, length - 2)  ;* Account for the newline and carriage return characters.
	DllCall("Kernel32\LocalFree", "Ptr", buffer)

	return (Exception(Format("0x{:X}", messageID), -1, message))
}
for example:

Code: Select all

if (!DllCall("User32\DrawIconEx", "Ptr", hDC, "Int", 0, "Int", 0, "Ptr", hCursor, "Int", 0, "Int", 0, "UInt", 0, "Ptr", 0, "UInt", 0x0003)) {
	throw (ErrorFromMessage(A_LastError))
}
In this case 0x57A is "Invalid cursor handle."
User avatar
Onimuru
Posts: 107
Joined: 08 Sep 2018, 18:35
Contact:

Re: How to get color of cursor in game?

06 Jun 2021, 04:54

@emanate22

I can't replicate your issue. My best guess is that the cursor handle that you're retrieving from the game isn't valid for some reason but I don't know what the underlying issue might be. Perhaps you can open a new help request for this?
emanate22
Posts: 52
Joined: 11 May 2021, 00:03

Re: How to get color of cursor in game?

06 Jun 2021, 06:16

Onimuru wrote:
06 Jun 2021, 04:54
@emanate22

I can't replicate your issue. My best guess is that the cursor handle that you're retrieving from the game isn't valid for some reason but I don't know what the underlying issue might be. Perhaps you can open a new help request for this?
ok i see.Thank you for your help.Let me find out myself

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: joefiesta and 39 guests