public void CenterToScreen(bool useWorkArea = true) { var monitor = User32Methods.MonitorFromWindow(this.Handle, MonitorFlag.MONITOR_DEFAULTTONEAREST); MonitorInfo monitorInfo; User32Helpers.GetMonitorInfo(monitor, out monitorInfo); var screenRect = useWorkArea ? monitorInfo.WorkRect : monitorInfo.MonitorRect; var midX = screenRect.Width / 2; var midY = screenRect.Height / 2; var size = this.GetWindowSize(); this.SetPosition(midX - size.Width / 2, midY - size.Height / 2); }
private void ToggleFullScreen() { if (!isFullScreen) { isMaximized = User32Methods.IsZoomed(Handle); //if (isMaximized) // SetState(ShowWindowCommands.SW_RESTORE); windowStyles = GetStyles(); windowExStyles = GetExStyles(); GetWindowRect(out windowRect); SetStyle(windowStyles & ~(WindowStyles.WS_CAPTION | WindowStyles.WS_THICKFRAME)); SetExStyles(windowExStyles & ~(WindowExStyles.WS_EX_DLGMODALFRAME | WindowExStyles.WS_EX_WINDOWEDGE | WindowExStyles.WS_EX_CLIENTEDGE | WindowExStyles.WS_EX_STATICEDGE)); IntPtr monitor = User32Methods.MonitorFromWindow(Handle, MonitorFlag.MONITOR_DEFAULTTONEAREST); User32Helpers.GetMonitorInfo(monitor, out MonitorInfo monitorInfo); SetPosition(monitorInfo.MonitorRect, WindowPositionFlags.SWP_NOZORDER | WindowPositionFlags.SWP_NOACTIVATE | WindowPositionFlags.SWP_FRAMECHANGED); } else { SetStyle(windowStyles); SetExStyles(windowExStyles); SetPosition(windowRect, WindowPositionFlags.SWP_NOZORDER | WindowPositionFlags.SWP_NOACTIVATE | WindowPositionFlags.SWP_FRAMECHANGED); if (isMaximized) { SetState(ShowWindowCommands.SW_MAXIMIZE); } } isFullScreen = !isFullScreen; ResetTransformation(); }
/// <summary> /// The center to screen. /// </summary> /// <param name="useWorkArea"> /// The use work area. /// </param> public void CenterToScreen(bool useWorkArea = true) { var monitor = User32Methods.MonitorFromWindow(Handle, MonitorFlag.MONITOR_DEFAULTTONEAREST); User32Helpers.GetMonitorInfo(monitor, out var monitorInfo); var screenRect = useWorkArea ? monitorInfo.WorkRect : monitorInfo.MonitorRect; var midX = screenRect.Width / 2; var midY = screenRect.Height / 2; var size = GetWindowSize(); var left = midX - (size.Width / 2); var top = midY - (size.Height / 2); User32Methods.SetWindowPos( Handle, IntPtr.Zero, left, top, -1, -1, WindowPositionFlags.SWP_NOACTIVATE | WindowPositionFlags.SWP_NOSIZE | WindowPositionFlags.SWP_NOZORDER); }