Win32 Window Position

This is a discussion on Win32 Window Position within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I am having a slight problem trying to centre my Win32 client to the screen. I am aware that ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Win32 Window Position

    Hi,

    I am having a slight problem trying to centre my Win32 client to the screen. I am aware that it is a small piece of code, but I just can't get past that problem. If anyone could help that would be much appreciated.

    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Code:
    BOOL CenterWindow (HWND hwndChild)
    {
    	RECT    rChild,  rWorkArea;
    	int     wChild, hChild;
    	int     xNew, yNew;
    	BOOL    bResult;
    
    	// Get the Height and Width of the child window
    	GetWindowRect (hwndChild, &rChild);
    	wChild = rChild.right - rChild.left;
    	hChild = rChild.bottom - rChild.top;
    
    	// Get the limits of the 'workarea'
    	bResult = SystemParametersInfo(
    		SPI_GETWORKAREA,    // system parameter to query or set
    		sizeof(RECT),
    		&rWorkArea,
    		0);
    	if (!bResult) {
    		rWorkArea.left = rWorkArea.top = 0;
    		rWorkArea.right = GetSystemMetrics(SM_CXSCREEN);
    		rWorkArea.bottom = GetSystemMetrics(SM_CYSCREEN);
    	}
    
    	// Calculate new X position, then adjust for workarea
    	xNew = (rWorkArea.right /2) - wChild/2;
    
    	// Calculate new Y position, then adjust for workarea
    	yNew = (rWorkArea.bottom/2) - hChild/2;
    
    	// Set it, and return
    	return SetWindowPos (hwndChild, NULL, xNew, yNew, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
    }

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    See rather the CenterWindow() finction from MSDN

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    Thanks for the code. Its much appreciated.

    Which part of the code will I place it?

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Quote Originally Posted by AMcIntyre View Post
    Thanks for the code. Its much appreciated.

    Which part of the code will I place it?
    You would call CenterWindow right after the window is created. For instance, after calling CreateWindow or CreateWindowEx. If it's a dialog window, you would call CenterWindow in WM_INITDIALOG.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,262
    Technically that code centers a window in the desktop area, not on a screen. This means that in a two monitor setup it would end up cut in half at the adjoining edge of each screen, which can be pretty annoying.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    GetWindowRect and SystemParameters info only give the dimensions of the primary display. If you wish to center the window on the secondary display or possibly split the window up between the primary and secondary display, you will have to write additional code to get the virtual screen dimensions. The OP in this thread had a problem with only being able to display a screensaver window on the primary display. Eventually he realized that he would have to use MonitorInfoEx to determine the virtual screen size in order to cover both displays with his screensaver window.

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    You can also use the undocumented DS_CENTER style on dialogs (in MSVC at least).
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 07:14 AM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. win32 apps compiled with GCC start console window!
    By Citrus538 in forum Windows Programming
    Replies: 5
    Last Post: 02-18-2002, 09:35 PM

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