Search the Community
Showing results for tags 'getpixel'.
-
Hello there, as I used this function in a lot of projects, I might as well share it It gets a color value from a background (or foreground of course) window by using a combination of WinAPI.au3 and GDIPlus.au3 (might as well be replaced with https://msdn.microsoft.com/en-us/library/windows/desktop/dd144909(v=vs.85).aspx which should work too and is maybe a bit faster, as it wouldn't need to create an extra GDIPlus Bitmap). Long story short; here is the function: ;=================================================================================================================================== ; ; Description: Gets the Color Value from a Pixel (possible in background windows [not hidden!]) ; Syntax: GetColor() ; Parameter(s): ; $iX - X Coordinate (In the Window, not whole Screen), get it by using the Autoit Window Info tool! ; $iY - Y Coordinate (In the Window, not whole Screen), get it by using the Autoit Window Info tool! ; $WinHandle - Handle obtained by WinGetHandle ; $iWidth - Width of the Window to Capture ; $iHeight - Height of the Window to Capture ; ; Return Value(s): Hex of Color Value ; ; Requirements: ; #RequireAdmin Braucht Adminrechte falls ihr auf ein Programm mit Adminrechten zugreifen wollt! ;#include <WinAPI.au3> ;#include <WindowsConstants.au3> ;#include <GDIPlus.au3> ;#include <ScreenCapture.au3> ; #include <Array.au3> ; ; Note: Does NOT work on hidden or minimized windows as Windows stops rendering them. ; If you do not want to put in the $WinHandle all the time, replace the $WinHandle Parameter ; with $WinHandle = $hwnd (if $hwnd is your Handle obtained by WinGetHandle) ; ; Tested only on Windows 10 Professional 64 Bit ; ;=================================================================================================================================== Func GetColor($iX,$iY,$WinHandle) _GDIPlus_Startup() Local $aPos = WinGetPos($WinHandle) $iWidth = $aPos[2] $iHeight = $aPos[3] Local $hDDC = _WinAPI_GetDC($WinHandle) Local $hCDC = _WinAPI_CreateCompatibleDC($hDDC) $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $iWidth, $iHeight) _WinAPI_SelectObject($hCDC, $hBMP) DllCall("User32.dll", "int", "PrintWindow", "hwnd", $WinHandle, "hwnd", $hCDC, "int", 0) _WinAPI_BitBlt($hCDC, 0, 0, $iWidth, $iHeight, $hDDC, 0, 0, $__SCREENCAPTURECONSTANT_SRCCOPY) $BMP = _GDIPlus_BitmapCreateFromHBITMAP($hBMP) Local $aPixelColor = _GDIPlus_BitmapGetPixel($BMP, $iX, $iY) _WinAPI_ReleaseDC($WinHandle, $hDDC) _WinAPI_DeleteDC($hCDC) _WinAPI_DeleteObject($hBMP) _GDIPlus_ImageDispose($BMP) _GDIPlus_Shutdown() Return Hex($aPixelColor, 6) EndFunc ;==>GetColor And when we are at it, here is a little program to get the right color values for your program, just press Shift + X after configuration it, by putting in the right Program Title and you will get the coordinates and the color value of your mouse position inside of the program you wanted ^^ ; #RequireAdmin Braucht Adminrechte falls ihr auf ein Programm mit Adminrechten zugreifen wollt! #include <WinAPI.au3> #include <WindowsConstants.au3> #include <GDIPlus.au3> #include <ScreenCapture.au3> #include <Array.au3> HotKeySet("{ESC}","_exit") HotKeySet("X","Go") Global $x Global $y Global $hBMP ;~ MsgBox(0,"","OK!") $Title = "PUT THE TITLE OF YOUR WINDOW HERE!" $hwnd = WinGetHandle($Title) MsgBox (0,"",$hwnd) While 1 Sleep (100) WEnd Func Go() Local $MousePos = _WinAPI_GetMousePos(True, $hWnd) $x = DllStructGetData($MousePos, "X") $y = DllStructGetData($MousePos, "Y") ;~ $x = if you want to set them manual ;~ $y = use this and comment out both DLLStructGetData $Color = GetColor($x, $y, $hwnd) MsgBox(0,"","x: " &$x& " y: " &$y& " Color: " &$Color) EndFunc Func _Exit() Exit EndFunc Func GetColor($iX,$iY,$WinHandle) _GDIPlus_Startup() Local $aPos = WinGetPos($WinHandle) $iWidth = $aPos[2] $iHeight = $aPos[3] Local $hDDC = _WinAPI_GetDC($WinHandle) Local $hCDC = _WinAPI_CreateCompatibleDC($hDDC) $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $iWidth, $iHeight) _WinAPI_SelectObject($hCDC, $hBMP) DllCall("User32.dll", "int", "PrintWindow", "hwnd", $WinHandle, "hwnd", $hCDC, "int", 0) _WinAPI_BitBlt($hCDC, 0, 0, $iWidth, $iHeight, $hDDC, 0, 0, $__SCREENCAPTURECONSTANT_SRCCOPY) $BMP = _GDIPlus_BitmapCreateFromHBITMAP($hBMP) Local $aPixelColor = _GDIPlus_BitmapGetPixel($BMP, $iX, $iY) _WinAPI_ReleaseDC($WinHandle, $hDDC) _WinAPI_DeleteDC($hCDC) _WinAPI_DeleteObject($hBMP) _GDIPlus_ImageDispose($BMP) _GDIPlus_Shutdown() Return Hex($aPixelColor, 6) EndFunc ;==>GetColor I hope I was able to help somebody on the long run If you have any improvements or found any mistake, please let me know and I will try to improve the function as soon as possible. Also please excuse my "not so clean" coding, I am still learning Have fun, KryTonX EDIT: Thanks at JohnOne for his improvements on the Script, I have added WinGetPos...I wonder why I never got that Idea
- 5 replies
-
- background
- window
-
(and 3 more)
Tagged with: