Link to home
Start Free TrialLog in
Avatar of Fearum
Fearum

asked on

How to create Dialogs and Windows in the center of the Screen? (Visual C++)

I know how to make em, just not how to make them centered, since I've no clue about how to get the screensize, not even a fucntion name, I'll just ask it here.
Avatar of AAB
AAB

you can use the CWnd::CenterWindows()

void CenterWindow( CWnd* pAlternateOwner = NULL );

Parameters

pAlternateOwner

Pointer to an alternate window relative to which it will be centered (other than the parent window).


Centers a window relative to its parent. Usually called from CDialog::OnInitDialog to center dialog boxes relative to the main window of the application. By default, the function centers child windows relative to their parent window, and pop-up windows relative to their owner. If the pop-up window is not owned, it is centered relative to the screen. To center a window relative to a specific window which is not the owner or parent, the pAlternateOwner parameter may be set to a valid window. To force centering relative to the screen, pass the value returned by CWnd::GetDesktopWindow as pAlternateOwner.

Example

BOOL CAboutDlg::OnInitDialog()
{
   CDialog::OnInitDialog();
   
   CenterWindow();
   return TRUE;
}

Avatar of Fearum

ASKER

I am NOT using MFC, just the regular win32API calls
Avatar of Fearum

ASKER

I do have a WM_INITDIALOG, ill try put your centerwindow there
Well, following on from AAB's suggestion, I would say that you need a handle to your window, and the API calls GetParent, GetClientRect and SetWindowPos (in that order).  A hint on how to implement it: look at the source code for CWindow::CenterWindow() (in ATLWIN.h, line 1134 in my version)
Avatar of Fearum

ASKER

The problem is not the window, really, more is getting the screens resolution so that I can center it ;)
ASKER CERTIFIED SOLUTION
Avatar of AAB
AAB

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>getting the screens resolution so that I can center it

You *can* center it using only the code in ATLWIN.h as mentioned above.  Did you look at the lines

// center within screen coordinates
::SystemParametersInfo(SPI_GETWORKAREA, NULL, &rcArea, NULL);
Avatar of Fearum

ASKER

I'll try these and award the right one - srry I';ve been offline so long.