I'm trying to resize window (only width so far) to keep constant aspect ratio of the client area. When I resize without Form1_ResizeEnd handler, I see the form resizing properly while dragging the window border, but when it's released the height is reset to the original value set in InitializeComponent.
I added Form1_ResizeEnd handler, now the form size is set correctly at the end, but still, while dragging, I see the window is repainted twice, flickering between the original size (set before the dragging) and the final size.
What am I doing wrong? How to resize without flickering effect?
To clarify more:
Without Form1_ResizeEnd, while changing the window size (dragging), the window is repainted quickly between (the new width x calculated height) and (the new width x the original height). When the dragging is over, the window settles on (the new width x the original height), so the new height is not ultimately applied.
If you comment the body of Form1_ResizeEnd, you should see that you can't change the height of the window. Applying Form1_ResizeEnd helps the new height to settle but doesn't solve the problem of repainting the window with two different heights while dragging.
I added Form1_ResizeEnd handler, now the form size is set correctly at the end, but still, while dragging, I see the window is repainted twice, flickering between the original size (set before the dragging) and the final size.
What am I doing wrong? How to resize without flickering effect?
C#:
namespace WinFormsApp1;
public partial class Form1 : Form
{
int cWidth, cHeight;
readonly double aspect;
public Form1()
{
InitializeComponent();
cWidth = this.ClientSize.Width;
cHeight = this.ClientSize.Height;
aspect = (double)cWidth / cHeight;
}
private void Form1_Load(object sender, EventArgs e)
{
tbox.Text = "loaded";
}
private void Form1_Resize(object sender, EventArgs e)
{
var width = this.ClientSize.Width;
var height = this.ClientSize.Height;
var newAspect = (double)width / height;
if (aspect < 1)
if (width != cWidth)
{
cHeight = (int)(width / aspect);
this.ClientSize = new Size(width, cHeight);
}
cWidth = width;
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
if (this.ClientSize.Width != cWidth || this.ClientSize.Height != cHeight)
this.ClientSize = new Size(cWidth, cHeight);
}
}
To clarify more:
Without Form1_ResizeEnd, while changing the window size (dragging), the window is repainted quickly between (the new width x calculated height) and (the new width x the original height). When the dragging is over, the window settles on (the new width x the original height), so the new height is not ultimately applied.
If you comment the body of Form1_ResizeEnd, you should see that you can't change the height of the window. Applying Form1_ResizeEnd helps the new height to settle but doesn't solve the problem of repainting the window with two different heights while dragging.