Link to home
Start Free TrialLog in
Avatar of bail3yz
bail3yz

asked on

GetPixel incorrectly returning -1

Dim hdc As Long
hdc = GetWindowDC(mainwindow)

MsgBox GetPixel(hdc, 100, 100) 'this returns the correct information

MsgBox checkPixel(hdc,100,100)  'code for this is below

MsgBox GetPixel(hdc, 100, 100) 'now this returns -1 everytime.. hdc is still the same value.. nothing with the window changed.. also I beloeve checkPixel returns -1 for every GetPixel call too

Anyone know why this happens?  whats setting this off?.. I ve had this problem before but cant remember the solution
Public Function checkPixel(ByVal hdc As Long, x As Integer, y As Integer)

Dim i As Integer

For i = 0 To 60
    If (GetPixel(hdc, x + i, y)) = 0 Then 
        checkPixel= 1
        Exit For
    End If
Next i

End Function

Open in new window

Avatar of bail3yz
bail3yz

ASKER

Ah I think I figured it out.. its the MsgBox that screws it up.. everything after that always returns -1 for that hdc.. anyone know why?
Avatar of bail3yz

ASKER

ahh.. seems any breakpoint somehow screws it up?

Avatar of Ark

R = GetPixel(hdc, x, y)
    If R = -1 Then
        BitBlt Picture1.hdc, 0, 0, 1, 1, hdc, x, y, vbSrcCopy
        R = Picture1.Point(0, 0)
    End If

Open in new window

Just to explain:
From MSDN:
>>If the pixel is outside of the current clipping region, the return value is CLR_INVALID<<
So BitBlt is a workaround
Avatar of bail3yz

ASKER

It is not outside the region though .. thats the problem

It appears its a bug with GetPixel or something.. when there is a msgbox or any other breakpoint before getpixel I think the hdc is somehow cleared or something like that

ie this code

MsgBox GetPixel(hdc, 100, 100)
MsgBox GetPixel(hdc, 100, 100)

exact same thing back to back.. first one will return the correct answer.. second one will return -1
Avatar of bail3yz

ASKER

I was going to close the question, but I will give you the points for trying to help instead
No problem, you can close question, I don't need points - I already have enough :)
The problem is that MsgBox switch focus on your application.
try
hdc=GetDC(lHandle)
MsgBox GetPixel(hdc, 100, 100)
SetForegroundWindow(lHandle)
MsgBox GetPixel(hdc, 100, 100)
Avatar of bail3yz

ASKER

ya I tried that.. its not just msgbox either

I could have it outputting the values to a file instead and if i just put a break point in the program everything after the breakpoint outputs -1.. even if the window is visible the entire time

altho the window should only need to be visible for the GetWindowDC I believe?.. altho I am not sure about that.. but the window is visible 100% of the time

i dont know if its a bug with getpixel or something with VB 6 maybe

ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bail3yz

ASKER

when I run the VB app in IDE mode and do the highlight red thing to add a breakpoint it does it

but when I add the info to a listbox it works

I didnt try your work around.. because now that I know whats causing it I just wont use msgbox or breakpoints to check the values when testing.. also not sure if your workaround would work on a 3rd party window?

I am still curious to why it happens tho.. when I get a chance I am going to try it in a blank project to see if it has anything to do with my project or if its an unrelated bug

ill try debug.print now

Avatar of bail3yz

ASKER

ya .. its definitely just breakpoints.. very strange

when i did debug.print it worked

then I did a test where I would debug.print 9 pixels and had a break point at the 5th pixel
and the final 4 all returned -1.. the window it was reading from was visible the entire time

Avatar of bail3yz

ASKER

dont want to cancel anymore