2024-02-08 18:02:54 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
2024-02-08 12:41:21 +08:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
2024-11-09 06:30:47 +08:00
|
|
|
|
namespace WpfMaiTouchEmulator.Managers;
|
2024-02-08 18:02:54 +08:00
|
|
|
|
|
|
|
|
|
class TouchPanelPositionManager
|
2024-02-08 12:41:21 +08:00
|
|
|
|
{
|
2024-02-08 18:02:54 +08:00
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
2024-11-09 06:30:47 +08:00
|
|
|
|
static extern nint FindWindow(string? lpClassName, string lpWindowName);
|
2024-02-08 12:41:21 +08:00
|
|
|
|
|
2024-02-08 18:02:54 +08:00
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
|
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
2024-11-09 06:30:47 +08:00
|
|
|
|
static extern bool GetWindowRect(nint hWnd, out RECT lpRect);
|
2024-02-08 12:41:21 +08:00
|
|
|
|
|
2024-02-08 18:02:54 +08:00
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
|
public struct RECT
|
|
|
|
|
{
|
|
|
|
|
public int Left;
|
|
|
|
|
public int Top;
|
|
|
|
|
public int Right;
|
|
|
|
|
public int Bottom;
|
2024-04-16 16:23:03 +08:00
|
|
|
|
|
|
|
|
|
public readonly int Width => Right - Left;
|
|
|
|
|
public readonly int Height => Bottom - Top;
|
2024-02-08 18:02:54 +08:00
|
|
|
|
}
|
2024-02-08 12:41:21 +08:00
|
|
|
|
|
2024-02-08 18:02:54 +08:00
|
|
|
|
public Rect? GetSinMaiWindowPosition()
|
|
|
|
|
{
|
2024-02-13 17:40:12 +08:00
|
|
|
|
try
|
2024-02-08 12:41:21 +08:00
|
|
|
|
{
|
2024-02-13 17:40:12 +08:00
|
|
|
|
var hWnd = FindWindow(null, "Sinmai");
|
2024-11-09 06:30:47 +08:00
|
|
|
|
if (hWnd != nint.Zero)
|
2024-02-08 12:41:21 +08:00
|
|
|
|
{
|
2024-02-13 17:40:12 +08:00
|
|
|
|
RECT rect;
|
|
|
|
|
if (GetWindowRect(hWnd, out rect))
|
|
|
|
|
{
|
|
|
|
|
// Calculate the desired size and position based on the other application's window
|
2024-04-16 16:23:03 +08:00
|
|
|
|
var renderRect = GetLargest916Rect(rect);
|
2024-11-09 15:30:58 +08:00
|
|
|
|
var height = renderRect.Height;
|
2024-11-09 06:30:47 +08:00
|
|
|
|
var left = rect.Left + (rect.Right - rect.Left - renderRect.Width) / 2; // Center horizontally
|
2024-11-09 15:30:58 +08:00
|
|
|
|
var top = renderRect.Top;
|
2024-04-16 16:23:03 +08:00
|
|
|
|
return new Rect(left, top, renderRect.Width, height);
|
2024-02-13 17:40:12 +08:00
|
|
|
|
}
|
2024-02-08 12:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-13 17:40:12 +08:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error("Failed top get sinmai window position", ex);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-08 18:02:54 +08:00
|
|
|
|
return null;
|
2024-02-08 12:41:21 +08:00
|
|
|
|
}
|
2024-04-16 16:23:03 +08:00
|
|
|
|
|
|
|
|
|
private static RECT GetLargest916Rect(RECT original)
|
|
|
|
|
{
|
|
|
|
|
var originalWidth = original.Width;
|
|
|
|
|
var originalHeight = original.Height;
|
|
|
|
|
|
2024-11-09 06:30:47 +08:00
|
|
|
|
var widthBasedHeight = originalWidth * 16 / 9;
|
|
|
|
|
var heightBasedWidth = originalHeight * 9 / 16;
|
2024-04-16 16:23:03 +08:00
|
|
|
|
|
|
|
|
|
if (widthBasedHeight <= originalHeight)
|
|
|
|
|
{
|
|
|
|
|
// Width-based rectangle fits
|
|
|
|
|
return new RECT
|
|
|
|
|
{
|
|
|
|
|
Left = original.Left,
|
|
|
|
|
Top = original.Top + (originalHeight - widthBasedHeight) / 2,
|
|
|
|
|
Right = original.Right,
|
|
|
|
|
Bottom = original.Top + (originalHeight + widthBasedHeight) / 2
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Height-based rectangle fits
|
|
|
|
|
return new RECT
|
|
|
|
|
{
|
|
|
|
|
Left = original.Left + (originalWidth - heightBasedWidth) / 2,
|
|
|
|
|
Top = original.Top,
|
|
|
|
|
Right = original.Left + (originalWidth + heightBasedWidth) / 2,
|
|
|
|
|
Bottom = original.Bottom
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-08 12:41:21 +08:00
|
|
|
|
}
|