Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GetPixel() function

Status
Not open for further replies.

d3sol4t3

Programmer
Oct 27, 2005
40
GB
Hey!

Im fairly new to programming and was wandering if anybody could help me with GetPixel()

Right the problem is that im trying to make a card counter, so im trying to detect a card played in a different window.

The problem is that i have no idea how to detect the colour of a pixel in another window....any ideas?

Is there a simple program that returns the colour of a pixel at given co-ordinates in another window?

Thank You!
 
If it helps heres the window id of the window im trying to get the pixel from:

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
P
rivate Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long

Private Const GW_HWNDNEXT = &H2

Dim hwndFind As Long

hwndFind& = FindWindow("Spades Client Class", vbNullString)
 
Once you get the hWnd from FindWindow function, you need to get a handle to its device context (hDC) to perform graphic operations. GetDC function returns the hDC.

After you get the hDC, you can call GetPixel function to retrieve the color of a pixel inside a window. After using the hDC in GDI API calls, you need to free the device context using ReleaseDC function.

The code will look like this.
___
[tt]
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
...
Dim hWndFind As Long, hDCFind As Long
Dim X As Long, Y As Long, Color As Long
hWndFind = FindWindow(...)
hDCFind = GetDC(hWndFind)
X = 100: Y = 200
Color = GetPixel(hDCFind, X, Y)
ReleaseDC hWndFind, hDCFind[/tt]
___

Note that the device context returned by GetDC function only covers the client area of a window. If you want to locate pixels in the non-client area as well, like menu-bar, borders or title-bar, use GetWindowDC function instead of GetDC, which returns the device context of entire window.
 
Thx 4 the reply :)

Slight problem

code looks like this :

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long

...

Private Sub Command1_Click()
Dim hWndFind As Long, hDCFind As Long
Dim X As Long, Y As Long, Color As Long
hWndFind = FindWindow("SciCalc", vbNullString)
hDCFind = GetDC(hWndFind)
X = 80: Y = 30
Color = GetPixel(hDCFind, X, Y)
ReleaseDC hWndFind, hDCFind
Msgbox (Color)

...


^^ The above example shows me trying to retrieve the pixel located at 80, 30 in the windows calculator, but
Msgbox (Color) returns -1, what am i doing wrong?
 
Actually nevermind that i fixed it :)

Using the above code ^^ Msgbox (Color) - the messagebox returns an 8 digit number, how would i convert that to hex using VB?
 
Hex$() function?

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Ok right used hex function to convert to hex, but the actual program itself does not work, it returns either FFFFFF (white) or 808080 (grey), i tried it on a program i created which had a pure blue background and it returned FFFFFFFF :(

Current code im using:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long

...

Private Sub Command1_Click()
Dim hWndFind As Long, hDCFind As Long
Dim X As Long, Y As Long, Color As Long
hWndFind = FindWindow("SciCalc", vbNullString)
hDCFind = GetDC(hWndFind)
X = 80: Y = 80
Color = GetPixel(hDCFind, X, Y)
ReleaseDC hWndFind, hDCFind
Text1.Text = Hex$(Color)
End Sub


^^ The above code was tested in wondows calculator and returned F3F6F6

Whats wrong with the code im using? is it finding the window properly?

 
Ok no worries, fixed my problem, case closed!
 
Can you document the problem and solution so that other users can benefit as well?

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top