In CreateWindowEx, is there a way to automatically center the window to the screen no matter what resolution of the machine?
In CreateWindowEx, is there a way to automatically center the window to the screen no matter what resolution of the machine?
The fifth & sixth params that most people pass CW_USEDEFAULT are to set the position of top left part of the screen.
I guess you can use a few calls to GetSystemMetrics() to work out the size of the screen......then look at the size of your window (set in the seventh & eighth params), work out the sizeing....and pass the correct params to CreateWindowEx()
Here's what I use:
Just expounding on what Fordy was saying. The param's that are in bold get the system width and height (respectively) then divide each by 2 (center of the screen) then subtract half the width/height of the window.Code:CreateWindowEx(0,WNDCLS,WNDTTL,WS_BORDER | WS_SYSMENU | WS_VISIBLE, GetSystemMetrics(SM_CXSCREEN)/2-160, GetSystemMetrics(SM_CYSCREEN)/2-120, 320,240,NULL,NULL,hInstMain,NULL)
There is a dialog style
DS_CENTER
Should work in windows as well. Will center in parents window. If no parent then center on main screen.
"Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
Friedrich Nietzsche
"I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
George Best
"If you are going through hell....keep going."
Winston Churchill
[code]
int width = 100; //...now you just change this for each program...
int height = 100; //...ditto...
int screenX = GetSystemMetrics(SM_CYSCREEN);
int screenY = GetSystemMetrics(SM_CXSCREEN);
int xCtr = (screenX / 2) - (width / 2);
int yCtr = (screenY/ 2) - (height / 2);
CreateWindowEx(0,WNDCLS,WNDTTL,WS_BORDER | WS_SYSMENU | WS_VISIBLE,
xCtr,
yCtr,
width,
height,
NULL,NULL,hInstMain,NULL)
[code]
I always prefered:
Usage for a main window 800 x 600 would be:Code:/* Function CenterWindow(), Centers Window */ VOID CenterWindow(HWND hwnd, HWND hwndParent, int Width, int Height) { /* Variables */ RECT rc; /* If Parent Window Is Set As Null, Get The Desktop Window */ if(hwndParent == NULL) { hwndParent = GetDesktopWindow(); } /* Get Parent Client Area Measurements */ GetClientRect(hwndParent, &rc); /* Center The Window */ MoveWindow( hwnd, (rc.right - rc.left - Width) / 2, (rc.bottom - rc.top - Height) / 2, Width, Height, TRUE ); return; }
CenterWindow(hwnd, NULL, 800, 600);
Usage for a child window 400 x 400 would be:
CenterWindow(hwndChild, hwndParent, 400, 400);
Hope it helps.
(of course I came into the conversation too late )
Yes, there are logical ways to center windows. You just have to work with what your mama gave you...
1978 Silver Anniversary Corvette