如何在 Delphi 中没有 MainForm? [英] How to not have a MainForm in Delphi?

查看:23
本文介绍了如何在 Delphi 中没有 MainForm?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试让我的应用程序中的一些无模式表单出现在任务栏上 - 利用 Windows 7 中新的有用任务栏.

i've been trying to get some modeless forms in my application to appear on the taskbar - taking advantage of the new useful taskbar in Windows 7.

在任务栏上存在表单之前,需要撤消 VCL 的许多问题.

There's are many issues with the VCL that need to be undone before a form can exist on the taskbar.

但最后一个问题是最小化 VCL 指定为主窗体的窗体会导致应用程序中的所有窗口消失.

But the final issue is that minimizing the form that the VCL has designated the main form causes all windows in the application to vanish.

十年前,Peter Above(团队B)记录了这些问题,并试图解决这些问题.但是有一些问题是无法解决的.这些问题在 VCL 本身中存在得太深,以至于实际上不可能使 Delphi 应用程序正常运行.

Ten years ago, Peter Below (TeamB) documented these problems, and attempts to work around them. But there are some issues that cannot be solved. The issues run so deep within the VCL itself, that it's effectively impossible to make Delphi applications behave properly.

这一切都源于你在工具栏上看到的按钮并不代表应用程序的窗口;它代表 TApplications 窗口,它是隐藏的,从不可见.然后是应用程序的 MainForm,它被赋予了特殊的能力,如果 it 被最小化,那么它会指示应用程序隐藏自己.

It all stems from the fact that the button you see on the toolbar does not represent the application's window; it represents the TApplications window, which is hidden and never seen. And then there is the application's MainForm, which is then imbued with special abilities where if it is minimized then it instructs the application to hide itself.

在我看来,如果我能做到

It seems to me that if i can do

Application.MainForm := nil;

然后所有这些错误都会消失.应用程序可以有它的隐藏窗口,同时我将覆盖应用程序中的每个other表单,包括我的主表单,:

then all these bugs would go away. The application can have its hidden window, and in the meantime i'll override every other form in the application, including my main form, with:

procedure TForm2.CreateParams(var params: TCreateParams ); 
begin 
   inherited CreateParams(params); 
   params.ExStyle := params.ExStyle or WS_EX_APPWINDOW; 
end; 

但在 Delphi 中,Application.MainForm 属性是只读的.

But in Delphi the Application.MainForm property is read-only.

我在 Delphi 中如何没有 MainForm ?

How can i not have a MainForm in Delphi?

推荐答案

如果没有分配 MainForm,您将无法运行 GUI 项目.主消息循环将立即退出.但是,这并不意味着 MainForm 必须运行您的 UI.您可以使用一个空白的隐藏 TForm 作为指定的 MainForm,然后让它将您的真实 MainForm 实例化为辅助 TForm.例如:

You cannot run a GUI project without a MainForm assigned. The main message loop will exit immediately without one. However, that does not mean that the MainForm has to run your UI. You can use a blank hidden TForm as the assigned MainForm, and then have it instantiate your real MainForm as a secondary TForm. For example:

HiddenMainFormApp.dpr:

HiddenMainFormApp.dpr:

project HiddenMainFormApp;

uses
  ..., Forms, HiddenMainForm;

begin
  Application.Initialize;
  Application.CreateForm(THiddenMainForm, MainForm);
  Application.ShowMainForm := False;
  Application.Run;
end.

HiddenMainForm.cpp:

HiddenMainForm.cpp:

uses
  ..., RealMainForm;

procedure THiddenMainForm.FormCreate(Sender: TObject);
begin
  RealMainForm := TRealMainForm.Create(Self);
  RealMainForm.Show;
end;

RealMainForm.cpp:

RealMainForm.cpp:

procedure TRealMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
  Application.Terminate;
end;

或者:

HiddenMainFormApp.dpr:

HiddenMainFormApp.dpr:

project HiddenMainFormApp;

uses
  ..., Forms, HiddenMainForm, RealMainForm;

begin
  Application.Initialize;
  Application.CreateForm(THiddenMainForm, MainForm);
  Application.ShowMainForm := False;

  RealMainForm := TRealMainForm.Create(Application);
  RealMainForm.Show;
  RealMainForm.Update;

  Application.Run;
end.

RealMainForm.cpp:

RealMainForm.cpp:

procedure TRealMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
  Application.Terminate;
end;

这篇关于如何在 Delphi 中没有 MainForm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆