Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2001-08-05
8
Medium Priority
?
326 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
  • 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
Independent Software Vendors: 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 150 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

Industry Leaders: 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!

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

877 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