调整窗体大小,同时保持宽高比 [英] Resize Form while keeping aspect ratio

查看:160
本文介绍了调整窗体大小,同时保持宽高比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口,其中我显示一张照片。我希望用户能够调整此窗口的大小,但
保持与图像相同的宽高比,因此窗口上不会出现大的空白区域。



我尝试过的是在OnResize事件中这样的东西:

  DragWidth:= Width; 
DragHeight:= Height;

//用宽高比计算相应的大小
// ...
//计算值现在位于CalcWidth和CalcHeight

宽度:= CalcWidth;
高度:= CalcHeight;

这个问题是,在调整大小之间的窗口闪烁,原始调整大小和计算,因为OnResize事件在调整大小已经完成(并且被绘制一次)之后调用。



您是否知道有一个平滑的纵横比调整大小的任何解决方案?



感谢任何帮助。

解决方案

添加以下处理程序OnCanResize事件对我来说似乎很好:

  procedure TForm1.FormCanResize(Sender:TObject; var NewWidth,
NewHeight:Integer; var Resize:Boolean);
var
AspectRatio:double;
begin
AspectRatio:= Height / Width;
NewHeight:= round(AspectRatio * NewWidth);
结束

您当然可以更改计算NewHeight和NewWidth的方法。以下方法在空白表单中尝试时,直观地正确:

  NewHeight:= round(0.5 *(NewHeight + ASPECTRATIO * NewWidth)); 
NewWidth:= round(NewHeight / AspectRatio);


I have a Window, in which i display a picture. I want the user to be able to resize this window, but keep it in the same aspect ratio than the image, so no big empty areas appear on the window.

What i tried is something like this in the OnResize event:

DragWidth := Width;
DragHeight := Height;

//Calculate corresponding size with aspect ratio
//...
//Calculated values are now in CalcWidth and CalcHeight

Width := CalcWidth;
Height := CalcHeight;

The Problem with this is, that the window flashes during the resize dragging between the original resize size and the calculated, since the OnResize event is afaik called after the resize is already done (and painted once).

Do you know any solution for having a smooth aspect-ratio resizing?

Thanks for any Help.

解决方案

Adding the following handler for the OnCanResize event seemed to work very well for me:

procedure TForm1.FormCanResize(Sender: TObject; var NewWidth,
  NewHeight: Integer; var Resize: Boolean);
var
  AspectRatio:double;
begin
  AspectRatio:=Height/Width;
  NewHeight:=round(AspectRatio*NewWidth);
end;

You may of course want to change the method for calculating NewHeight and NewWidth. The following method feels intuitively "right" when I try it on a blank form:

  NewHeight:=round(0.5*(NewHeight+AspectRatio*NewWidth));
  NewWidth:=round(NewHeight/AspectRatio);

这篇关于调整窗体大小,同时保持宽高比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆