private IntPtr DialogHookProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode < 0) { return(Win32Native.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)); } var msg = (Win32Native.CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(Win32Native.CWPRETSTRUCT)); if (msg.message == (int)Win32Native.CbtHookAction.HCBT_ACTIVATE) { try { CenterWindow(msg.hwnd); } finally { Win32Native.UnhookWindowsHookEx(_hHook); _hHook = IntPtr.Zero; } } return(Win32Native.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)); }