我的应用程序按照您描述的方式工作。这是我采取的方法。我本来想找到一种更简单的方法,但从来没有。
我从阅读这些文章开始。这第一个是彼得下面的一篇很棒的文章:
http://groups-beta.google.com/group/borland.public.delphi.winapi/msg/e9f75ff48ce960eb?hl=en
其他信息也在这里找到,但这并没有证明是一个有效的解决方案:供我使用:http:
//blogs.teamb.com/DeepakShenoy/archive/2005/04/26/4050.aspx
最终这就是我的结局。
我的启动画面兼作应用程序主窗体。主窗体与应用程序对象有特殊的联系。使用所有辅助形式可以让我获得我正在寻找的行为。
在任务栏上我想要的每个表单中,我都会覆盖 CreateParams。我在我的编辑表单上执行此操作,用户将其视为“主要表单”
procedure TUaarSalesMain.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
Params.WndParent := GetDesktopWindow;
end;
就 Delphi 而言,我的“主”表单在其 Activitate 函数中加载真正的主表单。我使用一个成员变量来跟踪第一次激活。然后在函数结束时隐藏启动表单,但不要关闭它。这对我来说很重要,因为如果用户正在编辑文档并关闭主窗体,我不希望同时强制关闭编辑屏幕。通过这种方式,所有可见形式都被视为相同。
if FFirstActivate = false then
exit;
FFristActivate := false;
/*
Main Load code here
Update Splash label, repaint
Application.CreateForm
etc.
*/
// I can't change visible here but I can change the size of the window
Self.Height := 0;
Self.Width := 0;
Self.Enabled := false;
// It is tempting to set Self.Visible := false here but that is not
// possible because you can't change the Visible status inside this
// function. So we need to send a message instead.
ShowWindow(Self.Handle, SW_HIDE);
end;
但是还有一个问题。当所有其他表单关闭时,您需要关闭主/启动窗口。我在关闭例程中对 Parent <> nil 进行了额外检查,因为我使用表单作为插件(形成我的目的,它们比框架更有效)。
我不太喜欢使用 Idle 事件,但我没有注意到这会拖累 CPU。
{
TApplicationManager.ApplicationEventsIdle
---------------------------------------------------------------------------
}
procedure TApplicationManager.ApplicationEventsIdle(Sender: TObject;
var Done: Boolean);
begin
if Screen.FormCount < 2 then
Close;
end;
{
TApplicationManager.FormCloseQuery
---------------------------------------------------------------------------
}
procedure TApplicationManager.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
var
i: integer;
begin
for i := 0 to Screen.FormCount - 1 do
begin
if Screen.Forms[i] <> self then
begin
// Forms that have a parent will be cleaned up by that parent so
// ignore them here and only attempt to close the parent forms
if Screen.Forms[i].Parent = nil then
begin
if Screen.Forms[i].CloseQuery = false then
begin
CanClose := false;
break;
end;
end;
end;
end;
end;
{
TApplicationManager.FormClose
---------------------------------------------------------------------------
}
procedure TApplicationManager.FormClose(Sender: TObject;
var Action: TCloseAction);
var
i: integer;
begin
for i := Screen.FormCount - 1 downto 0 do
begin
if Screen.Forms[i] <> self then
begin
// Forms that have a parent will be cleaned up by that parent so
// ignore them here and only attempt to close the parent forms
if Screen.Forms[i].Parent = nil then
begin
Screen.Forms[i].Close;
end;
end;
end;
end;
到目前为止,这对我很有帮助。我确实为 Vista 做了一个小改动,因为我的“主/启动”屏幕的图标仍在显示。我不记得那是什么了。我可能不需要在初始屏幕上设置宽度、高度、启用和发送隐藏消息。我只是想确保它没有出现:-)。
处理关闭事件是必要的。如果我没记错的话,Windows 发送关闭消息时需要这样做。我认为只有主表单才能收到该信息。