Solved

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

Posted on 2001-08-05
8
306 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:Fearum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 

Expert Comment

by:AAB
ID: 6352852
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;
}

0
 

Author Comment

by:Fearum
ID: 6352931
I am NOT using MFC, just the regular win32API calls
0
 

Author Comment

by:Fearum
ID: 6352936
I do have a WM_INITDIALOG, ill try put your centerwindow there
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:IainHere
ID: 6355866
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)
0
 

Author Comment

by:Fearum
ID: 6363478
The problem is not the window, really, more is getting the screens resolution so that I can center it ;)
0
 

Accepted Solution

by:
AAB earned 50 total points
ID: 6367232
You can get the the dimensions?widths and heights?of Windows display elements by usin API function GetSystmeMetrics();
you have to specify the index ..
This sample can help you ..

int x = ::GetSystemMetrics(SM_CXSCREEN);
   int y = ::GetSystemMetrics(SM_CYSCREEN);
   GetClientRect(hWnd,&wrect);
   int wx = wrect.right /2;
   int wy = wrect.bottom /2;
   if (rect.left != 0)        
   SetWindowPos(hWnd,HWND_TOP,(x /2) - wx  ,(y/2) - wy,0,0,SWP_NOSIZE);

Try it can help you...
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6367335
>>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);
0
 

Author Comment

by:Fearum
ID: 6431780
I'll try these and award the right one - srry I';ve been offline so long.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question