added saving app placement
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CursorLang.Interop;
|
||||
|
||||
/// <summary>
|
||||
/// Win32 API для размещения окна настроек: его собственные границы и рабочая
|
||||
/// область монитора, на который окно просят поставить.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Границы берутся у системы, а не из <c>Window.Left/Top/Width/Height</c>:
|
||||
/// высота окна подстраивается под содержимое, и WPF пересчитывает эти свойства
|
||||
/// по DPI монитора, а рабочая область монитора приходит в пикселях. Считать
|
||||
/// центр в одних единицах проще, чем переводить туда-обратно.
|
||||
/// </remarks>
|
||||
internal static class WindowPlacementNative
|
||||
{
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool GetWindowRect(IntPtr hWnd, out PopupWindowNative.Rect lpRect);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr MonitorFromRect(ref PopupWindowNative.Rect lprc, uint dwFlags);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MonitorInfo lpmi);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct MonitorInfo
|
||||
{
|
||||
public int cbSize;
|
||||
public PopupWindowNative.Rect rcMonitor;
|
||||
public PopupWindowNative.Rect rcWork;
|
||||
public uint dwFlags;
|
||||
}
|
||||
|
||||
private const uint MONITOR_DEFAULTTONEAREST = 2;
|
||||
|
||||
/// <summary>Границы окна в пикселях экрана — вместе с рамкой и заголовком.</summary>
|
||||
internal static PopupWindowNative.Rect? TryGetBounds(IntPtr hWnd) =>
|
||||
GetWindowRect(hWnd, out PopupWindowNative.Rect bounds) ? bounds : null;
|
||||
|
||||
/// <summary>
|
||||
/// Рабочая область — без панели задач — того монитора, на котором
|
||||
/// прямоугольник находится целиком или хотя бы большей частью.
|
||||
/// </summary>
|
||||
internal static PopupWindowNative.Rect? TryGetWorkAreaNear(PopupWindowNative.Rect rect)
|
||||
{
|
||||
IntPtr monitor = MonitorFromRect(ref rect, MONITOR_DEFAULTTONEAREST);
|
||||
|
||||
var info = new MonitorInfo { cbSize = Marshal.SizeOf<MonitorInfo>() };
|
||||
return GetMonitorInfo(monitor, ref info) ? info.rcWork : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user