-
Notifications
You must be signed in to change notification settings - Fork 3
/
UnityLibrary.cs
64 lines (51 loc) · 2.02 KB
/
UnityLibrary.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
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace UnityLoader
{
public class UnityLibrary
{
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);
[DllImport("user32.dll")]
private static extern bool CloseWindow(IntPtr hWnd);
private const int WM_ACTIVATE = 6;
private const int WA_INACTIVE = 0;
private const int WA_ACTIVE = 1;
protected void UpdateUnityHwnd(IntPtr windowHandle)
{
EnumChildWindows(windowHandle, (hwnd, lparam) =>
{
UnityHwnd = hwnd;
ActivateUnityWindow();
return false;
}, IntPtr.Zero);
}
public IntPtr UnityHwnd { get; private set; } = IntPtr.Zero;
public void ActivateUnityWindow()
{
SendMessage(UnityHwnd, WM_ACTIVATE, WA_ACTIVE, IntPtr.Zero);
}
public void DeactivateUnityWindow()
{
SendMessage(UnityHwnd, WM_ACTIVATE, WA_INACTIVE, IntPtr.Zero);
}
public void ResizeUnityWindow(int width, int height)
{
MoveWindow(UnityHwnd, 0, 0, width, height, true);
ActivateUnityWindow();
}
public virtual void CloseUnityWindow()
{
CloseWindow(UnityHwnd);
}
protected string CreateArgument(IntPtr windowHandle) => string.Format("-ParentHWND {0}", Environment.Is64BitProcess ? windowHandle.ToInt64() : windowHandle.ToInt32());
}
}