Sign in to follow this  

Centering a window

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


RECT workarea;

// get current screen dimensions less the system taskbar or application desktop toolbars
// receives the coordinates of the work area, expressed in virtual screen coordinates
SystemParametersInfo(SPI_GETWORKAREA, sizeof(workarea), &workarea, 0);





For details see:

SystemParametersInfo
Using SystemParametersInfo to access user interface settings

Share this post


Link to post
Share on other sites
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

This topic is 3862 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this