-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
204 lines (174 loc) · 6.06 KB
/
MainWindow.xaml.cs
File metadata and controls
204 lines (174 loc) · 6.06 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace MicroTimer;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
// P/Invoke declarations for getting display refresh rate
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll")]
private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
[System.Runtime.InteropServices.DllImport("imm32.dll")]
public static extern IntPtr ImmAssociateContext(IntPtr hWnd, IntPtr hIMC);
private const int VREFRESH = 116;
private bool _spaceKeyIsDown = false;
private bool _mouseLeftIsDown = false;
public MainWindow()
{
InitializeComponent();
// 配置高刷新率显示器支持
ConfigureHighRefreshRate();
this.DataContext = Handler.Instance;
// 设置窗口属性以支持高刷新率
this.Loaded += MainWindow_Loaded;
// 设置窗口为焦点,以便接收键盘事件
this.Focusable = true;
this.KeyDown += MainWindow_KeyDown;
this.KeyUp += MainWindow_KeyUp;
this.MouseLeftButtonDown += MainWindow_MouseLeftButtonDown;
this.MouseLeftButtonUp += MainWindow_MouseLeftButtonUp;
this.MouseRightButtonUp += Window_MouseRightButtonUp;
this.MouseDown += MainWindow_MouseDown;
// 确保窗口获得焦点
this.Activated += (sender, e) => this.Focus();
this.SourceInitialized += MainWindow_SourceInitialized;
this.Activated += (sender, e) => DisableInputMethod();
}
private void MainWindow_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Space)
{
e.Handled = true;
if (!_spaceKeyIsDown)
{
_spaceKeyIsDown = true;
Handler.Instance?.SingleAction();
}
}
else if (e.Key == Key.R)
{
e.Handled = true;
Handler.Instance?.ResetAction();
}
}
private void MainWindow_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Space)
{
e.Handled = true;
_spaceKeyIsDown = false;
}
}
private void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!_mouseLeftIsDown)
{
_mouseLeftIsDown = true;
Handler.Instance?.SingleAction();
}
}
private void MainWindow_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_mouseLeftIsDown = false;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// 窗口加载完成后进行最终配置
OptimizeForHighRefreshRate();
// 确保窗口获得焦点
this.Focus();
}
private void ConfigureHighRefreshRate()
{
// 设置渲染选项
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
// 设置合成模式以支持高刷新率
this.UseLayoutRounding = true;
this.SnapsToDevicePixels = true;
}
private void OptimizeForHighRefreshRate()
{
// 获取显示器刷新率信息
var refreshRate = GetDisplayRefreshRate();
Handler.Instance.SetRefreshRate(refreshRate);
string titleZh = $"MicroTimer - 当前刷新率: {refreshRate}Hz - 精度{(1000.0 / refreshRate).ToString("F2")}ms - 空格/左键: 开始/暂停 - R/鼠标中键: 重置 - 右键: 放大毫秒";
string titleEn = $"MicroTimer - Refresh Rate: {refreshRate}Hz - Precision {(1000.0 / refreshRate).ToString("F2")}ms - Space/LeftClick: Start/Pause - R/MiddleClick: Reset - RightClick: Enlarge ms";
var culture = CultureInfo.CurrentUICulture;
if (culture.Name.StartsWith("zh", StringComparison.OrdinalIgnoreCase))
{
this.Title = titleZh;
}
else
{
this.Title = titleEn;
}
// 根据刷新率调整渲染设置
if (refreshRate >= 120) // 高刷新率显示器
{
// 启用硬件加速
RenderOptions.SetCachingHint(this, CachingHint.Cache);
// 设置动画帧率
Timeline.DesiredFrameRateProperty.OverrideMetadata(
typeof(Timeline),
new FrameworkPropertyMetadata(refreshRate));
}
}
private int GetDisplayRefreshRate()
{
try
{
// 使用Windows API获取显示器刷新率
IntPtr hdc = GetDC(IntPtr.Zero);
if (hdc != IntPtr.Zero)
{
int refreshRate = GetDeviceCaps(hdc, VREFRESH);
ReleaseDC(IntPtr.Zero, hdc);
return refreshRate;
}
// 备用方法:使用Windows Forms
var screen = System.Windows.Forms.Screen.PrimaryScreen;
return screen?.Bounds.Width > 0 ? 60 : 60;
}
catch
{
return 60; // 默认值
}
}
protected override void OnClosed(EventArgs e)
{
// 清理资源
Handler.Instance.Dispose();
base.OnClosed(e);
}
private void Window_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
Handler.Instance.SwapText();
}
private void MainWindow_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Middle)
{
Handler.Instance.ResetAction();
}
}
private void MainWindow_SourceInitialized(object? sender, EventArgs e)
{
DisableInputMethod();
}
private void DisableInputMethod()
{
var hwnd = new WindowInteropHelper(this).Handle;
ImmAssociateContext(hwnd, IntPtr.Zero);
}
}