如果做到仅仅全屏即可,只需在loaded函数中添加如下代码:
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; }
如下文章转自:http://blog.163.com/leingliu@126/blog/static/194762822009524611352/
因为想尝试一下使Wpf应用程序的全屏状态,但是Wpf没有全屏操作的直接实现,在网上搜索一下,发现很多都是使用pinvoke调用win32动态链接库来实现,而且功能也不全,当发现参考文章中的内容时,发现了全屏的简单方法,而且也不用使用Pinvoke,随即修改和完善了功能,代码如下:
using System; using System.Windows; using System.Windows.Forms; using System.Windows.Interop; namespace PhotoViewer { /// <summary> /// Wpf应用程序全屏辅助类 /// /// ——全部使用.Net类进行操作 /// ——可进入全屏和退出全屏 /// ——可恢复到全屏前的状态 /// ——全屏时可切换到其他应用程序 /// /// Author:wowin /// Email:leingliu@126.com /// LastUpdate:2009-6-24 /// </summary> public static class FullScreenHelper { 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; } } }
还有一个不爽的问题是,在最大化时如果已经是最大化状态,必须先退出最大化状态,造成多闪烁一次,目前还没有找到解决方法。