1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-22 13:50:14 +00:00
msfs-popout-panel-manager/Provider/WindowManager.cs

155 lines
6.3 KiB
C#
Raw Normal View History

2022-06-30 23:53:08 +00:00
using MSFSPopoutPanelManager.Model;
using System;
2021-12-14 05:40:07 +00:00
using System.Drawing;
2022-01-27 13:40:04 +00:00
using System.Runtime.InteropServices;
2021-12-14 05:40:07 +00:00
namespace MSFSPopoutPanelManager.Provider
{
public class WindowManager
{
public static void ApplyHidePanelTitleBar(IntPtr handle, bool hideTitleBar)
{
2022-01-18 14:58:11 +00:00
var currentStyle = PInvoke.GetWindowLong(handle, PInvokeConstant.GWL_STYLE).ToInt64();
2021-12-14 05:40:07 +00:00
if (hideTitleBar)
2022-01-18 14:58:11 +00:00
PInvoke.SetWindowLong(handle, PInvokeConstant.GWL_STYLE, (uint)(currentStyle & ~(PInvokeConstant.WS_CAPTION | PInvokeConstant.WS_SIZEBOX)));
2021-12-14 05:40:07 +00:00
else
2022-01-18 14:58:11 +00:00
PInvoke.SetWindowLong(handle, PInvokeConstant.GWL_STYLE, (uint)(currentStyle | (PInvokeConstant.WS_CAPTION | PInvokeConstant.WS_SIZEBOX)));
2021-12-14 05:40:07 +00:00
}
2022-06-30 23:53:08 +00:00
public static void ApplyAlwaysOnTop(IntPtr handle, PanelType panelType, bool alwaysOnTop, Rectangle panelRectangle)
2021-12-14 05:40:07 +00:00
{
2022-06-30 23:53:08 +00:00
// Override weird size adjustment for Touch Panel WPF window
int newWidth = panelType == PanelType.MSFSTouchPanel ? panelRectangle.Width - 16 : panelRectangle.Width;
int newHeight = panelType == PanelType.MSFSTouchPanel ? panelRectangle.Height - 39 : panelRectangle.Height;
2021-12-14 05:40:07 +00:00
if (alwaysOnTop)
2022-06-30 23:53:08 +00:00
PInvoke.SetWindowPos(handle, new IntPtr(PInvokeConstant.HWND_TOPMOST), panelRectangle.Left, panelRectangle.Top, newWidth, newHeight, PInvokeConstant.SWP_ALWAYS_ON_TOP);
2021-12-14 05:40:07 +00:00
else
2022-06-30 23:53:08 +00:00
PInvoke.SetWindowPos(handle, new IntPtr(PInvokeConstant.HWND_NOTOPMOST), panelRectangle.Left, panelRectangle.Top, newWidth, newHeight, 0);
2022-01-27 13:40:04 +00:00
}
public static void ApplyAlwaysOnTop(IntPtr handle, bool alwaysOnTop)
{
Rectangle rect;
PInvoke.GetWindowRect(handle, out rect);
Rectangle clientRectangle;
PInvoke.GetClientRect(handle, out clientRectangle);
2022-06-30 23:53:08 +00:00
ApplyAlwaysOnTop(handle, PanelType.WPFWindow, alwaysOnTop, new Rectangle(rect.X, rect.Y, clientRectangle.Width, clientRectangle.Height));
2021-12-14 05:40:07 +00:00
}
public static void CloseWindow(IntPtr handle)
{
2022-01-18 14:58:11 +00:00
PInvoke.SendMessage(handle, PInvokeConstant.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
public static void MoveWindow(IntPtr handle, int x, int y)
{
Rectangle rectangle;
PInvoke.GetClientRect(handle, out rectangle);
2022-04-18 03:38:33 +00:00
PInvoke.MoveWindow(handle, x, y, rectangle.Width, rectangle.Height, true);
2021-12-14 05:40:07 +00:00
}
2022-06-30 23:53:08 +00:00
public static void MoveWindow(IntPtr handle, PanelType panelType, int x, int y, int width, int height)
{
// Override weird size adjustment for Touch Panel WPF window
int newWidth = panelType == PanelType.MSFSTouchPanel ? width - 16 : width;
int newHeight = panelType == PanelType.MSFSTouchPanel ? height - 39 : height;
PInvoke.MoveWindow(handle, x, y, newWidth, newHeight, true);
}
public static void MoveWindowWithMsfsBugOverrirde(IntPtr handle, PanelType panelType, int x, int y, int width, int height)
{
int originalX = x;
// Override weird size adjustment for Touch Panel WPF window
int newWidth = panelType == PanelType.MSFSTouchPanel ? width - 16 : width;
int newHeight = panelType == PanelType.MSFSTouchPanel ? height - 39 : height;
PInvoke.MoveWindow(handle, x, y, newWidth, newHeight, true);
// Fixed MSFS bug, create workaround where on 2nd or later instance of width adjustment, the panel shift to the left by itself
// Wait for system to catch up on panel coordinate that were just applied
System.Threading.Thread.Sleep(200);
Rectangle rectangle;
PInvoke.GetWindowRect(handle, out rectangle);
if (rectangle.Left != originalX)
PInvoke.MoveWindow(handle, originalX, y, newWidth, newHeight, false);
}
2022-01-27 13:40:04 +00:00
public static void MinimizeWindow(IntPtr handle)
{
PInvoke.ShowWindow(handle, PInvokeConstant.SW_MINIMIZE);
}
public static void BringWindowToForeground(IntPtr handle)
2021-12-14 05:40:07 +00:00
{
2022-01-27 13:40:04 +00:00
PInvoke.ShowWindowAsync(new HandleRef(null, handle), PInvokeConstant.SW_RESTORE);
PInvoke.SetForegroundWindow(handle);
2021-12-14 05:40:07 +00:00
}
2022-01-27 13:40:04 +00:00
public static void CloseAllCustomPopoutPanels()
2021-12-14 05:40:07 +00:00
{
2022-01-27 13:40:04 +00:00
PInvoke.EnumWindows(new PInvoke.CallBack(EnumAllCustomPopoutPanels), 1);
2021-12-14 05:40:07 +00:00
}
2022-01-27 13:40:04 +00:00
public static void MinimizeAllPopoutPanels(bool active)
2021-12-14 05:40:07 +00:00
{
2022-01-27 13:40:04 +00:00
if (active)
2021-12-14 05:40:07 +00:00
{
2022-01-27 13:40:04 +00:00
PInvoke.EnumWindows(new PInvoke.CallBack(EnumToMinimizePopoutPanels), 0);
}
else
{
PInvoke.EnumWindows(new PInvoke.CallBack(EnumToMinimizePopoutPanels), 1);
2021-12-14 05:40:07 +00:00
}
}
2022-01-18 14:58:11 +00:00
2022-06-30 23:53:08 +00:00
public static IntPtr FindWindowByCaption(string caption)
{
return PInvoke.FindWindowByCaption(IntPtr.Zero, caption);
}
2022-01-27 13:40:04 +00:00
private static bool EnumToMinimizePopoutPanels(IntPtr hwnd, int index)
2022-01-18 14:58:11 +00:00
{
2022-01-27 13:40:04 +00:00
var className = PInvoke.GetClassName(hwnd);
var caption = PInvoke.GetWindowText(hwnd);
if (className == "AceApp" && caption.IndexOf("Microsoft Flight Simulator") == -1) // MSFS windows designation
{
if (index == 0)
PInvoke.ShowWindow(hwnd, PInvokeConstant.SW_MINIMIZE);
else
PInvoke.ShowWindow(hwnd, PInvokeConstant.SW_RESTORE);
}
return true;
2022-01-18 14:58:11 +00:00
}
private static bool EnumAllCustomPopoutPanels(IntPtr hwnd, int index)
{
var className = PInvoke.GetClassName(hwnd);
var caption = PInvoke.GetWindowText(hwnd);
2022-07-10 23:58:54 +00:00
if (className == "AceApp" && caption.IndexOf("WINDOW") > -1) // For multi monitor window, do nothing
return true;
2022-01-18 14:58:11 +00:00
if (className == "AceApp" && (caption.IndexOf("(Custom)") > -1 || caption == String.Empty)) // Only close non-builtin pop out panels
{
WindowManager.CloseWindow(hwnd);
}
2022-07-10 23:58:54 +00:00
else if (className == "AceApp" && caption.IndexOf("Microsoft Flight Simulator") == -1) // For builtin pop out (ATC, VFR Map, ect)
2022-01-18 14:58:11 +00:00
{
WindowManager.MoveWindow(hwnd, 0, 0);
}
return true;
}
2021-12-14 05:40:07 +00:00
}
}