Jump to content
  • Advertisement
Sign in to follow this  
spooky_paul

Centering a window

This topic is 4077 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 want to have a window dead-center in the screen when it initializes and i was pondering about making 2 functions that measure the screen`s resolution then divide it in 2 and subtract half the apps dimensions (pixels). A function for width and one for height. Than use them with the CreateWindowEx function. Can you help me elaborate those functions? I am not all that familiar with the windows api. Do you know any better way to center a window? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
I would check out the GetSystemMetrics function.
To center a window I usually do just this:


if (!(hWnd = CreateWindowEx(NULL,
"mainClassName",
gameName,
WS_OVERLAPPEDWINDOW | WS_VISIBLE |WS_CLIPSIBLINGS |
WS_CLIPCHILDREN,
GetSystemMetrics(SM_CXSCREEN)/2-xScreenSize/2,
GetSystemMetrics(SM_CYSCREEN)/2-yScreenSize/2,
xScreenSize+GetSystemMetrics(SM_CXFRAME)*2,
yScreenSize+GetSystemMetrics(SM_CYCAPTION)+GetSystemMetrics(SM_CXFRAME)*2,
NULL,
NULL,
hInstance,
NULL)))
{
MessageBox(hWnd, "Can't create window", "Error!", MB_OK);
PostQuitMessage(0);
}



where xScreenSize and yScreenSize are the dimensions of your game window. The lines that set the width and height of the window make sure to compensate for windows borders.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!