Results 1 to 11 of 11

Thread: [RESOLVED] help: find a pixel on screen by its color

Threaded View

  1. #8
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: help: find a pixel on screen by its color

    Quote Originally Posted by Cipherman
    ive often seen the hDC thing in api calls but not entirely sure what it means.

    in this example

    lColor = GetPixel(GetWindowDC(GetDesktopWindow()), x, y )

    does it mean it will only search pixels on the desktop

    -

    and in this example

    ColorMe = GetPixel(hDC, X, Y)

    does it mean it will search pixels on the current screen?
    or do i have to change the hDC to something else to do that?
    Quote Originally Posted by MSDN
    hDc: This property is a Windows operating environment device context handle. The Windows operating environment manages the system display by assigning a device context for the Printer object and for each form and PictureBox control in your application. You can use the hDC property to refer to the handle for an object's device context. This provides a value to pass to Windows API calls.
    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.

    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:
    Quote Originally Posted by MSDN
    GetDC: The GetDC function retrieves a handle to a display device context (DC) for the client area of a specified window or for the entire screen. You can use the returned handle in subsequent GDI functions to draw in the DC.
    And,
    Quote Originally Posted by MSDN
    GetWindowDC: The GetWindowDC function retrieves the device context (DC) for the entire window, including title bar, menus, and scroll bars. A window device context permits painting anywhere in a window, because the origin of the device context is the upper-left corner of the window instead of the client area.
    You can find their full documentation in your MSDN CD or here

    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,
    VB Code:
    1. Dim lDesktopHwnd As Long
    2. Dim lDesktopDc As Long
    3. Dim lColor As Long
    4.  
    5. lDesktopHwnd = GetDesktopWindow() 'get hWnd of the desktop
    6. lDesktopDc = GetWindowDC(lDesktopHwnd) 'get the hDc of the desktop
    7. lColor = GetPixel(lDesktopDc, x, y) 'get the color of specified pixel
    Hope it helps.

    Edit: Hey ! You changed the complete post.
    Last edited by iPrank; Aug 26th, 2006 at 03:52 AM.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width