CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #1
    Join Date
    Jun 2010

    [RESOLVED] How to implement a screen color picker

    I am trying to implement a screen color picker. I think the attached image is a better explanation of what I want do achieve than a bunch of words. I'm thinking of something similar to what Spy used to pick a window to spy on.

    The following attempt failed so miserably that I hardly dare to post the code:

    System::Void Form1::btnScreenPicker_MouseMove(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e)
      if (static_cast<bool>(e->Button & System::Windows::Forms::MouseButtons::Left)) {
        Graphics ^gr = Graphics::FromHwnd(static_cast<Button ^>(sender)->Handle);
    #ifdef _DEBUG
        Diagnostics::Debug::Assert(!gr->Clip->IsInfinite(gr), "Mouse move handler got passed a Graphics with an infinite clipping region");
        Bitmap ^bmp = gcnew Bitmap(gr->ClipBounds.Width, gr->ClipBounds.Height, gr);
        lblScreenPickerPreview->BackColor = bmp->GetPixel(e->X, e->Y);
    Clicking on the button in question and holding down the mouse key results in some kind of "machine gun" sound, indicating that the assertion you can see gets fired repeatedly and Windows tries to play the assertion sound but but doesn't get far with that until I release the button. (That's not the real problem here, but why doesn't the app get blocked once the assertion fails for the first time? )

    Picking colors from my own button is of course really boring, but I tried that as a first test, and not even that works. And I'm uncertain about the concept of the color picker in general: It would certainly be terribly inefficient to bitblt the entire screen to my own bitmap just to pick a single pixel on every mouse move. But capturing the screen only once in the MouseDown handler and then keep using that bitmap would lead to missing every screen change that happens while I hold the button...

    Yes, lots of open questions here...

    I'd appreciate any help. TIA
    Attached Images Attached Images  
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Tags for this Thread

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.