|
Center a Dialog Box with Respect to its Owner Window
|
|
|
|
| Submitted on: 2/21/2004 3:53:23 AM
By: Ixac
Level: Beginner User Rating: Unrated Compatibility:Microsoft Visual C++
Users have accessed this article 524 times. | (About the author) |
| | The function presented here takes a handle to a dialog box as a parameter and center's it with respect to its owner. | | | Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description. |
Center a Dialog Box with Respect to its Owner Window
Visual C++ 6.0 provides an option - "Center
Dialog" to center the dialog with respect to the screen. This is quite
convenient. However, no option has been provided to center the dialog
with respect to the owner of the dialog. "CenterDlgToParent" function,
presented in this article, centers the dialog identified by the handle to the
window passed as a parameter with respect to its parent.
The function also takes into account the
desktop co-ordinates. When calculating the center of the dialog, if it is
found that the dialog box crosses the screen boundaries then the position is
manipulated so that the dialog box remains within the screen boundaries.
Source Code
void CenterDlgToParent
(HWND hwndDlg)
{
// Get the parent window
HWND hwndParent = reinterpret_cast (
::GetWindowLong (hwndDlg, GWL_HWNDPARENT));
// Get window rect of the parent
RECT rectParent;
::GetWindowRect (hwndParent, &rectParent;);
// Calculate the width and height of the parent
int nParentWidth = rectParent.right -
rectParent.left;
int nParentHeight = rectParent.bottom -
rectParent.top;
// Get window rect of the dialog
RECT rectDlg;
::GetWindowRect (hwndDlg, &rectDlg;);
// Calculate the width and height of the dialog
int nDlgWidth = rectDlg.right - rectDlg.left;
int nDlgHeight = rectDlg.bottom -
rectDlg.top;
// Calculate the center of the dialog
// Note: Subtracting the dialogs calculated center from the its parent
// left and top makes sure that the dialog box is centered even when
// the parent is smaller in dimension as compared to the dialog.
rectDlg.left = rectParent.left - ((nDlgWidth / 2) - (nParentWidth / 2));
rectDlg.top = rectParent.top - ((nDlgHeight / 2) - (nParentHeight / 2));
// Get the width and height of the screen
int nScreenWidth = ::GetSystemMetrics (SM_CXSCREEN);
int nScreenHeight = ::GetSystemMetrics (SM_CYSCREEN);
// Calculate dialog's left and top
int nDlgLeft = rectDlg.left < 0 ? 0 :
rectDlg.left;
int nDlgTop = rectDlg.top < 0 ? 0 :
rectDlg.top;
// If dialog's right goes beyond the right
boundary of the screen,
// subtract the amount by which it goes beyond from the dialog's left.
int nDlgRight = rectDlg.left +
nDlgWidth;
if (nDlgRight > nScreenWidth)
nDlgLeft -= (nDlgRight - nScreenWidth);
// If dialog's bottom goes beyond the bottom
boundary of the screen,
// subtract the amount by which it goes beyond from the dialog's right.
int nDlgBottom = rectDlg.top + nDlgHeight;
if (nDlgBottom > nScreenHeight)
nDlgTop -= (nDlgBottom - nScreenHeight);
// Move window to center
::MoveWindow (hwndDlg, nDlgLeft, nDlgTop, nDlgWidth,
nDlgHeight, TRUE);
} |
Using the Source Code
To use the function in your Win32 SDK or MFC
projects, cut and paste the function to a file where you normally keep all
your helper functions. This file is typically included in all other
files. The best place to use this function in Win32 SDK is in your
WM_INITDIALOG handler. In MFC, it is off-course in "OnInitDialog".
"CenterDlgToParent" is the usually the first function in these handlers.
|
| | | Other 11 submission(s) by this author
| | | Report Bad Submission | | | Your Vote! |
See Voting Log | | Other User Comments | There are no comments on this submission. | | Add Your Feedback! | Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. | | |