我的应用程式主窗体可见后,如何直接进行对话框? [英] How can I make a a dialog box happen directly after my app's main form is visible?

查看:122
本文介绍了我的应用程式主窗体可见后,如何直接进行对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 TForm OnActivate 事件,让我有机会尽快显示一个对话框因为我的应用程序已经开始我想要主窗体已经被加载可见。这是一个很好的方法?



我发现 OnActivate 正常工作,除非表单 WindowState wsMaximized



过去我已经完成了想要以各种方式,但我希望有一个更好的方法。



这对我有用:

  procedure TForm1.FormCreate(Sender:TObject); 
begin
Application.OnIdle:= OnIdle;
结束

程序TForm1.OnIdle(发件人:TObject; var Done:Boolean);
begin
Application.OnIdle:= nil;
form2:= TForm2.Create(Application);
form2.ShowModal;
结束

有没有更好的方法?

解决方案

在表单的OnCreate事件处理程序中发布用户消息,并在消息的处理程序中显示对话框:

 code> unit Unit1; 

接口

const
UM_DLG = WM_USER + $ 100;

type
TForm1 = class(TForm)
...
procedure UMDlg(var Msg:TMessage);消息UM_DLG;
...

实现

程序TForm1.FormCreate(发件人:TObject);
begin
PostMessage(Handle,UM_DLG,0,0);
结束

程序TForm1.UMDlg(var Msg:TMessage);
begin
form2:= TForm2.Create(Application);
form2.ShowModal;
结束

虽然我发现定时器方法更好:只需在表单上放置一个定时器组件,将间隔设置为100 (ms)并实现OnTimer事件:

  procedure Timer1Timer(Sender:TObject); 
begin
Timer1.Enabled:= False; //停止计时器 - 应该执行一次

form2:= TForm2.Create(Application);
form2.ShowModal;
结束






这两种方法之间的区别是: p>

当用户消息从OnCreate或OnShow处理程序发布时,消息将以正常的优先级发送,这意味着其他窗口初始化消息可能会被发布和处理。实质上,WM_PAINT消息将在UM_DLG消息之后处理。如果UM_DLG消息需要很长时间才能处理而不抽取消息队列(例如,打开数据库连接),则表单将显示为空,而不显示客户区。



WM_TIMER消息是一个低优先级的消息,而不是首先处理表单初始化消息,只有WM_TIMER消息将被处理,即使WM_TIMER消息被发布在表单创建完成。


I have been using TForm's OnActivate event to give me the opportunity to show a dialog box as soon as my app has started. I want the main form to already be loaded & visible. What's a good way to do this?

I have found that OnActivate works fine unless the forms WindowState is wsMaximized.

in the past I've accomplished what I want in various ways but I expect there is a better way.

Here's what worked for me:

procedure TForm1.FormCreate(Sender: TObject);  
begin 
  Application.OnIdle:=OnIdle; 
end; 

procedure TForm1.OnIdle(Sender: TObject; var Done: Boolean); 
begin 
  Application.OnIdle:=nil; 
  form2:=TForm2.Create(Application); 
  form2.ShowModal; 
end;

Is there a better way?

解决方案

In the form's OnCreate event handler post a user message and show the dialog in the handler of the message:

unit Unit1;

interface

const
  UM_DLG = WM_USER + $100;

type
  TForm1 = class(TForm)
  ...
  procedure UMDlg(var Msg: TMessage); message UM_DLG;
...

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
  PostMessage(Handle, UM_DLG, 0, 0);
end;

procedure TForm1.UMDlg(var Msg: TMessage);
begin
  form2 := TForm2.Create(Application); 
  form2.ShowModal; 
end;

Although I found timer approach even better: just drop a timer component on the form, set Interval to 100 (ms) and implement OnTimer event:

procedure Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := False; // stop the timer - should be executed only once

  form2 := TForm2.Create(Application); 
  form2.ShowModal;
end;


The difference between the two approaches is:

When user message is posted either from OnCreate or OnShow handler, the message is dispatched with normal priority which means that other window initialization messages might get posted and handled after it. For essence, WM_PAINT messages would be handled after UM_DLG message. If UM_DLG message is taking long time to process without pumping the message queue (for example, opening db connection), then form would be shown blank without client area painted.

WM_TIMER message is a low-priority message and than means that form initialization messages would be handled first and only then WM_TIMER message would be processed, even if WM_TIMER message is posted before the form creation completes.

这篇关于我的应用程式主窗体可见后,如何直接进行对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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