Solved

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

Posted on 2001-08-05
8
289 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
Comment Utility
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
Comment Utility
I am NOT using MFC, just the regular win32API calls
0
 

Author Comment

by:Fearum
Comment Utility
I do have a WM_INITDIALOG, ill try put your centerwindow there
0
 
LVL 4

Expert Comment

by:IainHere
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Fearum
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
I'll try these and award the right one - srry I';ve been offline so long.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now