Archived

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

Beau

How to create a window that is centered?

Recommended Posts

Hello, I''m just getting started with programming for Windows in C++, eventually hoping to learn DirectX and make a game. Anyway, when creating a window, I know you can use CW_USEDEFAULT when setting up where the window will be to use the default location, but I was wondering if there is a simple way to have the window be centered. Also, is there a good place to look up all the useful definitions such as CW_USEDEFAULT and the many others? Thanks, Beau

Share this post


Link to post
Share on other sites
You can get the screen width and height using GetSystemMetrics() and do the math from there:
cx = screen_width/2;
cy = screen_height/2;
min_x = cx - (window_width/2), max_x = cx + (window_width/2);
min_y = cy - (window_height/2), max_y = cy + (window_height/2);

You now use min_x, min_y, max_x and max_y as your window dimensions.



I wanna work for Microsoft!

Share this post


Link to post
Share on other sites
if you are creating the window, i''ll assume you know the width and height of the window you are creating.
  
RECT rect;
GetWindowRect(GetDesktopWindow(), &rect);

//in the CreateWindow function call, for the x and y:

//x:

(rect.right >> 1) - WINDOW_WIDTH
// or if you don''t like shifting (this does the same thing)

(rect.right / 2) - WINDOW_WIDTH

//y:

(rect.bottom >> 1) - WINDOW_HEIGHT
// same as above

(rect.bottom / 2) = WINDOW_HEIGHT

Share this post


Link to post
Share on other sites