One solution is to use the underlying Windows API, as Windows is sending the WM_SIZING message when a window is being resized.
一种解决方案是使用底层Windows API,因为Windows在调整窗口大小时发送WM_SIZING消息。
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);
}
You need to set both Width
and Height
on SizeInt32
. This works:
您需要在SizeInt32上同时设置宽度和高度。这是可行的:
MainWindow.xaml.cs
MainWindow.xaml.cs
using Microsoft.UI.Xaml;
using Windows.Graphics;
namespace WinUI3App1;
public sealed partial class MainWindow : Window
{
private double _currentWindowWidth = 0;
private double _currentWindowHeight = 0;
public MainWindow()
{
this.InitializeComponent();
this.SizeChanged += MainWindow_SizeChanged;
KeepWindowSizeAspectRatio();
}
public double WindowSizeAspectRatio { get; set; } = 16.0 / 9;
private void MainWindow_SizeChanged(object sender, WindowSizeChangedEventArgs args)
{
KeepWindowSizeAspectRatio();
}
private void KeepWindowSizeAspectRatio()
{
if (_currentWindowWidth is 0 ||
_currentWindowHeight is 0)
{
_currentWindowWidth = this.AppWindow.Size.Width;
_currentWindowHeight = this.AppWindow.Size.Height;
}
int newWidth = this.AppWindow.Size.Width;
int newHeight = this.AppWindow.Size.Height;
if (this.AppWindow.Size.Width != _currentWindowWidth)
{
newHeight = (int)(newWidth / WindowSizeAspectRatio);
_currentWindowWidth = this.AppWindow.Size.Width;
}
if (this.AppWindow.Size.Height != _currentWindowHeight)
{
newWidth = (int)(newHeight * WindowSizeAspectRatio);
_currentWindowHeight = this.AppWindow.Size.Height;
}
this.AppWindow.Resize(new SizeInt32(newWidth, newHeight));
}
}
更多回答
Works, but the window resize is not as smooth as with Simon's solution. Thanks!
工作,但窗口大小调整不像西蒙的解决方案那么顺利。谢谢!
我是一名优秀的程序员,十分优秀!