Click here to Skip to main content
Licence 
First Posted 7 Aug 2000
Views 79,813
Bookmarked 21 times

A Better CenterWindow() Function

By | 3 Oct 2000 | Article
This is a good replacement for CWnd::CenterWindow() that works.
  • Download demo project - 42.2 Kb
  • Sample Image - CenterAnywhere1.gif
    Figure 1. The main CenterSample sample program screen.

    Introduction

    Centering windows on the screen is something which you can normally do with the CWnd::CenterWindow() function in MFC. CenterWindow() takes a pointer to a CWnd as its argument, and supposedly the function will center the window on which it's called against the window to which you pass it a pointer:

    pDlg->CenterWindow(AfxGetMainWnd()); // Centers pDlg against the main window?
    Listing 1. Demonstrating a use of CWnd::CenterWindow() to center a dialog.

    However, a question posed to the MFC Mailing List recently asked, "I have a dialog-based program, where the user can click a button and have a sub-dialog pop up. If I call CWnd::CenterWindow() in the sub-dialog's OnInitDialog() handler, the dialog will always be centered in the center of the screen, not centered with respect to the main dialog. How do I do this?"

    So I came up with a "Brute Force" centering function which actually works better than CWnd::CenterWindow(). It's called CSubDialog::CenterWindowOnOwner(), and I added to my sample program's sub-dialog's class, CSubDialog:

    void CSubDialog::CenterWindowOnOwner(CWnd* pWndToCenterOn)
    {
        // Get the client rectangle of the window on which we want to center
        // Make sure the pointer is not NULL first
    
        if (pWndToCenterOn == NULL)
            return;
     
        CRect rectToCenterOn;
        pWndToCenterOn->GetWindowRect(&rectToCenterOn);
    
        // Get this window's area
        CRect rectSubDialog;
        GetWindowRect(&rectSubDialog);
    
        // Now rectWndToCenterOn contains the screen rectangle of the window 
        // pointed to by pWndToCenterOn.  Next, we apply the same centering 
        // algorithm as does CenterWindow()
    
        // find the upper left of where we should center to
    
        int xLeft = (rectToCenterOn.left + rectToCenterOn.right) / 2 - 
            rectSubDialog.Width() / 2;
        int yTop = (rectToCenterOn.top + rectToCenterOn.bottom) / 2 - 
            rectSubDialog.Height() / 2;
    
        // Move the window to the correct coordinates with SetWindowPos()
        SetWindowPos(NULL, xLeft, yTop, -1, -1,
            SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
    }
    Listing 2. Our brute-force CenterWindowOnOwner() function.

    Then I added code to CSubDialog::OnInitDialog() to center it with respect to the main dialog, which is the main window of the application:

    BOOL CSubDialog::OnInitDialog()
    {
        CDialog::OnInitDialog();
    
        ...
    
        CWnd* pMainWnd = AfxGetMainWnd();
        CenterWindowOnOwner(pMainWnd);
    
        return TRUE;
    }
    Listing 3.How to call CenterWindowOnOwner().

    And voila! The sub-dialog will always center itself on the main dialog (or main application window), no matter where on the screen that window is placed.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Brian C Hart

    Software Developer (Senior)
    Corrugated Technologies, Inc.
    United States United States

    Member

    Follow on Twitter Follow on Twitter
    From Fridley, Minnesota and I like computer programming! When I got started, I was working mostly with Windows GUI programming in C/C++. Then later on I worked with COM/DCOM for a school internship. I used COM/DCOM to write an ad hoc cluster server and job-running environment for a cluster of 24 Windows-based high-end visualization workstations. I moved on to C# and have been working in C# and Windows Forms ever since. I have yet to embrace Silverlight Smile | :)

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    GeneralTruely multi-monitor aware CenterWindow code Pinmemberryltsov0:25 20 Jun '08  
    Generalcentering data inside a CMDIChildWnd PinsussAnonymous20:33 18 Sep '04  
    GeneralMultiple-monitor support PinmemberLeo Davidson5:25 20 Jun '01  
    GeneralRe: Multiple-monitor support PinmemberLeo Davidson5:31 20 Jun '01  
    GeneralRe: Multiple-monitor support Pinmemberandi payn12:15 1 Oct '01  
    GeneralRe: Multiple-monitor support Pinmember_Stilgar_9:14 1 Mar '07  
    Generalbulletproof CenterWindow function for multi-monitors PinmemberAlfie Weinie13:38 13 Feb '01  
    GeneralRe: bulletproof CenterWindow function for multi-monitors Pinmemberyelena22s13:58 15 Aug '05  
    GeneralSuggestion PinsussDominic I. Holmes7:40 9 Oct '00  
    GeneralCenter Form on CFormView PinsussGfw11:41 7 Oct '00  
    GeneralRe: Center Form on CFormView PinmemberVirender Sandhu16:39 14 Nov '02  

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Mobile
    Web04 | 2.5.120517.1 | Last Updated 4 Oct 2000
    Article Copyright 2000 by Brian C Hart
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid