# GetPixel help

This topic is 4846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a little problem with the GetPixel function in visual basic. I have a tutorial video on how to make a program in VB6.0, and in this video they use the GetPixel together with GetDC. And then, if you move your mouse, the color (as a long, and as a color) comes in a Label. At the video it works great (when he moves his mouse over a black images it sais 0, and over other colors it changes its variable). But in my program, It just keep saying "-1". I have looked it up, and i know it means that it can't find the window. But in the GetDC, i used GetDC(0), This is also used in the video, because it would take the hole window. So what am i doing wrong? Here is my code:
Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, X As Long, Y As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As PointAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Type PointAPI
X As Long
Y As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Dim SelWnd As Long

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.MousePointer = 99
Me.MouseIcon = Picture1.Picture
Picture1.Visible = False
End Sub

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim mousexy As PointAPI
GetCursorPos mousexy
SelWnd = WindowFromPoint(mousexy.X, mousexy.Y)
Me.MousePointer = 0
Picture1.Visible = True
End Sub

Private Sub Timer1_Timer()
Dim mousexy As PointAPI
GetCursorPos mousexy
Label2.Caption = GetPixel(GetDC(0), mousexy.X, mousexy.Y)
Label1.Caption = GetCursor()
End Sub

Public Function GetCursor()
Dim WndRECT As RECT
Dim mousexy As PointAPI
GetWindowRect SelWnd, WndRECT
GetCursorPos mousexy
GetCursor = (mousexy.X - WndRECT.Left) & "," & (mousexy.Y - WndRECT.Top)
End Function


##### Share on other sites
This is where you stop using VB, and start playing around with C++ [smile]. You'll realize that once you get started with SDL, Visual Basic isn't powerful enough for you.

##### Share on other sites
Ah, an answer i want to hear. :-D
I just download vb two days ago, because i downloaded that video. But I'm programming in c++ for a couple of years now. So I'm a bit better in C++ then i am in VB.
Well, let's see if i can do it in C++.
I never worked with finding a pixelcolor in C++, but i think i could find that one out. Or do you have a good function for it in C++?

##### Share on other sites
I think i found how i could get it in C++ (using borland C++ builder):

void __fastcall TForm1::Button1Click(TObject *Sender){   char* winTitle = new char[255];    winTitle = "Form1";      // input the title bar of the window    HWND hWnd = FindWindow(0, winTitle);  // find the window's handle using the title bar text..    HDC   winDC = GetDC(hWnd);   // get the graphics handle to the client area of the window. HDC hdc;    COLORREF ERG= GetPixel(winDC,120,120);    Label1 -> Caption = GetRValue(ERG);    Label2 -> Caption = GetGValue(ERG);    Label3 -> Caption = GetBValue(ERG);}

And it worked, so thanx for pointing me in the right direction

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632581
• Total Posts
3007197

×