Results 1 to 9 of 9

Thread: get the point colour

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    get the point colour

    When somebody clicks on a window or any place in the client area, how do get the color of the point where the mouse if clicked.


    Actually, I want to make a color picker.
    Baaaaaaaaah

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Something like this: (i haven't tested it)
    PHP Code:
    BUTE Red,Green,Blue;
    POINT Mouse;
    GetCursorPos(&Mouse);
    COLORREF Colors;
    Colors GetPixel(GetWindowDC(ChildWindowFromPoint(WindowFromPoint(Mouse), Mouse)), Mouse.xMouse.y);
                
    Red GetRValue(Colors);  //contains red value
    Green GetGValue(Colors);  //contains green value
    Blue GetBValue(Colors); //contains blue value 
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    But If I put that in a some event like "WM_MOSEMOVE" then I want to write the value that I get so that I know if that code is working. I have tried to show a messagebox with one the values in but it gives me the following error:

    Code:
    error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'unsigned char' to 'const char *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    I used the following code to show a messagebox with the red and blue values in:

    Code:
    case WM_MOUSEMOVE:
    GetCursorPos(&Mouse);
    Colors = GetPixel(GetWindowDC(ChildWindowFromPoint(WindowFromPoint(Mouse), Mouse)), Mouse.x, Mouse.y);
                
    	Red = GetRValue(Colors);  //contains red value
    	Green = GetGValue(Colors);  //contains green value
    	Blue = GetBValue(Colors); //contains blue value 
    	MessageBox(NULL,Red,"Hi",MB_OK);
    		return 0;
    Baaaaaaaaah

  4. #4
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Use the itoa function to use the red var in a messagebox.
    Also i think it is not a good idea to pop up a messagebox on the mousemove message.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  5. #5

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    I am using the messageboxes for just testing so that I know I am getting the right value.


    Let me test and if it does not work then I will reply again...
    Baaaaaaaaah

  6. #6

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    no, it is not quit right

    Here is the code that I use. But I always shows me the value "192" for every color(Red, Green, Blue)
    Even if I change my windows' color, it stays the same:

    Code:
    case WM_LBUTTONDOWN:
    
    Colors = GetPixel(GetWindowDC(ChildWindowFromPoint(WindowFromPoint(Mouse), Mouse)), Mouse.x, Mouse.y);
                
    Red = GetRValue(Colors);  //contains red value
    Green = GetGValue(Colors);  //contains green value
    Blue = GetBValue(Colors); //contains blue value
    hdc = GetDC(hwnd);
    TextOut(hdc, 30,50,itoa(Red, buffer,10), 3);
    TextOut(hdc, 80,50,itoa(Blue, buffer1,10), 3);
    TextOut(hdc, 130,50,itoa(Green, buffer2,10), 3);
    		ReleaseDC(hwnd, hdc);
    return 0;
    I am drawing the text when the left button is clicked. So I also have problem with the PAINT event because whenever I resize my window, it just repaints the whole area and I loose my text.

    How can I fix both of these?
    Baaaaaaaaah

  7. #7
    Lively Member
    Join Date
    Dec 2000
    Location
    Indiana
    Posts
    73

    DLL?

    If you dont mind it, you can write a tiny ActiveX DLL in VB using its "point" function....... Just pass it the x and y.... and it will return pixel color

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    That kind of defeats the object of using C++ though
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    Megatron
    Guest
    The GetPixel API is a lot faster than the Point function, and is more efficient when using C++.

    Even if you did make a VB DLL, you'd have to include the runtime files.

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