// Boilerplate window-centering code. // Split out of HookCallback() for clarity. private static void CenterWindowOnParent(IntPtr hChildWnd) { // Get child (MessageBox) size Win32.RECT rcChild = new Win32.RECT(); Win32.GetWindowRect(hChildWnd, ref rcChild); int cxChild = rcChild.right - rcChild.left; int cyChild = rcChild.bottom - rcChild.top; // Get parent (Form) size & location IntPtr hParent = Win32.GetParent(hChildWnd); Win32.RECT rcParent = new Win32.RECT(); Win32.GetWindowRect(hParent, ref rcParent); int cxParent = rcParent.right - rcParent.left; int cyParent = rcParent.bottom - rcParent.top; // Center the MessageBox on the Form int x = rcParent.left + (cxParent - cxChild) / 2; int y = rcParent.top + (cyParent - cyChild) / 2; uint uFlags = 0x15; // SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE; Win32.SetWindowPos(hChildWnd, IntPtr.Zero, x, y, 0, 0, uFlags); }