設定WPF窗體全屏顯示:
阿新 • • 發佈:2022-05-02
//全屏程式碼: private void Window_Loaded(object sender, RoutedEventArgs e) { // 設定全屏 this.WindowState = System.Windows.WindowState.Normal; this.WindowStyle = System.Windows.WindowStyle.None; this.ResizeMode = System.Windows.ResizeMode.NoResize; this.Topmost = true; this.Left = 0.0; this.Top = 0.0; this.Width = System.Windows.SystemParameters.PrimaryScreenWidth; this.Height = System.Windows.SystemParameters.PrimaryScreenHeight; }
擴充套件類:
專案先引用下面dll:
1:System.Windows.Forms; 2:System.Drawing;
public static class ExpendMethod { private static Window _fullWindow; private static WindowState _windowState; private static WindowStyle _windowStyle; private static bool _windowTopMost; private static ResizeMode _windowResizeMode; private static Rect _windowRect; /// <summary> /// 進入全屏 /// </summary> /// <param name="window"></param> public static void GoFullscreen(this Window window) { //已經是全屏 if (window.IsFullscreen()) return; //儲存窗體資訊 _windowState = window.WindowState; _windowStyle = window.WindowStyle; _windowTopMost = window.Topmost; _windowResizeMode = window.ResizeMode; _windowRect.X = window.Left; _windowRect.Y = window.Top; _windowRect.Width = window.Width; _windowRect.Height = window.Height; //變成無邊窗體 window.WindowState = WindowState.Normal;//假如已經是Maximized,就不能進入全屏,所以這裡先調整狀態 window.WindowStyle = WindowStyle.None; window.ResizeMode = ResizeMode.NoResize; window.Topmost = true;//最大化後總是在最上面 //獲取視窗控制代碼 var handle = new WindowInteropHelper(window).Handle; //獲取當前顯示器螢幕 Screen screen = Screen.FromHandle(handle); //調整視窗最大化,全屏的關鍵程式碼就是下面3句 window.MaxWidth = screen.Bounds.Width; window.MaxHeight = screen.Bounds.Height; window.WindowState = WindowState.Maximized; //解決切換應用程式的問題 window.Activated += new EventHandler(window_Activated); window.Deactivated += new EventHandler(window_Deactivated); //記住成功最大化的窗體 _fullWindow = window; } static void window_Deactivated(object sender, EventArgs e) { var window = sender as Window; window.Topmost = false; } static void window_Activated(object sender, EventArgs e) { var window = sender as Window; window.Topmost = true; } /// <summary> /// 退出全屏 /// </summary> /// <param name="window"></param> public static void ExitFullscreen(this Window window) { //已經不是全屏無操作 if (!window.IsFullscreen()) return; //恢復視窗先前資訊,這樣就退出了全屏 window.Topmost = _windowTopMost; window.WindowStyle = _windowStyle; window.ResizeMode = ResizeMode.CanResize;//設定為可調整窗體大小 window.Left = _windowRect.Left; window.Width = _windowRect.Width; window.Top = _windowRect.Top; window.Height = _windowRect.Height; window.WindowState = _windowState;//恢復視窗狀態資訊 window.ResizeMode = _windowResizeMode;//恢復視窗可調整資訊 //移除不需要的事件 window.Activated -= window_Activated; window.Deactivated -= window_Deactivated; _fullWindow = null; } /// <summary> /// 窗體是否在全屏狀態 /// </summary> /// <param name="window"></param> /// <returns></returns> public static bool IsFullscreen(this Window window) { if (window == null) throw new ArgumentNullException("window"); return _fullWindow == window; } }