-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathDesktop.cs
More file actions
191 lines (180 loc) · 6.52 KB
/
Copy pathDesktop.cs
File metadata and controls
191 lines (180 loc) · 6.52 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
using System.Media;
using System.Runtime.InteropServices;
using System.Windows;
namespace BytecodeApi.Win32;
/// <summary>
/// Provides a set of <see langword="static" /> methods for Windows desktop interaction.
/// </summary>
public static class Desktop
{
/// <summary>
/// Gets the screen DPI. A value of 96 corresponds to 100% font scaling.
/// </summary>
public static Point Dpi
{
get
{
using System.Drawing.Graphics graphics = System.Drawing.Graphics.FromHwnd(0);
nint desktop = graphics.GetHdc();
return new(Native.GetDeviceCaps(desktop, 88), Native.GetDeviceCaps(desktop, 90));
}
}
/// <summary>
/// Gets the current mouse position in screen coordinates.
/// </summary>
public static Point MousePosition
{
get
{
Point dpi = Dpi;
return new(
System.Windows.Forms.Control.MousePosition.X * 96.0 / dpi.X,
System.Windows.Forms.Control.MousePosition.Y * 96.0 / dpi.Y);
}
}
/// <summary>
/// Gets a <see cref="bool" /> value indicating whether the left mouse button is pressed.
/// </summary>
public static bool IsLeftMouseButtonPressed => Native.GetAsyncKeyState(1) != 0;
/// <summary>
/// Gets a <see cref="bool" /> value indicating whether the right mouse button is pressed.
/// </summary>
public static bool IsRightMouseButtonPressed => Native.GetAsyncKeyState(2) != 0;
/// <summary>
/// Gets a <see cref="Rect" />[] that represent the bounds of all screens.
/// </summary>
public static Rect[] Screens
{
get
{
Point dpi = Dpi;
return System.Windows.Forms.Screen.AllScreens
.Select(screen => new Rect(
screen.Bounds.X * 96.0 / dpi.X,
screen.Bounds.Y * 96.0 / dpi.Y,
screen.Bounds.Width * 96.0 / dpi.X,
screen.Bounds.Height * 96.0 / dpi.Y))
.ToArray();
}
}
/// <summary>
/// Gets a <see cref="bool" /> value indicating whether the workstation is locked.
/// </summary>
public static bool IsWorkstationLocked
{
get
{
nint desktop = 0;
try
{
desktop = Native.OpenInputDesktop(0, false, 256);
if (desktop == 0)
{
desktop = Native.OpenDesktop("Default", 0, false, 256);
}
return desktop != 0 ? !Native.SwitchDesktop(desktop) : throw Throw.Win32();
}
finally
{
if (desktop != 0) Native.CloseDesktop(desktop);
}
}
}
/// <summary>
/// Gets a <see cref="bool" /> value indicating whether the screensaver is running.
/// </summary>
public static bool IsScreensaverRunning
{
get
{
bool running = false;
return Native.SystemParametersInfo(114, 0, ref running, 0) ? running : throw Throw.Win32();
}
}
/// <summary>
/// Plays the <see cref="SystemSounds.Beep" /> sound.
/// </summary>
public static void Beep()
{
Beep(true);
}
/// <summary>
/// Plays the <see cref="SystemSounds.Beep" /> or the <see cref="SystemSounds.Hand" /> sound, depending on the <paramref name="success" /> parameter.
/// </summary>
/// <param name="success"><see langword="true" /> to play <see cref="SystemSounds.Beep" />; <see langword="false" /> to play <see cref="SystemSounds.Hand" />.</param>
public static void Beep(bool success)
{
(success ? SystemSounds.Beep : SystemSounds.Hand).Play();
}
/// <summary>
/// Captures the entire virutal screen and returns a <see cref="System.Drawing.Bitmap" /> with the image.
/// </summary>
/// <param name="allScreens"><see langword="true" /> to capture all screens, <see langword="false" /> to only capture the primary screen.</param>
/// <returns>
/// A <see cref="System.Drawing.Bitmap" /> with the image of the captured screen.
/// </returns>
public static System.Drawing.Bitmap CaptureScreen(bool allScreens)
{
Point dpi = Dpi;
int left = allScreens ? (int)(SystemParameters.VirtualScreenLeft * dpi.X / 96.0) : 0;
int top = allScreens ? (int)(SystemParameters.VirtualScreenTop * dpi.Y / 96.0) : 0;
int width = (int)((allScreens ? SystemParameters.VirtualScreenWidth : SystemParameters.PrimaryScreenWidth) * dpi.X / 96.0);
int height = (int)((allScreens ? SystemParameters.VirtualScreenHeight : SystemParameters.PrimaryScreenHeight) * dpi.Y / 96.0);
System.Drawing.Bitmap bitmap = new(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);
graphics.CopyFromScreen(left, top, 0, 0, bitmap.Size);
return bitmap;
}
/// <summary>
/// Locks the workstation.
/// </summary>
public static void LockWorkstation()
{
Native.LockWorkStation();
}
/// <summary>
/// Turns on the Windows screensaver using a HWND broadcast message, if a screensaver is configured.
/// </summary>
public static void TurnOnScreenSaver()
{
Native.SendMessage(0xffff, 0x112, 0xf140, 0);
}
/// <summary>
/// Changes the Windows wallpaper to an image from the specified file and throws an exception, if changing the wallpaper failed.
/// </summary>
/// <param name="path">A <see cref="string" /> specifying the path to an image file.</param>
public static void ChangeWallpaper(string path)
{
Check.ArgumentNull(path);
Check.FileNotFound(path);
if (Native.SystemParametersInfo(20, 0, path, 1) != 1)
{
throw Throw.Win32();
}
}
}
file static class Native
{
[DllImport("user32.dll", EntryPoint = "OpenDesktopA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern nint OpenDesktop(string desktop, int flags, bool inherit, uint desiredAccess);
[DllImport("user32", SetLastError = true)]
public static extern nint OpenInputDesktop(uint flags, bool inherit, uint desiredAccess);
[DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern nint CloseDesktop(nint desktop);
[DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SwitchDesktop(nint desktop);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo(int action, int param1, string param2, int winIni);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SystemParametersInfo(uint action, uint param1, ref bool param2, int winIni);
[DllImport("user32.dll")]
public static extern void LockWorkStation();
[DllImport("user32.dll")]
public static extern nint SendMessage(nint handle, uint msg, int wParam, int lParam);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int GetDeviceCaps(nint dc, int index);
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(int vKey);
}