我有一个用 Delphi 10.4 编写的可视化控件。为了方便地关注问题,我仅创建了具有 Anchors 和 Align 属性的 TwinControl。我包括了 AutoSize 属性。问题是当该组件构建安装并放置在 TForm 上时,控件将不会响应表单大小调整。
该控件已将控件设置为锚定正确。我向表单添加了一个 TButton,并将其锚点设置为 akRight。按钮随着表单的拉伸而移动。我的控制没有。我可以提供这个小测试的完整源代码。也许有人以前见过这个。我以前曾在我的组件中使用过 TFrame,但发现存在一些显示问题,仅部分控件显示或根本不显示。我的客户没有留下深刻的印象。我希望能得到一些智慧。
这是来自 TWinControl 的代码
unit SampleControl;
interface
uses
System.Classes, Vcl.Controls, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Buttons, Vcl.Dialogs,
Vcl.Graphics,AdvGrid,vcl.Forms,System.SysUtils,StrUtils,Messages, EBS3DataUtils,
EBSGridSetup,System.UITypes,WinApi.Windows,vcl.Grids,AdvObj,System.Variants;
Type
TSampleControl = class(TWinControl)
private
FAnchors: TAnchors;
FAlign : TAlign;
FAutoSize : Boolean;
procedure InitializeComponents;
procedure SetAnchors(const Value: TAnchors);
procedure SetAlign(const Value: TAlign);
procedure SetAutoSize(const Value: Boolean);
Public
procedure Resize; Override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
Published
property Anchors: TAnchors read FAnchors write SetAnchors default [akLeft, akTop];
property Align: TAlign read FAlign write SetAlign default alNone;
property AutoSize: Boolean read FAutoSize write SetAutoSize default True;
end;
Procedure Register;
implementation
constructor TSampleControl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Width := 425;
Height := 170;
Parent := AOwner AS TWinControl;
InitializeComponents;
end;
destructor TSampleControl.Destroy;
begin
// Free components created in InitializeComponents
inherited Destroy;
end;
procedure TSampleControl.InitializeComponents;
begin
// Create and set up components
FAlign := AlNone;
FAutoSize := True;
FAnchors := [akLeft,akTop];
end;
procedure TSampleControl.SetAnchors(const Value: TAnchors);
begin
if FAnchors <> Value then
begin
FAnchors := Value;
end;
end;
procedure TSampleControl.SetAlign(const Value: TAlign);
begin
if FAlign <> Value then
begin
FAlign := Value;
end;
end;
procedure TSampleControl.Resize;
begin
Inherited;
end;
procedure TSampleControl.SetAutoSize(const Value: Boolean);
begin
if FAutoSize <> Value then
begin
FAutoSize := Value;
end;
end;
procedure Register;
begin
RegisterComponents('EBSH', [TSampleControl]);
end;
end.
这是 TForm 中的代码
unit ComponentTest;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,AdvUtil, Vcl.Grids, AdvObj,
BaseGrid, AdvGrid, UniProvider, SQLServerUniProvider, MyDropdown,
MyProgressBar, Vcl.StdCtrls, EBSGrid, JvExStdCtrls, JvMemo, Vcl.ExtCtrls,
Notes, ContactPanel, EBSGridSetup, SampleControl;
type
TfrmCompTest = class(TForm)
Button1: TButton;
SampleControl1: TSampleControl;
procedure FormResize(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmCompTest: TfrmCompTest;
implementation
{$R *.dfm}
procedure TfrmCompTest.FormResize(Sender: TObject);
begin
SampleControl1.Resize;
end;
end.
您的控件重新引入了 Anchors、Align 和 AutoSize 的新属性。这些与 TControl 中的类似命名属性无关。您应该覆盖原始属性以发布它们并根据您的需要初始化它们,而不是创建新属性:
Type
TSampleControl = class(TWinControl)
private
procedure InitializeComponents;
procedure SetAnchors(const Value: TAnchors);
procedure SetAlign(const Value: TAlign);
procedure SetAutoSize(const Value: Boolean);
Public
procedure Resize; Override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
Published
property Anchors;
property Align;
property AutoSize;
end;
procedure TSampleControl.InitializeComponents;
begin
// Create and set up components
Align := alNone;
AutoSize := True;
Anchors := [akLeft,akTop];
end;