Re: Preserve Aspect Ratio When Resizing Form
- From: ricecake@xxxxxxxxxxxxxxxx (Marcus Kwok)
- Date: Tue, 25 Apr 2006 09:24:11 -0700
larrylard@xxxxxxxxxxx wrote:
Marcus Kwok wrote:
I have a Form that has a PictureBox in it that has a dock style set to
"fill". I can set the PictureBox's PictureBoxSizeMode to StretchImage,
but I would like to preserve the aspect ratio of the image so that the
user may scale the size of the image but not squish it out of
proportion. Is there a simple way to do this?
If it is not possible or requires too much overhead I may just use the
ScrollablePictureBox from
http://www.c-sharpcorner.com/Graphics/ScrollablePictureBox.asp
and forget about being able to resize the image.
I am writing in Managed C++ (.NET 1.1) but in the past I have not had
much difficulty translating code from VB.NET or C# into Managed C++.
The example code at
<http://tinyurl.com/mc9wn>
=
<http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFormsControlClassResizeTopic.asp>
should point you in the right direction.
Thanks for the link. Keeping the window square is easy and works.
However, I am having problems with my method:
I added a double to the class called aspect_ratio.
In the constructor, I calculate
aspect_ratio = static_cast<double>(image->Width) / static_cast<double>(image->Height);
Then in the Resize event handler:
System::Void PlotDisplayWindow_Resize(System::Object * sender, System::EventArgs * e)
{
Control* control = __try_cast<Control*>(sender);
// Looking at this, I think I need Height instead of Width, but the
// concept is what's important
control->Size = System::Drawing::Size(static_cast<int>(aspect_ratio * control->Size.Width), control->Size.Width);
}
When I do this and try to resize the form, I get a flickering of
rectangles of the appropriate aspect ratio as I drag the border of the
frame, but as soon as I let go of the border then the window snaps back
to its original size.
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
.
- References:
- Preserve Aspect Ratio When Resizing Form
- From: Marcus Kwok
- Re: Preserve Aspect Ratio When Resizing Form
- From: larrylard
- Preserve Aspect Ratio When Resizing Form
- Prev by Date: Datagrid Combo Box - Newby question
- Next by Date: How does one create a rounded window?
- Previous by thread: Re: Preserve Aspect Ratio When Resizing Form
- Next by thread: modeless status dlg and disabling main app
- Index(es):
Relevant Pages
|