#### Archived

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

# center the window?

This topic is 6396 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m using OpenGl in VC++ 6. In the CreateWindowEx( function it asks for x,y coordinates to create the new window at. Is there anyway I can tell it to center it on the screen? The reason being, on my 19in monitor I put it some x,y coords and got the window centered, but when I ran the program on my 15in monitor the window was halfway off the screen. Is there a function that finds the center? Thanks, Brian

##### Share on other sites
use getsystemmetrics(..)
sorry i cant say more but i don''t have all the help on me computer can someone pls tell me which one of the windows sdk help files this function is described in. i try downloading the files from MS but something always goes wrong (its a 500mb download) i need more detailed info on what help files i need

##### Share on other sites
From your problem it sound like u are using a hard coded value for x and y based on the resolution on your comp with the 19inch monitor. And the 15inch monitor is using a different resolution so its not the center anymore. ie.: when in 800x600 the center would be 400x300, but in 1600x1200 the center would be 800x600! Zedzeek is right....you use the GetSystemMetrics function to find the current screen res(size). You can then use a bit of math(divide by 2) to get the center for y and x. Then based on the size of your window, you would take:
screenx -= windowssizex / 2
screeny -= windowsizey / 2
This code is based on a screen where values are like this:
0,0 1024, 0

0, 768 1024, 768

If you dont understand what im saying...email me at :
blide@mail.com

##### Share on other sites
damn my visual didnt come out right....what i was showing is that the upper left is 0, 0 and right gets higher in x as down gets higher in y.

##### Share on other sites
also forgot to mention that after all that...the screenx and screeny are the coordinates on the screen for the upper left corner of your window...which is what you need(thats what you specify to createwindowex)

##### Share on other sites
Just to make this complete:
  int iScreenWidth = GetSystemMetrics(SM_CXSCREEN);int iScreenHeight = GetSystemMetrics(SM_CYSCREEN);

Afterwards you might need AdjustWindowRectEx to compensate a pixels for the border...

##### Share on other sites
Thanks for all the replys guys!

Brian