Quick Search for:  in language:    
function,presented,here,takes,handle,dialog,p
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
C/ C++ Stats

 Code: 728,035. lines
 Jobs: 123. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for C/ C++.
String substr() function
By Doug Lorenzen on 3/30


Calculate Pi to 10000 digits
By Feng Zi Qiao on 3/30


Click here to see a screenshot of this code!^A Game that is RGP
By Eric Gilbert Sondraal on 3/29

(Screen Shot)

Click here to see a screenshot of this code!MFC Media Player
By Kim Lindgren on 3/29

(Screen Shot)

Click here to see a screenshot of this code!TurboGUI - A GUI Framework for Turbo C/C++
By Siva Chandran P on 3/29

(Screen Shot)

Click here to see a screenshot of this code!DAM Progress Bars
By Dean McNiven on 3/29

(Screen Shot)

Click here to see a screenshot of this code!David Blain's Card Trick
By Arindam Ray on 3/29

(Screen Shot)

C like basic
By njrm on 3/28


Click here to see a screenshot of this code!a 5 in 1 calculator
By Eric Gilbert Sondraal on 3/27

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Free Magazine
FREE full subscription to InformationWeek Magazine!


InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology. In-depth editorial focuses on the strategic side of technology to help you make decisions that affect your company's bottom line.
Click here for more information on a free full-year subscription!

See other free magazines available


Affiliate Sites

 
 
   

Center a Dialog Box with Respect to its Owner Window

Print
Email
 

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
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor 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.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | C/ C++ Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997-2004 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.