MaiTouchSensorEmulator/TouchPanelPositionManager.cs

51 lines
1.5 KiB
C#
Raw Normal View History

2024-02-08 18:02:54 +08:00
using System.Runtime.InteropServices;
2024-02-08 12:41:21 +08:00
using System.Windows;
2024-02-08 18:02:54 +08:00
namespace WpfMaiTouchEmulator;
class TouchPanelPositionManager
2024-02-08 12:41:21 +08:00
{
2024-02-08 18:02:54 +08:00
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr 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)]
static extern bool GetWindowRect(IntPtr 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-02-08 12:41:21 +08:00
2024-02-08 18:02:54 +08:00
public Rect? GetSinMaiWindowPosition()
{
try
2024-02-08 12:41:21 +08:00
{
var hWnd = FindWindow(null, "Sinmai");
if (hWnd != IntPtr.Zero)
2024-02-08 12:41:21 +08:00
{
RECT rect;
if (GetWindowRect(hWnd, out rect))
{
// Calculate the desired size and position based on the other application's window
var width = rect.Right - rect.Left;
var height = width;
var left = rect.Left + ((rect.Right - rect.Left) - width) / 2; // Center horizontally
var top = rect.Bottom - height;
return new Rect(left, top, width, height);
}
2024-02-08 12:41:21 +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
}
}