private void WmSysCommand(IntPtr hwnd, IntPtr wparam) { var scWparam = (int)wparam & 65520; _lastScwParam = scWparam; _lastState = WindowState; if (scWparam == 61456) { NativeMethods.RedrawWindow(hwnd, IntPtr.Zero, IntPtr.Zero, RedrawWindowFlags.Invalidate | RedrawWindowFlags.NoChildren | RedrawWindowFlags.UpdateNow | RedrawWindowFlags.Frame); } if (_wasMaximized) { WindowStyle = WindowStyle.None; _wasMaximized = false; return; } if (scWparam == 61472 || scWparam == 61728) { WindowStyle = WindowStyle.SingleBorderWindow; } else { WindowStyle = WindowStyle.None; } if (scWparam == 61472 && WindowState == WindowState.Maximized) { _wasMaximized = true; } if ((scWparam == 61488 || scWparam == 61472 || (scWparam == 61456 || scWparam == 61440)) && (WindowState == WindowState.Normal && !IsAeroSnappedToMonitor(hwnd))) { _logicalSizeForRestore = new Rect(Left, Top, Width, Height); } if (scWparam == 61456 && WindowState == WindowState.Maximized && _logicalSizeForRestore == Rect.Empty) { _logicalSizeForRestore = new Rect(Left, Top, Width, Height); } if (scWparam != 61728 || WindowState == WindowState.Minimized || (_logicalSizeForRestore.Width <= 0.0 || _logicalSizeForRestore.Height <= 0.0)) { return; } Left = _logicalSizeForRestore.Left; Top = _logicalSizeForRestore.Top; Width = _logicalSizeForRestore.Width; Height = _logicalSizeForRestore.Height; }