-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeMethods.cs
More file actions
92 lines (80 loc) · 3.1 KB
/
NativeMethods.cs
File metadata and controls
92 lines (80 loc) · 3.1 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
using System;
using System.Runtime.InteropServices;
namespace BluetoothSafetyLock
{
public static partial class NativeMethods
{
[DllImport("user32.dll", SetLastError = true)]
public static extern bool LockWorkStation();
[DllImport("user32.dll", SetLastError = true)]
public static extern bool OpenClipboard(IntPtr hWndNewOwner);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool EmptyClipboard();
[DllImport("user32.dll", SetLastError = true)]
public static extern bool CloseClipboard();
[DllImport("user32.dll", SetLastError = true)]
public static extern bool DestroyIcon(IntPtr hIcon);
[DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
public const int MOUSEEVENTF_MOVE = 0x01;
public static void WakeScreen()
{
// Small mouse move to wake screen
mouse_event(MOUSEEVENTF_MOVE, 0, 1, 0, 0);
mouse_event(MOUSEEVENTF_MOVE, 0, -1, 0, 0);
}
public static void ClearClipboard()
{
if (OpenClipboard(IntPtr.Zero))
{
EmptyClipboard();
CloseClipboard();
}
}
public static bool IsWindowsInDarkMode()
{
try
{
using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"))
{
if (key != null)
{
var val = key.GetValue("AppsUseLightTheme");
if (val is int i) return i == 0;
}
}
}
catch { }
return true; // Default to dark if we can't read registry
}
public static bool IsInStartup()
{
try {
using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", false)) {
if (key != null) {
var val = key.GetValue("BluetoothSafetyLock");
return val != null;
}
}
} catch { }
return false;
}
public static void SetStartup(bool enable)
{
try {
using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true)) {
if (key != null) {
if (enable) {
string? exePath = System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName;
if (exePath != null) {
key.SetValue("BluetoothSafetyLock", $"\"{exePath}\"");
}
} else {
key.DeleteValue("BluetoothSafetyLock", false);
}
}
}
} catch { }
}
}
}