MaiTouchSensorEmulator/TouchPanelPositionManager.cs

51 lines
1.5 KiB
C#

using System.Runtime.InteropServices;
using System.Windows;
namespace WpfMaiTouchEmulator;
class TouchPanelPositionManager
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string? lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public Rect? GetSinMaiWindowPosition()
{
try
{
var hWnd = FindWindow(null, "Sinmai");
if (hWnd != IntPtr.Zero)
{
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);
}
}
}
catch (Exception ex)
{
Logger.Error("Failed top get sinmai window position", ex);
}
return null;
}
}