Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / Visual C++ 10.0
Tip/Trick

CenterWindow in WIN32

Rate me:
Please Sign up or sign in to vote.
4.86/5 (11 votes)
2 Oct 2011CPOL 60.1K   8   8
How to center window in WIN32
In MFC, there is CWnd::CenterWindow, but to do this in WIN32, you need the following code:
C++
BOOL CenterWindow(HWND hwndWindow)
{
     HWND hwndParent;
     RECT rectWindow, rectParent;

     // make the window relative to its parent
     if ((hwndParent = GetParent(hwndWindow)) != NULL)
     {
         GetWindowRect(hwndWindow, &rectWindow);
         GetWindowRect(hwndParent, &rectParent);

         int nWidth = rectWindow.right - rectWindow.left;
         int nHeight = rectWindow.bottom - rectWindow.top;

         int nX = ((rectParent.right - rectParent.left) - nWidth) / 2 + rectParent.left;
         int nY = ((rectParent.bottom - rectParent.top) - nHeight) / 2 + rectParent.top;

         int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
         int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);

         // make sure that the dialog box never moves outside of the screen
         if (nX < 0) nX = 0;
         if (nY < 0) nY = 0;
         if (nX + nWidth > nScreenWidth) nX = nScreenWidth - nWidth;
         if (nY + nHeight > nScreenHeight) nY = nScreenHeight - nHeight;

         MoveWindow(hwndWindow, nX, nY, nWidth, nHeight, FALSE);

         return TRUE;
     }

     return FALSE;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer NXP Semiconductors
Romania Romania
My professional background includes knowledge of analyst programmer for Microsoft Visual C++, Microsoft Visual C#, Microsoft Visual Basic, Sun Java, assembly for Intel 80x86 microprocessors, assembly for PIC microcontrollers (produced by Microchip Inc.), relational databases (MySQL, Oracle, SQL Server), concurrent version systems, bug tracking systems, web design (HTML5, CSS3, XML, PHP/MySQL, JavaScript).

Comments and Discussions

 
Questionousfar Pin
ousfarmoha12-Apr-20 5:12
ousfarmoha12-Apr-20 5:12 
GeneralMy vote of 5 Pin
ominion18-Nov-15 6:24
ominion18-Nov-15 6:24 
QuestionDoes not work on Multiscreen either Pin
llothar7-Jun-15 17:42
llothar7-Jun-15 17:42 
GeneralMy vote of 1 Pin
Czarek Tomczak31-Jan-13 16:49
Czarek Tomczak31-Jan-13 16:49 
GeneralReason for my vote of 4 Code is clearly laid out and not dep... Pin
Krosus4-Oct-11 22:52
Krosus4-Oct-11 22:52 
GeneralReason for my vote of 1 Unoptimal, cluttered and clumsy code... Pin
Edwin Wanderbrough1-Oct-11 9:17
Edwin Wanderbrough1-Oct-11 9:17 
GeneralReason for my vote of 1 Not interesting. Not professional po... Pin
Slava200912-Sep-11 20:04
Slava200912-Sep-11 20:04 
QuestionThank you, Cap Pin
Slava200912-Sep-11 20:03
Slava200912-Sep-11 20:03 

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

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