XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension.NetCore.InputSimulator

【DLL】XFE各类拓展的模拟键盘输入

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension.NetCore.InputSimulator

add-新增获取屏幕绝对大小和屏幕缩放倍率方法

1fba3a5
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

2 个文件 +43 -13
Modified XFEExtension.NetCore.InputSimulator/InputSimulator.cs +41 -11
@@ -29,6 +29,10 @@ public static partial class InputSimulator
29 29 private static partial int GetDpiForWindow(IntPtr hWnd);
30 30 [LibraryImport("user32.dll")]
31 31 private static partial short GetAsyncKeyState(int vKey);
32 [LibraryImport("user32.dll")]
33 private static partial IntPtr GetDesktopWindow();
34 [LibraryImport("gdi32.dll")]
35 private static partial int GetDeviceCaps(IntPtr hdc, int nIndex);
32 36 #endregion
33 37 #region 常量
34 38 private const int KEYEVENTF_KEYDOWN = 0x0000; // 按键按下
@@ -46,6 +50,12 @@ public static partial class InputSimulator
46 50 private const int VK_MBUTTON = 0x04; // 检测鼠标中键
47 51 private const int SM_CXSCREEN = 0; // 屏幕宽度
48 52 private const int SM_CYSCREEN = 1; // 屏幕高度
53 private const int HORZRES = 8;
54 private const int VERTRES = 10;
55 private const int LOGPIXELSX = 88;
56 private const int LOGPIXELSY = 90;
57 private const int DESKTOPVERTRES = 117;
58 private const int DESKTOPHORZRES = 118;
49 59 #endregion
50 60 #region 键盘方法
51 61 /// <summary>
@@ -199,28 +209,28 @@ public static partial class InputSimulator
199 209 LocateTo(0, 0);
200 210 break;
201 211 case ScreenPosition.TopRight:
202 LocateTo(GetScreenSize().X - 1, 0);
212 LocateTo(GetScreenSize().Width - 1, 0);
203 213 break;
204 214 case ScreenPosition.Top:
205 LocateTo(GetScreenSize().X / 2, 0);
215 LocateTo(GetScreenSize().Width / 2, 0);
206 216 break;
207 217 case ScreenPosition.Left:
208 LocateTo(0, GetScreenSize().Y / 2);
218 LocateTo(0, GetScreenSize().Height / 2);
209 219 break;
210 220 case ScreenPosition.Right:
211 LocateTo(GetScreenSize().X - 1, GetScreenSize().Y / 2);
221 LocateTo(GetScreenSize().Width - 1, GetScreenSize().Height / 2);
212 222 break;
213 223 case ScreenPosition.Center:
214 LocateTo(GetScreenSize().X / 2, GetScreenSize().Y / 2);
224 LocateTo(GetScreenSize().Width / 2, GetScreenSize().Height / 2);
215 225 break;
216 226 case ScreenPosition.BottomLeft:
217 LocateTo(0, GetScreenSize().Y - 1);
227 LocateTo(0, GetScreenSize().Height - 1);
218 228 break;
219 229 case ScreenPosition.Bottom:
220 LocateTo(GetScreenSize().X / 2, GetScreenSize().Y - 1);
230 LocateTo(GetScreenSize().Width / 2, GetScreenSize().Height - 1);
221 231 break;
222 232 case ScreenPosition.BottomRight:
223 LocateTo(GetScreenSize().X - 1, GetScreenSize().Y - 1);
233 LocateTo(GetScreenSize().Width - 1, GetScreenSize().Height - 1);
224 234 break;
225 235 default:
226 236 break;
@@ -304,9 +314,21 @@ public static partial class InputSimulator
304 314 /// 获取屏幕的宽高
305 315 /// </summary>
306 316 /// <returns>屏幕宽高</returns>
307 public static Point GetScreenSize()
317 public static Size GetScreenSize()
318 {
319 return new Size(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
320 }
321 /// <summary>
322 /// 获取屏幕的绝对宽高
323 /// </summary>
324 /// <returns></returns>
325 public static Size GetAbsoluteScreenSize()
308 326 {
309 return new Point(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
327 var hdc = GetDC(GetDesktopWindow());
328 int nWidth = GetDeviceCaps(hdc, DESKTOPHORZRES);
329 int nHeight = GetDeviceCaps(hdc, DESKTOPVERTRES);
330 _ = ReleaseDC(IntPtr.Zero, hdc);
331 return new Size(nWidth, nHeight);
310 332 }
311 333 /// <summary>
312 334 /// 获取鼠标的当前位置
@@ -327,6 +349,14 @@ public static partial class InputSimulator
327 349 return ((double)point.X / (GetSystemMetrics(SM_CXSCREEN) - 1), (double)point.Y / (GetSystemMetrics(SM_CYSCREEN) - 1));
328 350 }
329 351 /// <summary>
352 /// 获取当前缩放倍率
353 /// </summary>
354 /// <returns></returns>
355 public static double GetScalingFactorForWindow()
356 {
357 return GetAbsoluteScreenSize().Width / GetScreenSize().Width;
358 }
359 /// <summary>
330 360 /// 获取当前窗口的缩放倍率
331 361 /// </summary>
332 362 /// <param name="hWnd">窗口句柄</param>
@@ -335,7 +365,7 @@ public static partial class InputSimulator
335 365 {
336 366 var hDC = GetDC(hWnd);
337 367 var dpi = GetDpiForWindow(hWnd);
338 ReleaseDC(hWnd, hDC);
368 _ = ReleaseDC(hWnd, hDC);
339 369 return dpi / 96.0f;
340 370 }
341 371 #endregion
Modified XFEExtension.NetCore.InputSimulator/XFEExtension.NetCore.InputSimulator.csproj +2 -2
@@ -18,10 +18,10 @@
18 18 <PackageReadmeFile>README.md</PackageReadmeFile>
19 19 <RepositoryUrl>https://github.com/XFEstudio/XFEExtension.NetCore.InputSimulator</RepositoryUrl>
20 20 <PackageTags>XFE;拓展;键盘;按键;模拟输入;Input</PackageTags>
21 <PackageReleaseNotes>新增获取鼠标按键是否按下方法</PackageReleaseNotes>
21 <PackageReleaseNotes>新增获取屏幕绝对大小和屏幕缩放倍率方法</PackageReleaseNotes>
22 22 <PackageLicenseFile>LICENSE</PackageLicenseFile>
23 23 <PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
24 <Version>2.2.0</Version>
24 <Version>2.3.0</Version>
25 25 <PackageIcon>logoIcon.png</PackageIcon>
26 26 </PropertyGroup>
27 27