private int WndProc(IntPtr hWnd, uint msg, int wParam, IntPtr lParam) { var message = (WindowsMessage)msg; if (closing) { return(0); } if (msg == (uint)WindowsMessage.DESTROY) { closing = true; } if (hWnd == hwndPopupForm) { var hwndForeground = ControlExtensions.GetForegroundWindow(); var rect = this.Bounds; var point = rect.Location; IntPtr hwndPoint; point.Offset(5, 5); hwndPoint = ControlExtensions.WindowFromPoint(point); if (hwndPoint != hwndPopupForm && autoSetPosition) { offPointCount++; if (offPointCount > 100) { if (!allowMove) { SetPosition(); offPointCount = 0; } } } if (message == WindowsMessage.MOUSEACTIVATE) { OnActivated(EventArgs.Empty); } return(CallWindowProc(prevMsgBoxWndFunc, hWnd, msg, wParam, lParam)); } else if (hWnd == hwndMain) { int result; if (!moved && message.IsOneOf(WindowsMessage.SIZE, WindowsMessage.SIZING)) { if (autoSetPosition) { SetPosition(); } } result = CallWindowProc(prevMainWndFunc, hWnd, msg, wParam, lParam); if (message == WindowsMessage.ACTIVATEAPP) { this.BeginInvoke(() => { var focus = this.GetFocus(); if (Application.OpenForms.Cast <Form>().SelectMany(f => f.GetAllControls()).Any(c => c == focus)) { if (!this.GetAllControls().Any(c => c == focus)) { OnDeactivate(EventArgs.Empty); } } }); } if (message == WindowsMessage.MOUSEACTIVATE) { this.BeginInvoke(() => { var focus = this.GetFocus(); if (!this.GetAllControls().Any(c => c == focus)) { OnDeactivate(EventArgs.Empty); } }); } //else if (message == WindowsMessage.PARENTNOTIFY) //{ // OnDeactivate(EventArgs.Empty); //} return(result); } return(1); }