Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows API
  4. "Simple" capture pixel under mouse routine...

"Simple" capture pixel under mouse routine...

Scheduled Pinned Locked Moved Windows API
tutorialquestionlearning
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Craig Longman
    wrote on last edited by
    #1

    I'd really have thought after so many years of being a graphical OS, this wouldn't be so hard... I'm trying to make a "simple" button that when clicked, allows the user to select a pixel on the screen to choose a colour. Almost everything is _finally_ working OK, I have a mouse hook in to commandeer the mouse entirely (no UAC involved even!?!) and I have a timer to grab the current pixel under the cursor. Not the simplest to accomplish, but eventually I figured it out. Even managed to figure out how to swallow the clicks so they don't get passed on. Now, I'm trying to turn the cursor into an eyedropper while I have the mouse so it's clear where the user is hovering over... SetCursor seems entirely ineffective. I set it with a cursor (one of the defaults from 'Add->Cursor' in the resource manager), confirmed it's loaded correctly, but nada... I can't tell if SetCursor is going to work to hold it, as the cursor never even changes. I have verified that the cursor is successfully loaded, well, verified that a non-NULL handle is returned, but still it doesn't seem to change when I set the cursor. I've also tried "resetting" the cursor to NULL with a call to ::SetWindowLong( m_hWnd, GCL_HCURSOR, (LONG)NULL ), but it seemed to make no difference. Clearly it's possible, as every fourth program has a colour picker doodad it seems, but what hoops does one have to jump through? Do I really have to create a fake window, copy the screen DC, and proceed from there? Pointers/advice appreciated. Thanks, CraigL

    P 1 Reply Last reply
    0
    • C Craig Longman

      I'd really have thought after so many years of being a graphical OS, this wouldn't be so hard... I'm trying to make a "simple" button that when clicked, allows the user to select a pixel on the screen to choose a colour. Almost everything is _finally_ working OK, I have a mouse hook in to commandeer the mouse entirely (no UAC involved even!?!) and I have a timer to grab the current pixel under the cursor. Not the simplest to accomplish, but eventually I figured it out. Even managed to figure out how to swallow the clicks so they don't get passed on. Now, I'm trying to turn the cursor into an eyedropper while I have the mouse so it's clear where the user is hovering over... SetCursor seems entirely ineffective. I set it with a cursor (one of the defaults from 'Add->Cursor' in the resource manager), confirmed it's loaded correctly, but nada... I can't tell if SetCursor is going to work to hold it, as the cursor never even changes. I have verified that the cursor is successfully loaded, well, verified that a non-NULL handle is returned, but still it doesn't seem to change when I set the cursor. I've also tried "resetting" the cursor to NULL with a call to ::SetWindowLong( m_hWnd, GCL_HCURSOR, (LONG)NULL ), but it seemed to make no difference. Clearly it's possible, as every fourth program has a colour picker doodad it seems, but what hoops does one have to jump through? Do I really have to create a fake window, copy the screen DC, and proceed from there? Pointers/advice appreciated. Thanks, CraigL

      P Offline
      P Offline
      pasztorpisti
      wrote on last edited by
      #2

      Note that the cursor shape is always changed on every mouse move because the windows that is under the cursor is always receiving WM_SETCURSOR message, so you are fighting against the windows that are under your cursor. On way to avoid that is capturing the mouse by your HWND. A good thing about mouse capturing is that while you capture the mouse, no WM_SETCURSOR messages are sent to windows under the cursor, so you can set your on cursor and it will stay until you release the capture. Here you can find a small example prog about using mouse capture. http://www.codeproject.com/Tips/127813/Using-SetCapture-and-ReleaseCapture-correctly.aspx[^]. Some hints about picking a color: The easiest way is to ask the user to move the cursor above a pixel and to ask him to press for example a key because you can not prevent the window below the cursor from receiving the click. Another more sophisticated solution can be creating a big window that covers the whole virtual screen (not THE SCREEN but the virtual screen including all monitors!) and you could capture and draw the actual image of the whole virtual screen on this big window. In this case you don't even need the SetCapture() trick, and its easy to receive the mouse click. Another quick note: If you use a window that covers the whole screen than either return 1 from the window's WM_ERASEBKGND message handler, or do the whole painting in WM_ERASEBKGND instead of WM_PAINT to avoid flickering when you show up the window! EDIT: Or maybe you could use a color picking mechanism that is similar to the HWND picker of spy++. The user presses down the left button on your color picker control that captures the mouse and sets the cursor. After it the user moves the cursor above a pixel and releases the left mouse button, this is when you take action. During mousemove you can update your info area to show something about the actual pixel.

      modified on Thursday, December 2, 2010 11:16 AM

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups