delphi - 调整表单大小,同时保持纵横比

标签 delphi events forms

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

我在 OnResize 事件中尝试过类似的操作:

DragWidth := Width;
DragHeight := Height;

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

Width := CalcWidth;
Height := 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);
end;

您当然可能想要更改计算 NewHeight 和 NewWidth 的方法。当我在空白表单上尝试时,以下方法直观地感觉“正确”:

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

关于delphi - 调整表单大小,同时保持纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6265746/

相关文章:

delphi - Delphi Berlin数据断点无效的地址/数据断点的地址使我难以理解

mysql - 是否可以在 MySQL 中回滚 CREATE EVENT?

c# - 如何触发创建的复选框更改事件 C#

WPF/XAML : multiple controls using the same events - is there an easier way?

delphi - TPopupMenu 项目未显示加速字符,是 Windows 设置问题还是什么?

delphi - 我应该使用 delphi tframes 来处理多页表单吗?

delphi - 使用 DDE 将图像添加到 Excel 工作表

php - 在 CSS 菜单中选择表单失去 CSS 下拉菜单的焦点

Javascript 表单提交等待完成

c# - 如何通过逗号分隔将mysql中的记录从另一种形式加载到一种形式C#