Solved

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

Posted on 2001-08-05
8
300 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
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Embarcadero C++ Builder XE2 TDateTime 8 80
Dynamically allocate memory 9 70
FMX TCameraComponent Problem 2 92
No module found pypyodbc, 3 43
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

685 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