ok imagine that my entire screen was blue and one pixel was red. how can i find this pixel?
is there a way to scan through every pixel then when the pixel u want is found , store its location in a variable?
thanks
Printable View
ok imagine that my entire screen was blue and one pixel was red. how can i find this pixel?
is there a way to scan through every pixel then when the pixel u want is found , store its location in a variable?
thanks
Take a look at http://www.allapi.net/apilist/apilist.php for GetPixel/GetWindowDC/GetDesktopWindow/GetDIBits/BitBlt APIs.
Easiest (but very slow) way is using the GetPixel API.
Something like this:
VB Code:
lColor = GetPixel(GetWindowDC(GetDesktopWindow()), x, y )
Or, you can do it by modifying the code from GetDIBits's example. It will be a LOT faster.
this works for entire screen and not just restricted to the dymentions of program form?
Both codes should work for entire screen.
Do you want it for a particular form only ?
nope :)
thanks for help and links. ill check em' out.
when i run the below code. the returned color is 16382453
however i dont know what that means
i tried spliting it up into RGB but that didtn work cas it went over 255
163 824 53
163 82 453
16 382 453
so what does 16382453 mean?
thx
VB Code:
Private Declare Function GetDesktopWindow Lib "user32" () 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 GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long Private Sub Form_Load() Dim dhwnd As Long Dim dhdc As Long Dim color As Long Dim x As Long Dim y As Long dhwnd = GetDesktopWindow dhdc = GetWindowDC(dhwnd) color = GetPixel(dhdc, 0, 0) Debug.Print color End Sub
Quote:
Originally Posted by Cipherman
You can think it as a drawing board. If your window/control/printer has an hDc, whatever you 'draw' on its hDc, it appears on the screen/printed page.Quote:
Originally Posted by MSDN
Note: Some controls like Label or Image controls don't have any DC. They are painted direcly on the form. They are called lightweight controls.
Again, some controls, like the Frame control, don't expose the hDC property in VB IDE. You'll not see it in the intellisence list or in the object browser. But you can access it using the GetDC API.
GetPixel gets the specified pixel of a given hDc (in other words, a particular pixel of a window)
For your own form/Picturebox etc, you can get the hDc from the .hDc property.
For getting the hDc of an external window we have 2 APIs:
And,Quote:
Originally Posted by MSDN
You can find their full documentation in your MSDN CD or hereQuote:
Originally Posted by MSDN
The desktop itself is a window. It has valid hWnd (Window Handel) and valid DC.
So, in order to get a particular pixel from it's DC, we need to first get its hWnd by using the GetDesktopWindow API.
Then we need to get its dc using the GetWindowDC API.
Finally, we need to get the pixel's color value using the GetPixel API.
If we break the code,
lColor = GetPixel(GetWindowDC(GetDesktopWindow()), x, y )
it will be like,
Hope it helps. :)VB Code:
Dim lDesktopHwnd As Long Dim lDesktopDc As Long Dim lColor As Long lDesktopHwnd = GetDesktopWindow() 'get hWnd of the desktop lDesktopDc = GetWindowDC(lDesktopHwnd) 'get the hDc of the desktop lColor = GetPixel(lDesktopDc, x, y) 'get the color of specified pixel
Edit: Hey ! You changed the complete post. :D
colors in Windows (or VB) is represented by Long integers (32bit).Quote:
Originally Posted by Cipherman
The value 16382453 (or F9F9F5 in Hex) is the value of this color.
After the debug.print line add,
Me.BackColor = color
it will change the form's background color to the color of the specified pixel.
For VB's long to RGB conversion, you'll need to extract each color from the long value.
There are many example on the web: http://www.google.com/search?client=...utf-8&oe=utf-8
many thanks to both of you. im glad someone got to my first question before i edited it
:thumb:
Both of me are glad to help. ;)Quote:
Originally Posted by Cipherman
>.<
i got some code which worked for me
VB Code:
Public Sub LongToRGB(ByVal lngColor As Long, intRed As Integer, intGreen As Integer, intBlue As Integer) intRed = lngColor Mod &H100 lngColor = lngColor \ &H100 intGreen = lngColor Mod &H100 lngColor = lngColor \ &H100 intBlue = lngColor Mod &H100 End Sub
many thank iPrank