CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2009
    Posts
    32

    Mouse hit test/know when mouse is over something.

    I'm using Win32 and Direct2D and I need to know when the mouse is over something so I can move/rotate it only when the mouse is over it and is down.

    I know how to rotate/move stuff with the mouse and how to tell when the mouse is down and up, all I need to know is when it's over something, like a simple square or circle.

    Could someone tell me how that is done?

  2. #2
    Join Date
    Apr 2004
    Posts
    102

    Re: Mouse hit test/know when mouse is over something.

    You can determine whether the left mouse button is down or up by trapping WM_LBUTTONDOWN or WM_LBUTTONUP messages.

    Also, assuming that the window background is one consistent color. Let's assume white, you can call GetPixel in WM_MOUSEMOVE to determine the RGB values of the pixel at the cursor location. If the cursor is on the white backround, the RGB values would be 0xff, 0xff, 0xff. Otherwise, if the cursor is on a colored rectangle, then the RBG values would be something other than 0xff, 0xff, 0xff. The return values from GetPixel would be the RGB values of the rectangle. Thus, if the RGB values change from white to some other value, you then know your cursor is on an object.

  3. #3
    Join Date
    Nov 2004
    Posts
    17

    Re: Mouse hit test/know when mouse is over something.

    D3DXIntersect can do it , check the pick example in dx sdk

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Mouse hit test/know when mouse is over something.

    The technique that you want is called picking. Basically, you need to draw everything twice, once on the screen in real colour, and again on a picking buffer in a solid, unique colour. When the mouse button goes goes, check it's position, check which colour is on the picking buffer at that coordinate, and it'll tell you which object was clicked on. It's the best for things that are arbitrarily rotated or have a lot of objects, because it's pixel perfect.

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

Featured