.NET 6 WPF项目中AspectRatioWindow类一个生效一个失效求助
WPF .NET 6项目中AspectRatioWindow类在一个项目生效另一个失效的问题
我有两个基于.NET 6的WPF项目,其中一个项目里编写的AspectRatioWindow类可正常维持窗口宽高比,但在另一个项目中该类无法生效。以下是相关代码:
AspectRatioWindow类代码
namespace Bank.UI; using System.Runtime.InteropServices; using System.Windows.Interop; using System; using System.Windows; public class AspectRatioWindow : Window { private const int WM_SIZING = 0x0214; private const int WM_WINDOWPOSCHANGING = 0x0046; private const int WMSZ_LEFT = 1; private const int WMSZ_RIGHT = 2; private const int WMSZ_TOP = 3; private const int WMSZ_TOPLEFT = 4; private const int WMSZ_TOPRIGHT = 5; private const int WMSZ_BOTTOM = 6; private const int WMSZ_BOTTOMLEFT = 7; private const int WMSZ_BOTTOMRIGHT = 8; private IntPtr hWnd = IntPtr.Zero; private double xRatio = 1; private double yRatio = 1; private int sizingEdge = 0; private IntPtr DragHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handeled) { switch (msg) { case WM_SIZING: sizingEdge = wParam.ToInt32(); break; case WM_WINDOWPOSCHANGING: var position = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS))!; if (position.cx == Width && position.cy == Height) return IntPtr.Zero; switch (sizingEdge) { case WMSZ_TOP or WMSZ_BOTTOM or WMSZ_TOPRIGHT: position.cx = (int)(position.cy * xRatio); break; case WMSZ_LEFT or WMSZ_RIGHT or WMSZ_BOTTOMRIGHT or WMSZ_BOTTOMLEFT: position.cy = (int)(position.cx * yRatio); break; case WMSZ_TOPLEFT: position.cx = (int)(position.cy * xRatio); position.x = (int)Left - (position.cx - (int)Width); break; } Marshal.StructureToPtr(position, lParam, true); break; } return IntPtr.Zero; } public new void Show() { xRatio = Width / Height; yRatio = Height / Width; base.Show(); if (hWnd == IntPtr.Zero) { var hWnd = new WindowInteropHelper(this).Handle; var source = HwndSource.FromHwnd(hWnd); source?.AddHook(DragHook); } } [StructLayout(LayoutKind.Sequential)] private struct WINDOWPOS { public IntPtr hwnd; public IntPtr hwndInsertAfter; public int x; public int y; public int cx; public int cy; public int flags; } }
MainWindow的C#代码
namespace MyApp; using System; using System.ComponentModel; using System.Windows; public sealed partial class MainWindow : AspectRatioWindow { public MainWindow() { InitializeComponent(); } protected override async void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); await AppCore.OnStart(); } protected override async void OnClosing(CancelEventArgs e) { base.OnClosing(e); await AppCore.OnStart(); } }
MainWindow的XAML代码
<ui:AspectRatioWindow x:Class="MyApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ui="clr-namespace:MyApp.UI" Title="MyApp" MinHeight="500" MinWidth="800" Height="500" Width="800" WindowStyle="ThreeDBorderWindow" WindowStartupLocation="CenterScreen" ResizeMode="CanResize" Background="{StaticResource BackgroundColor}"> <Viewbox Stretch="Uniform"> <StackPanel Orientation="Vertical" Margin="20,10,20,10" HorizontalAlignment="Center"/> ..Controls.. </StackPanel> </Viewbox> </ui:AspectRatioWindow>
若需了解项目更多细节,可随时告知。
内容的提问来源于stack exchange,提问作者Simon
相关产品推荐
相关产品推荐

