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

bail3yzAsked:
Who is Participating?
 
GrahamSkanConnect With a Mentor RetiredCommented:
bail3yz,

If you are right about the cause of the problem, that is useful information, so it is not a good idea to delete that question.

You could check your guess by doing something other than using a message box such as  Debug.Print or writing to a file.
0
 
bail3yzAuthor Commented:
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?
0
 
bail3yzAuthor Commented:
ahh.. seems any breakpoint somehow screws it up?

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
ArkCommented:

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

0
 
ArkCommented:
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
0
 
bail3yzAuthor Commented:
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
0
 
bail3yzAuthor Commented:
I was going to close the question, but I will give you the points for trying to help instead
0
 
ArkCommented:
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)
0
 
bail3yzAuthor Commented:
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

0
 
ArkConnect With a Mentor Commented:
Did you try BitBlt workaround?
 
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) 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

Public Functiom GetPixelEx(ByVal hDc As Long, ByVal x As Long, ByVal y As Long) As Long
   Dim R As Long
   R = GetPixel(hD, P.x, P.y)
   If R = -1 Then
        BitBlt Picture1.hdc, 0, 0, 1, 1, hD, P.x, P.y, vbSrcCopy
        R = Picture1.Point(0, 0)
    End If
    GetPixelEx = R
End Function

Open in new window


Note: Picture1 can be invisible if Autoredraw=True
0
 
bail3yzAuthor Commented:
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

0
 
bail3yzAuthor Commented:
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

0
 
bail3yzAuthor Commented:
dont want to cancel anymore
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.