在下面的 VCL Forms 应用程序中,Application.MainForm 被隐藏,而是显示另一个 TForm 后代。奇怪的是,只有这个TForm后代的名字以“TForm”开头,按Alt+Tab时才能看到。如果其名称不以“TForm”开头,则在 Alt+Tab 时看不到。以上是在 Windows 10 Ann Edition x64 上使用 Delphi XE-Berlin 测试的。
你能帮忙评论一下类名在这里很重要的原因吗?O_O
此外,TForm 后代的类名在其他地方是否重要?
程序
program Strange;
uses
Forms,
uHiddenMainForm in 'uHiddenMainForm.pas' {HiddenMainForm},
uActualMainForm in 'uActualMainForm.pas' {FormActualMainForm};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(THiddenMainForm, HiddenMainForm); // Note: WRONG ! Still, interesting strangeness.
Application.ShowMainForm := False;
Application.MainFormOnTaskbar := True;
// Application.CreateForm(THiddenMainForm, HiddenMainForm); // Note: CORRECT
with TFormActualMainForm.Create(HiddenMainForm) do
begin
Show;
Update;
end;
Application.Run;
end.
主窗体的单元(即 Application.MainForm)
unit uHiddenMainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;
type
THiddenMainForm = class(TForm)
end;
var
HiddenMainForm: THiddenMainForm;
implementation
{$R *.dfm}
end.
实际“主”表格的单位
unit uActualMainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;
type
TFormActualMainForm = class(TForm)
procedure FormClose(Sender: TObject; var Action: TCloseAction);
protected
procedure CreateParams(var Params: TCreateParams) ; override;
end;
implementation
{$R *.dfm}
{ TFormActualMainForm }
procedure TFormActualMainForm.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
end;
procedure TFormActualMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Application.Terminate;
end;
end.
问题If its name does not start with "TForm", it can not be seen when Alt+Tab.
通过以下代码显示出来。唯一的区别是类名从 TFormActualMainForm 更改为 TXFormActualMainForm。
程序
program Strange;
uses
Forms,
uHiddenMainForm in 'uHiddenMainForm.pas' {HiddenMainForm},
uActualMainForm in 'uActualMainForm.pas' {XFormActualMainForm};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(THiddenMainForm, HiddenMainForm);
Application.ShowMainForm := False;
Application.MainFormOnTaskbar := True;
with TXFormActualMainForm.Create(HiddenMainForm) do
begin
Visible := True;
end;
Application.Run;
end.
实际“主”表格的单位
unit uActualMainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;
type
TXFormActualMainForm = class(TForm)
procedure FormClose(Sender: TObject; var Action: TCloseAction);
protected
procedure CreateParams(var Params: TCreateParams) ; override;
end;
implementation
{$R *.dfm}
{ TXFormActualMainForm }
procedure TXFormActualMainForm.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
end;
procedure TXFormActualMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Application.Terminate;
end;
end.