better-genshin-impact/BetterGenshinImpact/Helpers/DpiHelper.cs
辉鸭蛋 a06f0fcdb2 auto pathing: fix not releasing the mouse and keyboard when stopping tasks
更新日志和黑名单

移除 `PathExecutor.cs` 中的无用引用,重构 `Pathing` 方法,添加 `InitializePathing` 和 `ConvertWaypoints` 方法,提取传送点处理逻辑到 `HandleTeleportWaypoint` 方法。修改 `TpTask.cs` 中的日志格式为浮点数格式。将 `TaskRunner.cs` 中的 `FireAndForgetAsync` 方法重命名为 `RunThreadAsync`,并在 `ScriptService.cs` 中相应替换调用。为 `DpiHelper.cs` 中的 `ScaleY` 属性添加注释。更新 `pick_black_lists.json`,添加新的黑名单项 `"摆放巧像"`。在 `MapPathingViewModel.cs` 中添加 `_mapViewer` 字段,并在 `OnOpenMapViewer` 方法中使用。
2024-09-09 21:57:48 +08:00

65 lines
2.0 KiB
C#

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Interop;
using Vanara.PInvoke;
namespace BetterGenshinImpact.Helpers;
public class DpiHelper
{
/// <summary>
/// 只能主线程调用
/// </summary>
public static float ScaleY => GetScaleY();
private static float GetScaleY()
{
if (Environment.OSVersion.Version >= new Version(6, 3)
&& UIDispatcherHelper.MainWindow != null)
{
HWND hWnd = new WindowInteropHelper(Application.Current?.MainWindow).Handle;
HMONITOR hMonitor = User32.MonitorFromWindow(hWnd, User32.MonitorFlags.MONITOR_DEFAULTTONEAREST);
SHCore.GetDpiForMonitor(hMonitor, SHCore.MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out _, out uint dpiY);
return dpiY / 96f;
}
HDC hdc = User32.GetDC(HWND.NULL);
float scaleY = Gdi32.GetDeviceCaps(hdc, Gdi32.DeviceCap.LOGPIXELSY);
_ = User32.ReleaseDC(0, hdc);
return scaleY / 96f;
}
public static DpiScaleF GetScale(nint hWnd = 0)
{
if (hWnd != IntPtr.Zero)
{
HMONITOR hMonitor = User32.MonitorFromWindow(hWnd, User32.MonitorFlags.MONITOR_DEFAULTTONEAREST);
SHCore.GetDpiForMonitor(hMonitor, SHCore.MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out uint dpiX, out uint dpiY);
return new DpiScaleF(dpiX / 96f, dpiY / 96f);
}
using User32.SafeReleaseHDC hdc = User32.GetDC(hWnd);
float scaleX = Gdi32.GetDeviceCaps(hdc, Gdi32.DeviceCap.LOGPIXELSX);
float scaleY = Gdi32.GetDeviceCaps(hdc, Gdi32.DeviceCap.LOGPIXELSY);
return new(scaleX / 96f, scaleY / 96f);
}
}
[DebuggerDisplay("{ToString()}")]
public readonly struct DpiScaleF(float x = 1f, float y = 1f)
{
private readonly float x = x;
public float X => x;
private readonly float y = y;
public float Y => y;
public DpiScaleF Reserve()
{
return new DpiScaleF(1f / x, 1f / y);
}
public override string ToString() => $"{X},{Y}";
}