Jump to content
  • Advertisement


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


How to create a window that is centered?

This topic is 6100 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

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:


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

(rect.right / 2) - WINDOW_WIDTH


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

(rect.bottom / 2) = WINDOW_HEIGHT

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!