Hello, I was wondering is it possible from a VB program to grab the color of a pixel at a given location(Like.. 20x 40y)? And if so... How?
Printable View
Hello, I was wondering is it possible from a VB program to grab the color of a pixel at a given location(Like.. 20x 40y)? And if so... How?
You mean this?
VB Code:
Private Declare Function GetPixel Lib "gdi32" (ByVal Hdc As Long, ByVal x As Long, ByVal y As Long) As Long
How exactly would I use it? Because when I try it returns -1?... What I am trying to do is get a color value from a web page, then press a key/click something if the color is there.
Thank you a lot.
Sorry for the double post, but how would I make it so when I clicked the left mouse button it would output the color, mouse x and mouse y?
Try this:
Download APIGuide and API Viewer from the links in my sig. In APIGuide you'll find many other mouse-related APIs and their description.VB Code:
' Add a commandbutton in your form and paste this code. ' set the scalemode of the form to Pixels ' run the project, click the command button then click anywhere on the screen Option Explicit Private Type POINTAPI X As Long Y As Long End Type Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function ReleaseCapture Lib "user32" () As Long Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long Dim pt As POINTAPI Private Sub Command1_Click() SetCapture Me.hwnd Command1.Enabled = False End Sub Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = vbLeftButton Then pt.X = X pt.Y = Y ClientToScreen Me.hwnd, pt ReleaseCapture MsgBox "Clicked at " & pt.X & ":" & pt.Y & vbCrLf & _ "Get the color of pixel here" Me.Caption = "Form1" Command1.Enabled = True End If End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If Not Command1.Enabled Then ' this code is not running sometimes when mouse is over taskbar pt.X = X pt.Y = Y ClientToScreen Me.hwnd, pt Me.Caption = pt.X & ":" & pt.Y End If End Sub
Works great thanks. :D