修复触摸位置错误

This commit is contained in:
LanZhan
2026-04-10 01:11:09 +08:00
parent 73593a2ab7
commit ceb8f9c401
2 changed files with 94 additions and 2 deletions

View File

@@ -6,6 +6,12 @@ public static partial class ExternFunction
{
private static readonly bool _is64BitProcess = Environment.Is64BitProcess;
public const int ENUM_CURRENT_SETTINGS = -1;
public const uint DMDO_DEFAULT = 0;
public const uint DMDO_90 = 1;
public const uint DMDO_180 = 2;
public const uint DMDO_270 = 3;
// 热键修饰符常量
public const uint MOD_ALT = 0x0001;
public const uint MOD_CONTROL = 0x0002;
@@ -35,6 +41,41 @@ public static partial class ExternFunction
public int Bottom;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public unsafe struct DEVMODE
{
public fixed char dmDeviceName[32];
public ushort dmSpecVersion;
public ushort dmDriverVersion;
public ushort dmSize;
public ushort dmDriverExtra;
public uint dmFields;
public int dmPositionX;
public int dmPositionY;
public uint dmDisplayOrientation;
public uint dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
public fixed char dmFormName[32];
public ushort dmLogPixels;
public uint dmBitsPerPel;
public uint dmPelsWidth;
public uint dmPelsHeight;
public uint dmDisplayFlags;
public uint dmDisplayFrequency;
public uint dmICMMethod;
public uint dmICMIntent;
public uint dmMediaType;
public uint dmDitherType;
public uint dmReserved1;
public uint dmReserved2;
public uint dmPanningWidth;
public uint dmPanningHeight;
}
[LibraryImport("user32.dll", EntryPoint = "GetWindowLongW")]
private static partial nint GetWindowLong32(nint hWnd, int nIndex);
@@ -121,6 +162,18 @@ public static partial class ExternFunction
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool UnregisterHotKey(nint hWnd, int id);
[LibraryImport(
"user32.dll",
EntryPoint = "EnumDisplaySettingsW",
StringMarshalling = StringMarshalling.Utf16
)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool EnumDisplaySettings(
string? lpszDeviceName,
int iModeNum,
ref DEVMODE lpDevMode
);
[Flags]
public enum EXECUTION_STATE : uint
{

View File

@@ -257,6 +257,8 @@ public sealed partial class DesktopLyricWindowHelper : IDisposable
return;
}
var displayOrientation = GetCurrentDisplayOrientation();
foreach (var contact in digitizerData.Contacts)
{
var maxX = contact.MaxX;
@@ -266,8 +268,13 @@ public sealed partial class DesktopLyricWindowHelper : IDisposable
continue;
}
var screenX = contact.X * _screenWidth / maxX;
var screenY = contact.Y * _screenHeight / maxY;
var (screenX, screenY) = ConvertRawInputToScreenPoint(
contact.X,
contact.Y,
maxX,
maxY,
displayOrientation
);
// 触摸拖拽进行中
if (_isTouchDragging && contact.Identifier == _touchContactId)
@@ -328,6 +335,38 @@ public sealed partial class DesktopLyricWindowHelper : IDisposable
}
}
private static uint GetCurrentDisplayOrientation()
{
var devMode = new DEVMODE { dmSize = (ushort)Marshal.SizeOf<DEVMODE>() };
return EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref devMode)
? devMode.dmDisplayOrientation
: DMDO_DEFAULT;
}
private (int X, int Y) ConvertRawInputToScreenPoint(
int rawX,
int rawY,
int maxX,
int maxY,
uint displayOrientation
)
{
var normalizedX = Math.Clamp(rawX / (double)maxX, 0d, 1d);
var normalizedY = Math.Clamp(rawY / (double)maxY, 0d, 1d);
var (mappedX, mappedY) = displayOrientation switch
{
DMDO_90 => (1d - normalizedY, normalizedX),
DMDO_180 => (1d - normalizedX, 1d - normalizedY),
DMDO_270 => (normalizedY, 1d - normalizedX),
_ => (normalizedX, normalizedY),
};
var screenX = (int)Math.Round(mappedX * _screenWidth, MidpointRounding.AwayFromZero);
var screenY = (int)Math.Round(mappedY * _screenHeight, MidpointRounding.AwayFromZero);
return (Math.Clamp(screenX, 0, _screenWidth), Math.Clamp(screenY, 0, _screenHeight));
}
// ═══════════════════════════════════════════════════
// 定时器(置顶 + 缓存 Border 屏幕坐标)
// ═══════════════════════════════════════════════════