Thread: Centering a Window

  1. #1
    Unregistered
    Guest

    Lightbulb Centering a Window

    In CreateWindowEx, is there a way to automatically center the window to the screen no matter what resolution of the machine?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    The fifth & sixth params that most people pass CW_USEDEFAULT are to set the position of top left part of the screen.


    I guess you can use a few calls to GetSystemMetrics() to work out the size of the screen......then look at the size of your window (set in the seventh & eighth params), work out the sizeing....and pass the correct params to CreateWindowEx()

  3. #3
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Here's what I use:

    Code:
    CreateWindowEx(0,WNDCLS,WNDTTL,WS_BORDER | 
    WS_SYSMENU | WS_VISIBLE,
    GetSystemMetrics(SM_CXSCREEN)/2-160,
    GetSystemMetrics(SM_CYSCREEN)/2-120,
    320,240,NULL,NULL,hInstMain,NULL)
    Just expounding on what Fordy was saying. The param's that are in bold get the system width and height (respectively) then divide each by 2 (center of the screen) then subtract half the width/height of the window.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    There is a dialog style

    DS_CENTER

    Should work in windows as well. Will center in parents window. If no parent then center on main screen.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    [code]
    int width = 100; //...now you just change this for each program...
    int height = 100; //...ditto...

    int screenX = GetSystemMetrics(SM_CYSCREEN);
    int screenY = GetSystemMetrics(SM_CXSCREEN);

    int xCtr = (screenX / 2) - (width / 2);
    int yCtr = (screenY/ 2) - (height / 2);


    CreateWindowEx(0,WNDCLS,WNDTTL,WS_BORDER | WS_SYSMENU | WS_VISIBLE,
    xCtr,
    yCtr,
    width,
    height,
    NULL,NULL,hInstMain,NULL)
    [code]

  6. #6
    Unregistered
    Guest
    I always prefered:

    Code:
    /* Function CenterWindow(), Centers Window */
    VOID CenterWindow(HWND hwnd, HWND hwndParent, int Width, int Height)
    {
    
    	/* Variables */
    	RECT rc;
    
    	/* If Parent Window Is Set As Null, Get The Desktop Window */
    	if(hwndParent == NULL)
    	{
    
    		hwndParent = GetDesktopWindow();
    
    	}
    
    	/* Get Parent Client Area Measurements */
    	GetClientRect(hwndParent, &rc);
    
    	/* Center The Window */
    	MoveWindow(
    		hwnd,
    		(rc.right - rc.left - Width) / 2,
    		(rc.bottom - rc.top - Height) / 2,
    		Width,
    		Height,
    		TRUE
    	);
    
    	return;
    
    }
    Usage for a main window 800 x 600 would be:

    CenterWindow(hwnd, NULL, 800, 600);

    Usage for a child window 400 x 400 would be:

    CenterWindow(hwndChild, hwndParent, 400, 400);

    Hope it helps.

  7. #7
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    (of course I came into the conversation too late )

    Yes, there are logical ways to center windows. You just have to work with what your mama gave you...
    1978 Silver Anniversary Corvette

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, 08: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, 12:34 PM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM