Save aspect ratio of form when resizing in C #

How can I create a shape with a fixed aspect ratio that persists when resized?

I know that this can be done by overriding OnSizeChanged and manually changing the [new] Height / Width, but this causes a flicker as it changes once before the event is raised (to a size that does not match the proportion) and then resized again (to the correct ratio parties). Is there a better way?

+4
source share
3 answers

Code to get you started. The key must respond to the WM_SIZING message, it allows you to change the rectangle of the window. This pattern is rough, you really want to pay attention to which corner or edge the user is dragging, available from m.WParam. The user interface will never be big, you cannot do anything reasonable when the user drags a corner. Create the flexibility of your form so that it doesn’t matter to you that aspect ration is the real solution. Displaying the scroll bar when the content is not suitable to a large extent allows the user to do the right thing automatically.

 using System.Runtime.InteropServices; // etc.. public partial class Form1 : Form { public Form1() { InitializeComponent(); } protected override void WndProc(ref Message m) { if (m.Msg == 0x216 || m.Msg == 0x214) { // WM_MOVING || WM_SIZING // Keep the window square RECT rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT)); int w = rc.Right - rc.Left; int h = rc.Bottom - rc.Top; int z = w > h ? w : h; rc.Bottom = rc.Top + z; rc.Right = rc.Left + z; Marshal.StructureToPtr(rc, m.LParam, false); m.Result = (IntPtr)1; return; } base.WndProc(ref m); } [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; } } 
+11
source

To expand on the accepted answer:

 protected override void WndProc(ref Message m) { /* internal const int WMSZ_LEFT = 1; internal const int WMSZ_RIGHT = 2; internal const int WMSZ_TOP = 3; internal const int WMSZ_TOPLEFT = 4; internal const int WMSZ_TOPRIGHT = 5; internal const int WMSZ_BOTTOM = 6; internal const int WMSZ_BOTTOMLEFT = 7; internal const int WMSZ_BOTTOMRIGHT = 8; */ if (m.Msg == 0x214) { // WM_MOVING || WM_SIZING // Keep the window square RECT rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT)); int w = rc.Right - rc.Left; // get width int h = rc.Bottom - rc.Top; // get height int z = w > h ? w : h; // z is greatest of the two (w/h) switch ((int)m.WParam) { case 1: //left rc.Bottom = rc.Top + w;//define width rc.Top = rc.Top + (h - (rc.Bottom - rc.Top)) / 2;//define width break; case 2: //right rc.Bottom = rc.Top + w;//define width rc.Top = rc.Top + (h - (rc.Bottom - rc.Top)) / 2;//define width break; case 3: //top rc.Right = rc.Left + h;//define width rc.Left = rc.Left + (w - (rc.Right - rc.Left)) / 2;//define width break; case 4: rc.Top = rc.Bottom - z;//define height rc.Left = rc.Right - z;//define width break; case 5: rc.Top = rc.Bottom - z;//define height rc.Right = rc.Left + z;//define width break; case 6: rc.Right = rc.Left + h;//define width rc.Left = rc.Left + (w-(rc.Right - rc.Left))/2;//define width break; case 7: rc.Bottom = rc.Top + z;//define height rc.Left = rc.Right - z;//define width break; case 8: rc.Bottom = rc.Top + z;//define height rc.Right = rc.Left + z;//define width break; default: break; } Marshal.StructureToPtr(rc, m.LParam, false); m.Result = (IntPtr)1; return; } base.WndProc(ref m); } [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; } 
0
source

Why suggest resizing if you want to give the user a fixed aspect ratio?

You can offer a screen with a slider to resize the window to fit the aspect ratio. Then, after changing, you resize the window.

I would recommend that your users be able to resize and allow them to maintain the proportions that look best to them.

-3
source

All Articles