Archived

This topic is now archived and is closed to further replies.

C++ -- mouse-position in Windows

This topic is 5000 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I''m wondering if I could get some help with a problem I''m trying to solve: Basically, I''m moving an object on the screen according to the mouse position, but constrained along the x-axis (left and right movement only). However, I want the object to move even if the mouse is not within the client area, for example below the window. The program the way it is now, if the user moves the mouse around below the window, the object will not move at all. I''ve tried SetCapture(), but this just made it so the movement included the non-client area of the window as well. Following-up on some information I found elsewhere, I tried holding down the mouse-button, and it resulted in the desired behaviour. However, upon releasing it, it went back to how it was. Also, a minor point, but when I use SetCapture, the cursor switches to the hourglass.. why is that? and how would I leave it as a default arrow? Thank you for your time, TwoMinds

Share this post


Link to post
Share on other sites
Thanks for the reply.

Yeah, I''ve tried that already. My first attempt was just grabbing the WM_MOUSEMOVE message, and using the coordinates. Then, I tried using GetCursorPos(POINT*), but the same behaviour as described above ocurred.

I''m guessing (please correct me if I''m wrong) that the main problem is that Windows isn''t sending WM_MOUSEMOVE messages to my program when the mouse moves out of the client area. I guess it comes down to needing to find a way to get Windows to send those messages regardless (which is what I thought SetCapture would do).

That or I could constantly poll the mouse position in my game loop..... *cough*

Share this post


Link to post
Share on other sites
According to Petzold,

the mouse message

WM_NCHITTEST proceeds all other mouse messages and is usually processed by DefWindowProc(). It contains the screen coordinates (x,y) in the ''lParam'' parameter. This should give you access to the mouse position at all times. You will have to keep track of when the screen coordinates are inside or outside your window or client area. I have not tried this myself, so don''t kill me if it doesn''t work!

Another simpler approach might be to make a background popup window that fills the screen and use the mouse messages in that window to control the thing you want to do in the remaining window. Maybe that will do what you want, but I don''t know.

Brian

Share this post


Link to post
Share on other sites
Hi Brian, thanks for the suggestions

I''ve been trying to get the WM_NCHITTEST method, and I''m beginning to wonder if my problems might be stemming from not capturing the mouse properly. According to MSDN: "the system sends a WM_NCHITTEST message to either the window that contains the cursor hot spot or the window that has captured the mouse."

As mentioned above, there is an hourglass cursor when I start the program (that isn''t there if I don''t SetCapture). If I click outside my program, losing the capture, and then come back to my program (I haven''t inserted code to recapture) the normal arrow cursor is there, and the image moves with the cursor position. Still, when I move outside my window, the image doesn''t move... essentially the behaviour I got without calling SetCapture.

As it is now, I call SetCapture(g_hMainWnd) after doing all my init stuff for my windowclass, etc. Is there something special or tricky that I missed for using it properly? (or maybe something simple that I missed...)

Share this post


Link to post
Share on other sites
Just replying to my own message after a bit of research.

A couple messages up, I mentioned polling not really seriously.. but I came across this article (sorry, I don't know html tags or whatever) http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnargame/html/msdn_dos2win2.asp

The author mentions using polling to get the mouse-position, using the GetCursorPos(---) function mentioned already. However.. I was under the impression that polling is generally not a good practice.. is it acceptable in some circumstances?

Perhaps given that mouse movement occurs relatively fast (compared to other input.. like button clicks or key presses), it's more acceptable?

I'd be grateful for some input from experienced programmers. Thanks!


**edit** In the article, he mentions Direct Input 3, and his references are from the mid 90's.. things may have changed since he wrote it, I'm not sure


[edited by - TwoMinds on April 4, 2004 2:29:55 AM]

[edited by - TwoMinds on April 4, 2004 2:30:13 AM]

Share this post


Link to post
Share on other sites