/
FullScreenHelper.cs
124 lines (102 loc) · 3.78 KB
/
FullScreenHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
namespace TranslateApp
{
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.Height = 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;
}
}
}