1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-23 14:20:10 +00:00
msfs-popout-panel-manager/WindowsAgent/WindowActionManager.cs

316 lines
11 KiB
C#
Raw Normal View History

2023-07-12 22:41:31 +00:00
using MSFSPopoutPanelManager.DomainModel.Profile;
2022-07-23 19:23:32 +00:00
using System;
using System.Collections.Generic;
using System.Drawing;
2023-07-12 22:41:31 +00:00
using System.Globalization;
2022-07-23 19:23:32 +00:00
using System.Runtime.InteropServices;
2023-07-12 22:41:31 +00:00
using System.Threading;
2023-07-23 05:13:23 +00:00
using System.Windows.Forms;
2022-07-23 19:23:32 +00:00
namespace MSFSPopoutPanelManager.WindowsAgent
{
public class WindowActionManager
{
public static event EventHandler<bool> OnPopOutManagerAlwaysOnTopChanged;
public static void ApplyHidePanelTitleBar(IntPtr hwnd, bool hideTitleBar)
{
2023-07-12 22:41:31 +00:00
if (hideTitleBar)
{
var rect = WindowActionManager.GetWindowRectangle(hwnd);
var rectShadow = PInvoke.GetWindowRectShadow(hwnd);
MoveWindow(hwnd, rect.Left - rectShadow.Left, rect.Top, rect.Width - rectShadow.Width, rect.Height - rectShadow.Height);
Thread.Sleep(250);
}
2022-08-02 20:55:20 +00:00
var currentStyle = (uint)PInvoke.GetWindowLong(hwnd, PInvokeConstant.GWL_STYLE);
2022-07-23 19:23:32 +00:00
if (hideTitleBar)
2022-08-02 20:55:20 +00:00
currentStyle &= ~(PInvokeConstant.WS_CAPTION | PInvokeConstant.WS_SIZEBOX);
2022-07-23 19:23:32 +00:00
else
2022-08-02 20:55:20 +00:00
currentStyle |= (PInvokeConstant.WS_CAPTION | PInvokeConstant.WS_SIZEBOX);
PInvoke.SetWindowLong(hwnd, PInvokeConstant.GWL_STYLE, currentStyle);
2023-07-12 22:41:31 +00:00
Thread.Sleep(250);
if (!hideTitleBar)
{
var rect = WindowActionManager.GetWindowRectangle(hwnd);
var rectShadow = PInvoke.GetWindowRectShadow(hwnd);
MoveWindow(hwnd, rect.Left + rectShadow.Left, rect.Top, rect.Width + rectShadow.Width, rect.Height + rectShadow.Height);
}
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public static void ApplyAlwaysOnTop(IntPtr hwnd, PanelType panelType, bool alwaysOnTop)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
var rectangle = WindowActionManager.GetWindowRectangle(hwnd);
2022-07-23 19:23:32 +00:00
if (panelType == PanelType.PopOutManager)
{
OnPopOutManagerAlwaysOnTopChanged?.Invoke(null, alwaysOnTop);
2023-07-12 22:41:31 +00:00
return;
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
if (alwaysOnTop)
PInvoke.SetWindowPos(hwnd, new IntPtr(PInvokeConstant.HWND_TOPMOST), 0, 0, 0, 0, PInvokeConstant.SWP_ALWAYS_ON_TOP);
2022-07-23 19:23:32 +00:00
else
{
2023-07-12 22:41:31 +00:00
PInvoke.SetWindowPos(hwnd, new IntPtr(PInvokeConstant.HWND_NOTOPMOST), rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height, 0);
Thread.Sleep(250);
MoveWindow(hwnd, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
2022-07-23 19:23:32 +00:00
}
}
2023-07-12 22:41:31 +00:00
public static IntPtr FindWindowByClass(string className)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
return PInvoke.FindWindow(className, null);
2022-07-23 19:23:32 +00:00
}
public static void CloseWindow(IntPtr hwnd)
{
PInvoke.SendMessage(hwnd, PInvokeConstant.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
2022-08-13 06:14:49 +00:00
public static void MoveWindow(IntPtr hwnd, int x, int y, int width, int height)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
var rectShadow = PInvoke.GetWindowRectShadow(hwnd);
PInvoke.MoveWindow(hwnd, x + rectShadow.Left, y + rectShadow.Top, width + rectShadow.Width, height + rectShadow.Height, true);
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public static void MoveWindow(IntPtr hwnd, Rectangle rect)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
var rectShadow = PInvoke.GetWindowRectShadow(hwnd);
PInvoke.MoveWindow(hwnd, rect.Left + rectShadow.Left, rect.Top + rectShadow.Top, rect.Width + rectShadow.Width, rect.Height + rectShadow.Height, true);
2022-07-23 19:23:32 +00:00
}
public static void MinimizeWindow(IntPtr hwnd)
{
PInvoke.ShowWindow(hwnd, PInvokeConstant.SW_MINIMIZE);
}
public static void BringWindowToForeground(IntPtr hwnd)
{
PInvoke.ShowWindowAsync(new HandleRef(null, hwnd), PInvokeConstant.SW_RESTORE);
PInvoke.SetForegroundWindow(hwnd);
}
2023-07-12 22:41:31 +00:00
public static void SetWindowFocus(IntPtr hwnd)
{
var style = (uint) PInvoke.GetWindowLong(hwnd, PInvokeConstant.GWL_STYLE);
// Minimize and restore to be able to make it active.
if ((style & PInvokeConstant.SW_MINIMIZE) == PInvokeConstant.SW_MINIMIZE)
{
PInvoke.ShowWindow(hwnd, PInvokeConstant.SW_RESTORE);
}
uint currentlyFocusedWindowProcessId = PInvoke.GetWindowThreadProcessId(PInvoke.GetForegroundWindow(), IntPtr.Zero);
uint appThread = PInvoke.GetCurrentThreadId();
if (currentlyFocusedWindowProcessId != appThread)
{
PInvoke.AttachThreadInput(currentlyFocusedWindowProcessId, appThread, true);
PInvoke.BringWindowToTop(hwnd);
PInvoke.ShowWindow(hwnd, PInvokeConstant.SW_SHOW);
PInvoke.AttachThreadInput(currentlyFocusedWindowProcessId, appThread, false);
}
else
{
PInvoke.BringWindowToTop(hwnd);
PInvoke.ShowWindow(hwnd, PInvokeConstant.SW_SHOW);
}
}
2022-07-23 19:23:32 +00:00
public static IntPtr FindWindowByCaption(string caption)
{
return PInvoke.FindWindowByCaption(IntPtr.Zero, caption);
}
public static string GetWindowCaption(IntPtr hwnd)
{
2023-07-12 22:41:31 +00:00
try { return PInvoke.GetWindowText(hwnd); }
catch { return string.Empty; }
}
2023-07-12 22:41:31 +00:00
public static void SetWindowCaption(IntPtr hwnd, string caption)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
PInvoke.SetWindowText(hwnd, caption);
}
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
public static Rectangle GetWindowRectangle(IntPtr hwnd)
{
return PInvoke.GetWindowRectangleDwm(hwnd);
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public static Rectangle GetClientRectangle(IntPtr hwnd)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
Rectangle rect;
PInvoke.GetClientRect(hwnd, out rect);
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
return rect;
2022-07-23 19:23:32 +00:00
}
public static bool IsWindow(IntPtr hwnd)
{
return PInvoke.IsWindow(hwnd);
}
public static int GetWindowsCountByPanelType(List<PanelType> panelTypes)
{
var count = 0;
PInvoke.EnumWindows((IntPtr hwnd, int lParam) =>
{
var panelType = GetWindowPanelType(hwnd);
if (panelTypes.Contains(panelType))
count++;
return true;
}, 0);
return count;
}
public static List<IntPtr> GetWindowsByPanelType(List<PanelType> panelTypes)
{
List<IntPtr> windowHandles = new List<IntPtr>();
PInvoke.EnumWindows((IntPtr hwnd, int lParam) =>
{
var panelType = GetWindowPanelType(hwnd);
if (panelTypes.Contains(panelType))
windowHandles.Add(hwnd);
return true;
}, 0);
return windowHandles;
}
2022-07-23 19:23:32 +00:00
public static PanelType GetWindowPanelType(IntPtr hwnd)
{
var className = PInvoke.GetClassName(hwnd);
2023-07-12 22:41:31 +00:00
var caption = GetWindowCaption(hwnd);
2022-07-23 19:23:32 +00:00
if (className == "AceApp") // MSFS windows designation
{
2023-07-12 22:41:31 +00:00
if (String.IsNullOrEmpty(caption) || caption.IndexOf("(Custom)") > -1) // Pop Out window
2022-07-23 19:23:32 +00:00
return PanelType.CustomPopout;
else if (caption.IndexOf("Microsoft Flight Simulator") > -1) // MSFS main game window
return PanelType.FlightSimMainWindow;
else if (caption.IndexOf("WINDOW") > -1) // MSFS multi-monitor window
return PanelType.MultiMonitorWindow;
else
return PanelType.BuiltInPopout; // MSFS built-in window such as ATC, VFRMap
}
else if (className.IndexOf("HwndWrapper[MSFSPopoutPanelManager") > -1)
{
2023-07-12 22:41:31 +00:00
if (caption.IndexOf("HudBar") > -1)
return PanelType.HudBarWindow;
2022-07-23 19:23:32 +00:00
else
return PanelType.PopOutManager;
}
return PanelType.Unknown;
}
public static void CloseAllPopOuts()
{
PInvoke.EnumWindows((IntPtr hwnd, int lParam) =>
{
var panelType = GetWindowPanelType(hwnd);
2023-07-12 22:41:31 +00:00
if (panelType == PanelType.CustomPopout || panelType == PanelType.HudBarWindow)
CloseWindow(hwnd);
2022-07-23 19:23:32 +00:00
return true;
}, 0);
}
public static MsfsGameWindowConfig GetMsfsGameWindowLocation()
{
2023-07-12 22:41:31 +00:00
var isWindowedMode = IsMsfsGameInWindowedMode();
if (isWindowedMode)
{
2023-07-12 22:41:31 +00:00
var msfsGameWindowHandle = GetMsfsGameWindowHandle();
var rect = WindowActionManager.GetWindowRectangle(msfsGameWindowHandle);
return new MsfsGameWindowConfig(rect.Left, rect.Top, rect.Width, rect.Height);
}
return new MsfsGameWindowConfig(0, 0, 0, 0);
}
public static void SetMsfsGameWindowLocation(MsfsGameWindowConfig msfsGameWindowConfig)
{
2023-07-12 22:41:31 +00:00
var isWindowedMode = IsMsfsGameInWindowedMode();
if (isWindowedMode && msfsGameWindowConfig.IsValid)
2023-07-12 22:41:31 +00:00
{
var msfsGameWindowHandle = GetMsfsGameWindowHandle();
PInvoke.MoveWindow(msfsGameWindowHandle, msfsGameWindowConfig.Left, msfsGameWindowConfig.Top, msfsGameWindowConfig.Width, msfsGameWindowConfig.Height, true);
2023-07-12 22:41:31 +00:00
}
}
2023-07-12 22:41:31 +00:00
public static bool IsMsfsGameInWindowedMode()
{
2023-07-12 22:41:31 +00:00
var msfsGameWindowHandle = GetMsfsGameWindowHandle();
if (msfsGameWindowHandle != IntPtr.Zero)
{
var currentStyle = (uint)PInvoke.GetWindowLong(msfsGameWindowHandle, PInvokeConstant.GWL_STYLE);
return (currentStyle & PInvokeConstant.WS_CAPTION) != 0;
}
return false;
}
public static IntPtr GetMsfsGameWindowHandle()
{
IntPtr msfsGameWindowHandle = IntPtr.Zero;
// Get game window handle
PInvoke.EnumWindows(new PInvoke.CallBack((IntPtr hwnd, int lParam) =>
{
var className = PInvoke.GetClassName(hwnd);
if (className == "AceApp") // MSFS windows designation
{
2023-07-12 22:41:31 +00:00
var caption = GetWindowCaption(hwnd);
if (caption.IndexOf("Microsoft Flight Simulator") > -1) // MSFS main game window
{
msfsGameWindowHandle = hwnd;
}
}
return true;
}), 0);
return msfsGameWindowHandle;
}
2023-07-12 22:41:31 +00:00
public static void SetWindowTitleBarColor(IntPtr hwnd, string hexColor)
{
int color;
if (int.TryParse(hexColor, NumberStyles.HexNumber, null, out color))
PInvoke.DwmSetWindowAttribute(hwnd, DwmWindowAttribute.DWMWA_CAPTION_COLOR, ref color, sizeof(Int32));
}
2023-07-23 05:13:23 +00:00
public static List<MonitorInfo> GetMonitorsInfo()
{
var monitors = new List<MonitorInfo>();
foreach (Screen screen in Screen.AllScreens)
{
monitors.Add(new MonitorInfo { Name = screen.DeviceName.Substring(screen.DeviceName.LastIndexOf("\\") + 1), X = screen.Bounds.X, Y = screen.Bounds.Y, Width = screen.Bounds.Width, Height = screen.Bounds.Height });
}
return monitors;
}
2022-07-23 19:23:32 +00:00
}
}