-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
171 lines (143 loc) · 5.04 KB
/
MainWindow.xaml.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
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
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using System.Printing;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Threading;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.Input.KeyboardAndMouse;
namespace SystemTimeModifyTool;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
private int _duration = 2;
public MainWindow()
{
CheckWindows();
RequireAdministrator();
InitializeComponent();
Init();
}
private static void RequireAdministrator()
{
if (new WindowsPrincipal(WindowsIdentity.GetCurrent())
.IsInRole(WindowsBuiltInRole.Administrator)) return;
try
{
var processStartInfo = new ProcessStartInfo
{
UseShellExecute = true,
WorkingDirectory = Environment.CurrentDirectory,
FileName = Process.GetCurrentProcess().MainModule?.FileName,
Verb = "RunAs"
};
Process.Start(
processStartInfo);
}
catch (Exception exception)
{
MessageBox.Show(exception.ToString());
}
Application.Current.Shutdown();
}
private static void CheckWindows()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return;
MessageBox.Show("仅支持Windows", "仅支持Windows", MessageBoxButton.OK);
Application.Current.Shutdown();
}
private async void Init()
{
SystemTime.Text = DateTime.Now.ToString("G");
NetworkTime.Text = (await GetNetworkTime()).ToString(CultureInfo.CurrentCulture);
}
private void DurationChange(object sender, RoutedEventArgs e)
{
var stringDuration = ((RadioButton)sender).Content.ToString()?.Split("小时每次")[0];
_duration = int.Parse(stringDuration ?? "2");
DurationDisplay.Text = _duration.ToString();
}
private void MainWindow_OnClosing(object? sender, CancelEventArgs e)
{
var handle = new WindowInteropHelper(this).Handle;
PInvoke.UnregisterHotKey(new HWND(handle), 1);
PInvoke.UnregisterHotKey(new HWND(handle), 2);
PInvoke.UnregisterHotKey(new HWND(handle), 3);
}
private void MainWindow_OnSourceInitialized(object? sender, EventArgs e)
{
var handle = new WindowInteropHelper(this).Handle;
var source = HwndSource.FromHwnd(handle);
source?.AddHook(WndProc);
var hWnd = new HWND(handle);
PInvoke.RegisterHotKey(hWnd, 1, HOT_KEY_MODIFIERS.MOD_CONTROL, (uint)KeyInterop.VirtualKeyFromKey(Key.Q));
PInvoke.RegisterHotKey(hWnd, 2, HOT_KEY_MODIFIERS.MOD_CONTROL, (uint)KeyInterop.VirtualKeyFromKey(Key.A));
PInvoke.RegisterHotKey(hWnd, 3, HOT_KEY_MODIFIERS.MOD_CONTROL, (uint)KeyInterop.VirtualKeyFromKey(Key.S));
}
private static readonly HttpClient SharedClient = new()
{
BaseAddress = new Uri("https://www.baidu.com/"),
DefaultRequestHeaders =
{
Accept = { new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json) }
}
};
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
{
switch (msg)
{
case (int)PInvoke.WM_HOTKEY:
switch (wparam.ToInt32())
{
case 1:
SetSystemTime(DateTime.Now.AddHours(_duration));
break;
case 2:
SetSystemTime(DateTime.Now.AddHours(-_duration));
break;
case 3:
SyncTime();
break;
}
break;
}
return IntPtr.Zero;
}
private void SetSystemTime(DateTime dateTime)
{
PInvoke.SetLocalTime(new SYSTEMTIME()
{
wYear = (ushort)dateTime.Year,
wMonth = (ushort)dateTime.Month,
wDay = (ushort)dateTime.Day,
wHour = (ushort)dateTime.Hour,
wMinute = (ushort)dateTime.Minute,
wSecond = (ushort)dateTime.Second,
});
SystemTime.Text = dateTime.ToString(CultureInfo.CurrentCulture);
}
private async void SyncTime()
{
var dateTime = await GetNetworkTime();
NetworkTime.Text = dateTime.ToString(CultureInfo.CurrentCulture);
SetSystemTime(dateTime);
}
private static async Task<DateTime> GetNetworkTime()
{
var async = await SharedClient.GetAsync("");
if (async.Headers.Date == null) throw new Exception("网络时间获取失败");
var dateTime = async.Headers.Date.GetValueOrDefault().DateTime.ToLocalTime();
return dateTime;
}
}