From 532c6ebb5e4391bf5ab33a66f8e070776e3b59f6 Mon Sep 17 00:00:00 2001 From: noberumotto Date: Mon, 13 Jun 2022 02:52:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=BC=A0=E6=A0=87=E5=9D=90?= =?UTF-8?q?=E6=A0=87=E8=8E=B7=E5=8F=96api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Librarys/Win32API.cs | 56 +++++++++++++++++++++-- Core/Servicers/Instances/Sleepdiscover.cs | 11 ++--- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/Core/Librarys/Win32API.cs b/Core/Librarys/Win32API.cs index af85f8b..6a43b65 100644 --- a/Core/Librarys/Win32API.cs +++ b/Core/Librarys/Win32API.cs @@ -63,8 +63,8 @@ namespace Core.Librarys public const UInt32 PROCESS_QUERY_INFORMATION = 0x400; public const UInt32 PROCESS_VM_READ = 0x010; - private const uint WINEVENT_OUTOFCONTEXT = 0; - private const uint EVENT_SYSTEM_FOREGROUND = 3; + //private const uint WINEVENT_OUTOFCONTEXT = 0; + //private const uint EVENT_SYSTEM_FOREGROUND = 3; public static string UWP_AppName(IntPtr hWnd, uint pID) { WINDOWINFO windowinfo = new WINDOWINFO(); @@ -117,8 +117,26 @@ namespace Core.Librarys /// [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool GetCursorPos(out Point lpPoint); + private static extern bool GetCursorPos(out POINT lpPoint); + [StructLayout(LayoutKind.Sequential)] + public struct POINT + { + public int X; + public int Y; + + public static implicit operator Point(POINT point) + { + return new Point(point.X, point.Y); + } + } + + public static Point GetCursorPosition() + { + POINT lpPoint; + GetCursorPos(out lpPoint); + return lpPoint; + } #region 声音判断 /// @@ -255,5 +273,37 @@ namespace Core.Librarys [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern IntPtr GetModuleHandle(string lpModuleName); #endregion + + #region 获取窗口信息 + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); + + [StructLayout(LayoutKind.Sequential)] + public struct RECT + { + public int Left; // x position of upper-left corner + public int Top; // y position of upper-left corner + public int Right; // x position of lower-right corner + public int Bottom; // y position of lower-right corner + + public int Width + { + get + { + return Right - Left; + } + } + public int Height + { + get + { + return Bottom - Top; + } + } + + } + + #endregion } } diff --git a/Core/Servicers/Instances/Sleepdiscover.cs b/Core/Servicers/Instances/Sleepdiscover.cs index 0220ae2..4a6e937 100644 --- a/Core/Servicers/Instances/Sleepdiscover.cs +++ b/Core/Servicers/Instances/Sleepdiscover.cs @@ -112,8 +112,7 @@ namespace Core.Servicers.Instances { TimeSpan timeSpan = DateTime.Now - pressKeyboardLastTime; - Point point; - Win32API.GetCursorPos(out point); + Point point = Win32API.GetCursorPosition(); if (lastPoint.ToString() == point.ToString() && timeSpan.TotalSeconds > 10) { // 非鼠标或键盘激活 @@ -135,7 +134,7 @@ namespace Core.Servicers.Instances timer.Tick += Timer_Tick; timer.Start(); - Win32API.GetCursorPos(out lastPoint); + lastPoint = Win32API.GetCursorPosition(); playSoundStartTime = DateTime.MinValue; @@ -147,9 +146,7 @@ namespace Core.Servicers.Instances private void Timer_Tick(object sender, EventArgs e) { - Point point; - - Win32API.GetCursorPos(out point); + Point point = Win32API.GetCursorPosition(); if (point.X + point.Y == 0) { @@ -227,8 +224,8 @@ namespace Core.Servicers.Instances } Logger.Info("【当前坐标】status:" + status + ",lastPoint:" + lastPoint.ToString() + ",now point:" + point.ToString()); + lastPoint = point; - //Win32API.GetCursorPos(out lastPoint); } -- GitLab