-
Notifications
You must be signed in to change notification settings - Fork 2
/
MercuryHelper.cs
80 lines (70 loc) · 2.59 KB
/
MercuryHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Windows;
namespace toucca
{
static class MercuryHelper
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string? lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ClientToScreen(IntPtr hWnd, ref POINT lpPoint);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public readonly int Width => Right - Left;
public readonly int Height => Bottom - Top;
}
public static bool SetWindowRect(IntPtr hWnd, Rect rect)
{
return SetWindowPos(hWnd, IntPtr.Zero, (int)rect.Left, (int)rect.Top, (int)rect.Width, (int)rect.Height, 0x0040);
}
public static Rect? TryLocateMecury()
{
try
{
var hWnd = FindWindow(null, "Mercury "); // Good job Marvelous Inc.
if (hWnd != IntPtr.Zero)
{
RECT rect;
if (GetWindowRect(hWnd, out rect))
{
POINT pt = new() { X = 0, Y = 0 };
if (ClientToScreen(hWnd, ref pt))
{
rect.Left = pt.X;
rect.Top = pt.Y;
}
return new Rect(rect.Left, rect.Top, rect.Width - 10, (rect.Height - 10) * 0.938);
// TODO: Properly handle windowed mode
}
}
}
catch (Exception ex)
{
Logger.Error("Failed to determine Mercury position", ex);
}
return null;
}
}
}