How Can I Only Allow Uniform Resizing in a WPF Window? How Can I Only Allow Uniform Resizing in a WPF Window? wpf wpf

How Can I Only Allow Uniform Resizing in a WPF Window?


You can reserve aspect ratio of contents using WPF's ViewBox with control with fixed width and height inside.

Let's give this a try. You can change "Stretch" attribute of ViewBox to experience different results.

Here is my screeen shot: enter image description here

<Window x:Class="TestWPF.Window1"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    Title="Window1" Height="300" Width="300">    <Viewbox Stretch="Uniform">        <StackPanel Background="Azure" Height="400" Width="300" Name="stackPanel1" VerticalAlignment="Top">            <Button Name="testBtn" Width="200" Height="50">                <TextBlock>Test</TextBlock>            </Button>        </StackPanel>    </Viewbox></Window>


You can always handle the WM_WINDOWPOSCHANGING message, this let's you control the window size and position during the resizing process (as opposed to fixing things after the user finished resizing).

Here is how you do it in WPF, I combined this code from several sources, so there could be some syntax errors in it.

internal enum WM{   WINDOWPOSCHANGING = 0x0046,}[StructLayout(LayoutKind.Sequential)]internal struct WINDOWPOS{   public IntPtr hwnd;   public IntPtr hwndInsertAfter;   public int x;   public int y;   public int cx;   public int cy;   public int flags;}private void Window_SourceInitialized(object sender, EventArgs ea){   HwndSource hwndSource = (HwndSource)HwndSource.FromVisual((Window)sender);   hwndSource.AddHook(DragHook);}private static IntPtr DragHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handeled){   switch ((WM)msg)   {      case WM.WINDOWPOSCHANGING:      {          WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS));          if ((pos.flags & (int)SWP.NOMOVE) != 0)          {              return IntPtr.Zero;          }          Window wnd = (Window)HwndSource.FromHwnd(hwnd).RootVisual;          if (wnd == null)          {             return IntPtr.Zero;          }          bool changedPos = false;          // ***********************          // Here you check the values inside the pos structure          // if you want to override tehm just change the pos          // structure and set changedPos to true          // ***********************          if (!changedPos)          {             return IntPtr.Zero;          }          Marshal.StructureToPtr(pos, lParam, true);          handeled = true;       }       break;   }   return IntPtr.Zero;}


This is what my solution was.

You will need to add this to your control/window tag:

Loaded="Window_Loaded"

And you will need to place this in your code behind:

private double aspectRatio = 0.0;private void Window_Loaded(object sender, RoutedEventArgs e){    aspectRatio = this.ActualWidth / this.ActualHeight;}protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo){    if (sizeInfo.WidthChanged)    {        this.Width = sizeInfo.NewSize.Height * aspectRatio;    }    else    {        this.Height = sizeInfo.NewSize.Width * aspectRatio;    }}

I tried the Viewbox trick and I did not like it. I wanted to lock the window border to a specific size. This was tested on a window control but I assume it would work on a border as well.