如何在FireMonkey中限制窗体的最小宽度?它曾经在VCL中非常简单-它只在表单属性中有Max和Min约束。
发布于 2011-11-08 08:21:04
给未来读者的注意事项:
这只适用于XE3以下的版本,因为XE3中删除了Fmx::Platform::TPlatform类。感谢@Alain Thiffault在评论中指出这一点。
原文:
这里有一个更复杂(但更优雅)的替代解决方案,定义一个完全自定义的窗体类,您可以从中继承自己的...
unit FMX.ConstrainedForm;
interface
uses
System.Classes, System.Types, System.UITypes, FMX.Forms, FMX.Platform, FMX.Types;
type
TFormConstraints = class(TPersistent)
private
FMaxHeight: Integer;
FMaxLeft: Integer;
FMaxWidth: Integer;
FMaxTop: Integer;
FMinHeight: Integer;
FMinLeft: Integer;
FMinWidth: Integer;
FMinTop: Integer;
public
constructor Create;
published
property MaxHeight: Integer read FMaxHeight write FMaxHeight default 0;
property MaxLeft: Integer read FMaxLeft write FMaxLeft default 0;
property MaxWidth: Integer read FMaxWidth write FMaxWidth default 0;
property MaxTop: Integer read FMaxTop write FMaxTop default 0;
property MinHeight: Integer read FMinHeight write FMinHeight default 0;
property MinLeft: Integer read FMinLeft write FMinLeft default 0;
property MinWidth: Integer read FMinWidth write FMinWidth default 0;
property MinTop: Integer read FMinTop write FMinTop default 0;
end;
TConstrainedForm = class(TCustomForm)
private
FConstraints: TFormConstraints;
protected
procedure StartWindowResize; override;
procedure StartWindowDrag; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
published
property Constraints: TFormConstraints read FConstraints write FConstraints;
property BiDiMode;
property Caption;
property Cursor default crDefault;
property BorderStyle default TFmxFormBorderStyle.bsSizeable;
property BorderIcons default [TBorderIcon.biSystemMenu, TBorderIcon.biMinimize, TBorderIcon.biMaximize];
property ClientHeight;
property ClientWidth;
property Left;
property Top;
property Margins;
property Position default TFormPosition.poDefaultPosOnly;
property Width;
property Height;
property ShowActivated default True;
property StaysOpen default True;
property Transparency;
property TopMost default False;
property Visible;
property WindowState default TWindowState.wsNormal;
property OnCreate;
property OnDestroy;
property OnClose;
property OnCloseQuery;
property OnActivate;
property OnDeactivate;
property OnResize;
property Fill;
property StyleBook;
property ActiveControl;
property StyleLookup;
property OnPaint;
end;
procedure Register;
implementation
{ TFormConstraints }
constructor TFormConstraints.Create;
begin
inherited;
FMaxHeight := 0;
FMaxLeft := 0;
FMaxWidth := 0;
FMaxTop := 0;
FMinHeight := 0;
FMinLeft := 0;
FMinWidth := 0;
FMinTop := 0;
end;
{ TConstrainedForm }
constructor TConstrainedForm.Create(AOwner: TComponent);
begin
FConstraints := TFormConstraints.Create;
inherited;
end;
destructor TConstrainedForm.Destroy;
begin
FConstraints.Free;
inherited;
end;
procedure TConstrainedForm.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
if (FConstraints.FMinWidth > 0) and (AWidth < FConstraints.FMinWidth) then
AWidth := FConstraints.FMinWidth;
if (FConstraints.FMaxWidth > 0) and (AWidth > FConstraints.FMaxWidth) then
AWidth := FConstraints.FMaxWidth;
if (FConstraints.FMinHeight > 0) and (AHeight < FConstraints.FMinHeight) then
AHeight := FConstraints.FMinHeight;
if (FConstraints.FMaxHeight > 0) and (AHeight > FConstraints.FMaxHeight) then
AHeight := FConstraints.FMaxHeight;
if (FConstraints.FMinLeft > 0) and (ALeft < FConstraints.FMinLeft) then
ALeft := FConstraints.FMinLeft;
if (FConstraints.FMaxLeft > 0) and (ALeft > FConstraints.FMaxLeft) then
ALeft := FConstraints.FMaxLeft;
if (FConstraints.FMinTop > 0) and (ATop < FConstraints.FMinTop) then
ATop := FConstraints.FMinTop;
if (FConstraints.FMaxTop > 0) and (ATop > FConstraints.FMaxTop) then
ATop := FConstraints.FMaxTop;
Platform.SetWindowRect(Self, RectF(ALeft, ATop, ALeft + AWidth, ATop + AHeight));
inherited SetBounds(ALeft, ATop, AWidth, AHeight);
end;
procedure TConstrainedForm.StartWindowDrag;
begin
inherited;
end;
procedure TConstrainedForm.StartWindowResize;
begin
inherited;
end;
procedure Register;
begin
RegisterClass(TConstrainedForm);
end;
end.将此文件存储为FMX.ConstrainedForm.pas,将其添加到窗体的"uses“部分,并修改窗体的声明,以代替:
TForm1 = class(TForm)上面写着:
TForm1 = class(TConstrainedForm)由于缺乏定制设计(不管怎样,在这一点上,这是一个“快速解决方案”),您需要按如下方式连接窗体的OnCreate事件:
procedure TForm1.FormCreate(Sender: TObject);
begin
Constraints.MinWidth := 400;
Constraints.MinHeight := 400;
end;现在,这个窗体将不允许用户将其宽度或高度设置为低于400!
同样,在不对FireMonkey平台本身进行任何实质性更改的情况下,这是您目前所能得到的最好的结果!
发布于 2011-11-08 04:19:32
将其放在表单的"OnResize“事件上,适当地替换这些值。诚然,这不是世界上最好的解决方案,但在重新引入属性之前,它会帮你度过难关!
procedure TForm1.FormResize(Sender: TObject);
begin
if Width < 400 then
Width := 400;
if Height < 400 then
Height := 400;
end;上面的代码很容易更改为任何最大值或最小值的组合,所以请尽情享受吧!
发布于 2013-04-15 02:02:19
LaKraven,模拟一个mouseUp事件来消除闪烁。
if (Width > maxWidth) then
begin
Width := maxWidth;
Mouse_Event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
end;https://stackoverflow.com/questions/8031685
复制相似问题