* Centers a window relative to its parent window. * * @param hwndDlg Handle of the window to center. **/
| 11 | * @param hwndDlg Handle of the window to center. |
| 12 | **/ |
| 13 | void CenterWindow(HWND hwndDlg) |
| 14 | { |
| 15 | //TODO: This function should probably moved into the generic Win32 window file |
| 16 | //move the window relative to its parent |
| 17 | HWND hwndParent = GetParent(hwndDlg); |
| 18 | RECT rect; |
| 19 | RECT rectP; |
| 20 | |
| 21 | GetWindowRect(hwndDlg, &rect); |
| 22 | GetWindowRect(hwndParent, &rectP); |
| 23 | |
| 24 | int width = rect.right - rect.left; |
| 25 | int height = rect.bottom - rect.top; |
| 26 | |
| 27 | int x = ((rectP.right-rectP.left) - width) / 2 + rectP.left; |
| 28 | int y = ((rectP.bottom-rectP.top) - height) / 2 + rectP.top; |
| 29 | |
| 30 | int screenwidth = GetSystemMetrics(SM_CXSCREEN); |
| 31 | int screenheight = GetSystemMetrics(SM_CYSCREEN); |
| 32 | |
| 33 | //make sure that the dialog box never moves outside of the screen |
| 34 | if(x < 0) x = 0; |
| 35 | if(y < 0) y = 0; |
| 36 | if(x + width > screenwidth) x = screenwidth - width; |
| 37 | if(y + height > screenheight) y = screenheight - height; |
| 38 | |
| 39 | MoveWindow(hwndDlg, x, y, width, height, FALSE); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Centers a window on the screen. |
no outgoing calls
no test coverage detected