Skip to main content
GameDev.net gamedev.net
🔒 Locked

Center cursor to client area

Started by Hellsp4wn Aug 28, 2006 at 4:01 PM 4 replies 1.5k views
Original Post
Hellsp4wn
Hellsp4wn
I feel like an idiot, so I'm asking this in "For Beginners"... How exactly can one calculate the center of a window's client area? The GetClientRect-function does for some reason return the rectangle in client space (!!!), so the left and top elements are always zero. GetWindowRect returns the top-left including the titlebar, but I can't calculate the client area from that, since I can't find a function to retrieve the border width of the window. Anyone has the obvious solution to this?
ApochPiQ
ApochPiQ
There's probably a more elegant solution, but here's what comes to mind:

Create a rectangle with a known size, and then pass than on to AdjustWindowRect (or AdjustWindowRectEx if using extended styles). For instance, set top/left to (100,100) and bottom/right to (200,200). The AdjustWindowRect call will move the corners outwards by the same amount as the difference between your client and non-client bounding rects.

After adjusting the known rect, find the difference from the known size; e.g. the top-left may now be at (95,80). Call GetWindowRect and nudge its corners in by that amount. This will give you the client rect in screen coordinates.

From there, just divide (bottom-top) and (right-left) by 2 to get the centered coordinates and pass that to SetCursorPos.
Hellsp4wn
Hellsp4wn
This would have worked if AdjustWindowRect had worked the way it's supposed to. But it doesn't accommodate correctly for borders, for some reason.

I've realized this earlier, so I wrote some custom code to make the window size correct. But I can't base the offset on this either, since it can't differentiate between the titlebar's size and the bottom border's size, so all I get is an average...

I'm getting somewhat frustrated now.
Hellsp4wn
Hellsp4wn
I realized I just answered my own question. I can get the border width using my own code, and get the titlebar height using GetTitleBarInfo, so now I have both the things I need.

Thanks for the help, though =)
Anon Mike
Anon Mike
Assuming you're doing what I think you're doing you should stop (the extremely error-prone process of) trying to add in all the pieces of the window and just call ClientToScreen on the data returned from GetClientRect.
-Mike

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.