gpt4 book ai didi

Delphi - 是否可以禁用 Delphi 的表单延迟加载?

转载 作者:行者123 更新时间:2023-12-03 19:14:34 25 4
gpt4 key购买 nike

我听说 Delphi 应用程序使用“延迟加载”,延迟加载表单组件,直到它们被实际引用。 another post 中提到过- “这就是我们将 TPageControl 更改为延迟加载的原因 - Delphi IDE 的选项对话框加载时间过长!”

我假设这也适用于使用 Delphi 创建的应用程序,但我在 VCL 源代码中找不到任何提及延迟加载的内容,这表明如果它确实存在,它可能被称为其他东西。

在正常使用中,应用程序不经常启动并运行很长时间的情况下,可能需要放弃更快的启动时间并在第一次实际使用 VCL 组件时更快地绘制 VCL 组件。

Delphi 程序员对此有任何控制权吗? (LazyLoad := false ; 没用 ;-)

最佳答案

考虑以下简单的演示项目:

项目1.dpr

program Project1;

uses
Vcl.Forms,
Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

Unit1.pas
unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls;

type
TButton = class(Vcl.StdCtrls.TButton)
protected
procedure CreateWnd; override;
end;

TForm1 = class(TForm)
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
TabSheet3: TTabSheet;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure FormCreate(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TButton.CreateWnd;
begin
inherited;
Writeln('Window created: ' + Name);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
AllocConsole;
end;

end.

Unit1.dfm
object Form1: TForm1
Caption = 'Form1'
ClientHeight = 299
ClientWidth = 635
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object PageControl1: TPageControl
Left = 40
Top = 40
Width = 537
Height = 233
ActivePage = TabSheet1
object TabSheet1: TTabSheet
Caption = 'TabSheet1'
object Button1: TButton
Caption = 'Button1'
end
end
object TabSheet2: TTabSheet
Caption = 'TabSheet2'
object Button2: TButton
Caption = 'Button2'
end
end
object TabSheet3: TTabSheet
Caption = 'TabSheet3'
object Button3: TButton
Caption = 'Button3'
end
end
end
end

当你运行它时,控制台窗口会显示:
Window created: Button1

As you select each page in turn, the other buttons are created, as shown in the console window:

Window created: Button1Window created: Button2Window created: Button3

Now change the OnCreate event handler to force each page to be visible when the form is created:

procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
AllocConsole;

for i := 0 to PageControl1.PageCount-1 do begin
PageControl1.Pages[i].Visible := True;
end;
end;

现在,当表单首次显示时,控制台窗口显示:
Window created: Button1Window created: Button2Window created: Button3

This is because, as Danny says, the windows are not created until they are shown.

Now, the nuance with regards page controls is the handling of visibility of the pages. The constructor of TTabSheet contains this:

Visible := False;

此外, Visible TTabSheet 的属性(property)是这样发布的:
property Visible stored False;

这意味着当一个页面控件开始它的生命时,它的页面是隐藏的,在 VCL 意义上有 Visible。等于 False .正如 Danny 所说,窗口控件是在显示控件时首先创建的。这发生在 TWinControl.UpdateShowing开始是这样的:
procedure TWinControl.UpdateShowing;
var
ShowControl: Boolean;
I: Integer;
begin
ShowControl := (FVisible and (not (csDesigning in ComponentState) or not (csDesignerHide in ControlState)) or
((csDesigning in ComponentState) and not (csDesignerHide in ControlState)) and
not (csNoDesignVisible in ControlStyle)) and
not (csReadingState in ControlState) and not (csDestroying in ComponentState);
if ShowControl then
begin
if WindowHandle = 0 then CreateHandle; // <-- this is the key
if FWinControls <> nil then
for I := 0 to FWinControls.Count - 1 do
TWinControl(FWinControls[I]).UpdateShowing;
end;
....
end;

页面开始时不显示,然后当它们在 TPageControl.ChangeActivePage 中变为事件状态时对新激活的页面执行以下操作:
Page.BringToFront;
Page.Visible := True;

设置 VisibleTrue结果 TWinControl.UpdateShowing执行,并创建窗口句柄。

这就是为什么在表单创建时使所有页面可见的上述技巧具有您想要的效果。

现在,以上所有内容都非常以页面控制为中心。对于许多其他控件,如果控件可见,则在创建窗体时首先创建窗口。如果您对特定表格有特定问题,那么最好分享具体细节。

关于Delphi - 是否可以禁用 Delphi 的表单延迟加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33715320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com