|
@@ -10,6 +10,8 @@ using System.Windows.Media;
|
|
|
using InABox.Core;
|
|
|
using InABox.Wpf;
|
|
|
using Image = System.Windows.Controls.Image;
|
|
|
+using System.Runtime.InteropServices;
|
|
|
+using System.Windows.Interop;
|
|
|
|
|
|
namespace InABox.WPF;
|
|
|
|
|
@@ -33,6 +35,108 @@ public class FuncTemplateSelector : DataTemplateSelector
|
|
|
|
|
|
public static class WPFUtils
|
|
|
{
|
|
|
+
|
|
|
+ public static void MoveToCenter(this Window window)
|
|
|
+ {
|
|
|
+ if (!GetCursorPos(out POINT cursorPoint))
|
|
|
+ return;
|
|
|
+
|
|
|
+ IntPtr mainWindow = MonitorFromPoint(new POINT() { x = 1, y = 1 }, MONITOR_DEFAULTTO.MONITOR_DEFAULTTONULL);
|
|
|
+ IntPtr monitorHandle = MonitorFromPoint(cursorPoint, MONITOR_DEFAULTTO.MONITOR_DEFAULTTONULL);
|
|
|
+
|
|
|
+ MONITORINFO monitorInfo = new() { cbSize = (uint)Marshal.SizeOf<MONITORINFO>() };
|
|
|
+ if (!GetMonitorInfo(monitorHandle, ref monitorInfo))
|
|
|
+ return;
|
|
|
+
|
|
|
+ IntPtr windowHandle = new WindowInteropHelper(window).EnsureHandle();
|
|
|
+
|
|
|
+ if (!GetWindowPlacement(windowHandle, out WINDOWPLACEMENT windowPlacement))
|
|
|
+ return;
|
|
|
+
|
|
|
+ int left = monitorInfo.rcWork.left + Math.Max(0, (int)((monitorInfo.rcWork.Width - windowPlacement.rcNormalPosition.Width) / 2D));
|
|
|
+ int top = monitorInfo.rcWork.top + Math.Max(0, (int)((monitorInfo.rcWork.Height - windowPlacement.rcNormalPosition.Height) / 2D));
|
|
|
+
|
|
|
+ windowPlacement.rcNormalPosition = new RECT(left, top, windowPlacement.rcNormalPosition.Width, windowPlacement.rcNormalPosition.Height);
|
|
|
+ SetWindowPlacement(windowHandle, ref windowPlacement);
|
|
|
+ }
|
|
|
+
|
|
|
+ [DllImport("User32.dll", SetLastError = true)]
|
|
|
+ [return: MarshalAs(UnmanagedType.Bool)]
|
|
|
+ private static extern bool GetCursorPos(out POINT lpPoint);
|
|
|
+
|
|
|
+ [DllImport("User32.dll")]
|
|
|
+ private static extern IntPtr MonitorFromPoint(POINT pt, MONITOR_DEFAULTTO dwFlags);
|
|
|
+
|
|
|
+ private enum MONITOR_DEFAULTTO : uint
|
|
|
+ {
|
|
|
+ MONITOR_DEFAULTTONULL = 0x00000000,
|
|
|
+ MONITOR_DEFAULTTOPRIMARY = 0x00000001,
|
|
|
+ MONITOR_DEFAULTTONEAREST = 0x00000002,
|
|
|
+ }
|
|
|
+
|
|
|
+ [DllImport("User32.dll")]
|
|
|
+ [return: MarshalAs(UnmanagedType.Bool)]
|
|
|
+ private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);
|
|
|
+
|
|
|
+ [StructLayout(LayoutKind.Sequential)]
|
|
|
+ private struct MONITORINFO
|
|
|
+ {
|
|
|
+ public uint cbSize;
|
|
|
+ public RECT rcMonitor;
|
|
|
+ public RECT rcWork;
|
|
|
+ public uint dwFlags;
|
|
|
+ }
|
|
|
+
|
|
|
+ [DllImport("User32.dll", SetLastError = true)]
|
|
|
+ [return: MarshalAs(UnmanagedType.Bool)]
|
|
|
+ private static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl);
|
|
|
+
|
|
|
+ [DllImport("User32.dll", SetLastError = true)]
|
|
|
+ [return: MarshalAs(UnmanagedType.Bool)]
|
|
|
+ private static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl);
|
|
|
+
|
|
|
+ [StructLayout(LayoutKind.Sequential)]
|
|
|
+ private struct WINDOWPLACEMENT
|
|
|
+ {
|
|
|
+ public uint length;
|
|
|
+ public uint flags;
|
|
|
+ public uint showCmd;
|
|
|
+ public POINT ptMinPosition;
|
|
|
+ public POINT ptMaxPosition;
|
|
|
+ public RECT rcNormalPosition;
|
|
|
+ }
|
|
|
+
|
|
|
+ [StructLayout(LayoutKind.Sequential)]
|
|
|
+ private struct POINT
|
|
|
+ {
|
|
|
+ public int x;
|
|
|
+ public int y;
|
|
|
+ }
|
|
|
+
|
|
|
+ [StructLayout(LayoutKind.Sequential)]
|
|
|
+ private struct RECT
|
|
|
+ {
|
|
|
+ public int left;
|
|
|
+ public int top;
|
|
|
+ public int right;
|
|
|
+ public int bottom;
|
|
|
+
|
|
|
+ public int Width => right - left;
|
|
|
+ public int Height => bottom - top;
|
|
|
+
|
|
|
+ public RECT(int x, int y, int width, int height)
|
|
|
+ {
|
|
|
+ left = x;
|
|
|
+ top = y;
|
|
|
+ right = x + width;
|
|
|
+ bottom = y + height;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ #region Setters/Triggers etc
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
public static Style AddSetter(this Style style, DependencyProperty property, object value)
|
|
|
{
|
|
|
style.Setters.Add(new Setter(property, value));
|
|
@@ -55,12 +159,14 @@ public static class WPFUtils
|
|
|
style.Triggers.Add(trigger);
|
|
|
return trigger;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
public static SolidColorBrush ToBrush(this System.Windows.Media.Color color, double opacity = 1.0)
|
|
|
{
|
|
|
return new SolidColorBrush(color) { Opacity = opacity };
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
#region Multi-Binding
|
|
|
|
|
|
public static MultiBinding CreateMultiBinding(IMultiValueConverter? converter = null, string? format = null, object? parameter = null)
|