Delphi onshow 主窗体/模态窗体 [英] Delphi onshow main form / modal form

查看:32
本文介绍了Delphi onshow 主窗体/模态窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,它有一个主窗体和一些其他窗体.当应用程序加载时,它需要执行一些任务并在主窗体顶部以模态形式显示结果.我遇到的问题是,如果我调用函数来执行任务/创建并在主窗体 onshow 事件中显示模态窗体,则模态窗体会出现,但主窗体直到模态窗体关闭时才会出现,即我期望会发生什么.为了解决这个问题,我在主窗体中添加了一个计时器,并在主窗体 onshow 事件上启动它,计时器调用函数来执行任务/创建并显示模态窗体.所以现在主窗体出现在模态窗体之前.

I have a project which has a main form and some other forms. When the app loads it needs to carry out some tasks and show the results in a modal form on top of the main form. The problem I have is that if I put the call to the function to do the tasks / creates and Show the modal form in the main forms onshow event the modal form appears but the main form does not until the modal form is closed, which is what I would expect to happen. To counter this I have added a timer to the main form and start it on the main forms onshow event the timer calls the function to do the tasks / create and show the modal form. So now the main form appears before the modal form.

但是我看不出这是最好的解决方案,我想知道是否有人可以提供更好的解决方案.

However I cannot see this being the best solution and was wondering if anyone could offer a better one.

我正在使用 Delphi 7

I am using Delphi 7

科林

推荐答案

一个常用的选项是在表单的 OnShow 中给自己发一条消息.像这样:

One commonly used option is to post yourself a message in the form's OnShow. Like this:

const
  WM_SHOWMYOTHERFORM = WM_USER + 0;

type
  TMyMainForm = class(TForm)
    procedure FormShow(Sender: TObject);
  protected
    procedure WMShowMyOtherForm(var Message: TMessage); message WM_SHOWMYOTHERFORM;
  end;

...


procedure TMyMainForm.FormShow(Sender: TObject);
begin
  PostMessage(Handle, WM_SHOWMYOTHERFORM, 0, 0);
end;

procedure TMyMainForm.WMShowMyOtherForm(var Message: TMessage);
begin
  inherited;
  with TMyOtherForm.Create(nil) do begin
    try
      ShowModal;
    finally
      Free;
    end;
  end;
end;

这篇关于Delphi onshow 主窗体/模态窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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