How do you center a window?

This is a discussion on How do you center a window? within the Windows Programming forums, part of the Platform Specific Boards category; What CW_ command do you use to make the window centered in the screen? Thanks....

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question How do you center a window?

    What CW_ command do you use to make the window centered in the screen? Thanks.

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Assuming you're using the Win32 API, the following function function might do the trick.

    Code:
    /*
     *  Centers a window in the screen.
     */
    void CenterWindowInScreen( HWND hwnd )
    {
      if ( !IsWindow(hwnd) ) return;
    
      RECT rcWindow;
      GetWindowRect(hwnd,&rcWindow);
      int width   = rcWindow.right - rcWindow.left;
      int height  = rcWindow.bottom - rcWindow.top;
      int centerX =( GetSystemMetrics(SM_CXSCREEN) - width)/2;
      int centerY =( GetSystemMetrics(SM_CYSCREEN) - height)/2;
      SetWindowPos(hwnd,HWND_TOP,centerX,centerY,0,0,
    SWP_NOSIZE|SWP_NOZORDER);
    }
    Last edited by Dante Shamest; 05-25-2005 at 04:27 PM.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Quote Originally Posted by My Compiler
    100 C:\Dev-Cpp\print1.cpp `SWP' undeclared (first use this function)
    Am I missing a header file or somthing?

  4. #4
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    My bad. It's a typo. Fixed.

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Okay, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 08:14 AM
  2. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 09:23 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 01:34 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21