How to maintain a 16:9 aspect ratio of a Windows App SDK (WinUI 3) Window?
See the question and my original answer on StackOverflowOne solution is to use the underlying Windows API, as Windows is sending the WM_SIZING message when a window is being resized.
For that, you need to be able to handle window messages. Here is some sample code, with interop parts, that does that:
public sealed partial class MainWindow : Window
{
private readonly WindowProcedureHook _hook;
public MainWindow()
{
this.InitializeComponent();
// hook the window procedure
_hook = new WindowProcedureHook(this, WndProc);
}
private IntPtr? WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam)
{
// handle the WM_SIZING message
const int WM_SIZING = 0x0214;
if (msg == WM_SIZING)
{
// get sizing rect
var rc = Marshal.PtrToStructure<RECT>(lParam);
// if coming from left/right only, we adjust height
const int WMSZ_LEFT = 1;
const int WMSZ_RIGHT = 2;
if (wParam.ToInt64() == WMSZ_LEFT || wParam.ToInt64() == WMSZ_RIGHT)
{
rc.height = rc.width * 9 / 16;
}
else
{
rc.width = rc.height * 16 / 9;
}
// put it back, say we handled it
Marshal.StructureToPtr(rc, lParam, false);
return new IntPtr(1);
}
return null; // unhandled, let Windows do the job
}
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
public int width { get => right - left; set => right = left + value; }
public int height { get => bottom - top; set => bottom = top + value; }
}
}
// a utility class to hook window procedure and handle messages, or not
public sealed class WindowProcedureHook
{
private readonly IntPtr _prevProc;
private readonly WNDPROC _wndProc;
private readonly Func<IntPtr, int, IntPtr, IntPtr, IntPtr?> _callback;
public WindowProcedureHook(Window window, Func<IntPtr, int, IntPtr, IntPtr, IntPtr?> callback)
{
ArgumentNullException.ThrowIfNull(window);
ArgumentNullException.ThrowIfNull(callback);
_wndProc = WndProc;
var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
_callback = callback;
const int GWLP_WNDPROC = -4;
_prevProc = GetWindowLong(handle, GWLP_WNDPROC);
SetWindowLong(handle, GWLP_WNDPROC, Marshal.GetFunctionPointerForDelegate(_wndProc));
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam) => _callback(hwnd, msg, wParam, lParam) ?? CallWindowProc(_prevProc, hwnd, msg, wParam, lParam);
private delegate IntPtr WNDPROC(IntPtr handle, int msg, IntPtr wParam, IntPtr lParam);
private static IntPtr GetWindowLong(IntPtr handle, int index) =>
IntPtr.Size == 8 ? GetWindowLongPtrW(handle, index) : (IntPtr)GetWindowLongW(handle, index);
private static IntPtr SetWindowLong(IntPtr handle, int index, IntPtr newLong) =>
IntPtr.Size == 8 ? SetWindowLongPtrW(handle, index, newLong) : (IntPtr)SetWindowLongW(handle, index, newLong.ToInt32());
[DllImport("user32")]
private static extern IntPtr CallWindowProc(IntPtr prevWndProc, IntPtr handle, int msg, IntPtr wParam, IntPtr lParam);
// note: WinUI3 windows are unicode, so we only use the "W" versions
[DllImport("user32")]
private static extern IntPtr GetWindowLongPtrW(IntPtr hWnd, int nIndex);
[DllImport("user32")]
private static extern int GetWindowLongW(IntPtr hWnd, int nIndex);
[DllImport("user32")]
private static extern int SetWindowLongW(IntPtr hWnd, int nIndexn, int dwNewLong);
[DllImport("user32")]
private static extern IntPtr SetWindowLongPtrW(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
}