V
V
VanArcher2016-09-28 18:38:37
C++ / C#
VanArcher, 2016-09-28 18:38:37

How do I set a Windows Form to resize while maintaining the aspect ratio?

Adding a windows form element (not WPF).
It is necessary that the dimensions of the form do not lose the aspect ratio.
I use the "SizeChanged" form event, in it I build the resizing of the form, but for some reason it does not work correctly when the width changes, the height twitches from a larger value to the initial one, eventually retaining its initial value. And when you change the height, nothing happens.
There are given initial form size values ​​width = width_ = 600 and height = height_ = 450 and Height_K(width/height) and Width_K(height/width) transform coefficients are given.
Code:
private void Form_book_SizeChanged(object sender, EventArgs e)
{
if (Width != Width_)
{
Height = Height + (Width - Width_) * Height_K;
Width_ = Width;
}
else
{
Width = Width + (Height - Height_) * Width_K;
Height_ = Height;
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Kryuchek, 2016-10-18
@KMaxI

You need to store the previous window sizes in private variables, and compare the new value with the previous one.
And if they have changed, then execute the resize code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question