在窗体上显示窗体时触发的事件

时间:2022-08-17 00:02:58

I have a form that takes some time to create and I would like to show the user that something is happening and I have made a hourglass cursor when I call the function that shows the form. But my problem is that I can't get the

我有一个需要一些时间来创建的表单,我想向用户显示正在发生的事情,当我调用显示表单的函数时,我已经制作了一个沙漏光标。但我的问题是我无法得到

Screen.Cursor := crDefault;

to execute first when the form is actually displayed on screen. I have tried all events that makes a bit sense trying but with no luck. The hourglass is gone before the form is visible.

在窗体实际显示在屏幕上时首先执行。我尝试了所有尝试有点意义但没有运气的事件。在表单可见之前,沙漏消失了。

What can I try next to get this working.

我可以尝试下一步来实现这一目标。

I you need more info please say so and I will find it.

我需要更多信息请说明,我会找到它。

I set the hourglass in my mainform before calling the function that creates the form and then I have my form show as below.

我在调用创建表单的函数之前在我的mainform中设置了沙漏,然后我的表单显示如下。

procedure TfrmJsCv.FormShow(Sender: TObject);
begin
  fReportFile := Folders.DirDatabase + 'jobsearch_print.mdb';
  btnPrint.Visible := FileExists(fReportFile);
  try
    SetupMain;
    SetupCertificates;
    SetupContacts;
    SetupCourse;
    SetupJobs;
    SetupLanguage;
    SetupLanguageLevels;
    SetupItKnowledge;
    SetupKnowledgeLevels;
    SetupKnowledgeTypes;
    SetupOther;
    SetupReferences;
    SetupSchool;
    SetupYears;
  except
    on E: exception do
      Logfile.Error('F_JsCv.FormShow: ' + E.Message);
  end;
  PagCurriculumVitae.ActivePageIndex := 0;
end;

I have tried to put Screen.Cursor := crDefault; on the OnActivate, OnCreate, OnShow, OnResize, OnPaint events but the hourglass is reset before the form is ready/visible on screen

我试图把Screen.Cursor:= crDefault;在OnActivate,OnCreate,OnShow,OnResize,OnPaint事件上,但在窗体准备好/在屏幕上可见之前重置沙漏

All the Setupxxxxx procedures is procedures that prepares data for the form that is rather complex - it collects data from 12 different tables.

所有Setupxxxxx过程都是为相当复杂的表单准备数据的过程 - 它从12个不同的表中收集数据。

1 个解决方案

#1


Are you showing the form modally? In that case the crHourGlass setting is cancelled by the call to ShowModal where a call Screen.Cursor := crDefault; is made . See Vcl.Forms unit, function TCustomForm.ShowModal: Integer; line 7139 (XE7 source).

你是否以模态方式显示表单?在这种情况下,通过调用ShowModal取消crHourGlass设置,其中调用Screen.Cursor:= crDefault;是。参见Vcl.Forms单元,函数TCustomForm.ShowModal:Integer;第7139行(XE7来源)。

The cure would be to call Screen.Cursor := crHourGlass; in TfrmJsCv.FormShow() before all those SetupXXX calls and Screen.Cursor := crDefault; after.

治愈方法是调用Screen.Cursor:= crHourGlass;在所有那些SetupXXX调用和Screen.Cursor:= crDefault之前的TfrmJsCv.FormShow()中;后。

Now that OP confirmed that the form was shown modally, I think some more explanation may be beneficial for future readers.

既然OP确认表格是模态显示的,我认为一些更多的解释可能对未来的读者有益。

OP said:

"The hourglass is gone before the form is visible."
"I set the hourglass in my mainform before calling the function that creates the form ..."

“在表格可见之前,沙漏就消失了。” “在调用创建表单的函数之前,我在mainform中设置了沙漏......”

Thus he has something like this in the main form:

因此他在主要形式中有类似的东西:

  Screen.Cursor := crHourGlass;
  frmJsCv.ShowModal

Which ends up in Vcl.Forms.TCustomForm.ShowModal

最终在Vcl.Forms.TCustomForm.ShowModal中

...
SaveCursor := Screen.Cursor;
Screen.Cursor := crDefault;
SaveCount := Screen.CursorCount;
...
try
  Show;  // 
  try
    // message loop
  finally
    Hide;
  end;
finally
  if Screen.CursorCount = SaveCount then
    Screen.Cursor := SaveCursor
  else Screen.Cursor := crDefault;
  ...
end;

The purpose is obviously to let the modal form manipulate the cursor as needed and then to restore the cursor to what it was before the call to ShowModal. This has been the same at least from Delphi 7.

显然,目的是让模态形式根据需要操作光标,然后将光标恢复到调用ShowModal之前的状态。这与Delphi 7至少相同。

Note that ordinary Show doesn't do the above excersize.

请注意,普通Show不会执行以上操作。

Another effect of setting crHourGlass before the call to ShowModal, and not setting crDefaultback afterwards would be that the cursor would still not be shown as an hourglass while the form is shown, but immediately afterwards. This could be as confusing as this explanation :)

在调用ShowModal之前设置crHourGlass,而不是之后设置crDefaultback的另一个影响是,在显示表单时,光标仍然不会显示为沙漏,但之后会立即显示。这可能像这个解释一样令人困惑:)

#1


Are you showing the form modally? In that case the crHourGlass setting is cancelled by the call to ShowModal where a call Screen.Cursor := crDefault; is made . See Vcl.Forms unit, function TCustomForm.ShowModal: Integer; line 7139 (XE7 source).

你是否以模态方式显示表单?在这种情况下,通过调用ShowModal取消crHourGlass设置,其中调用Screen.Cursor:= crDefault;是。参见Vcl.Forms单元,函数TCustomForm.ShowModal:Integer;第7139行(XE7来源)。

The cure would be to call Screen.Cursor := crHourGlass; in TfrmJsCv.FormShow() before all those SetupXXX calls and Screen.Cursor := crDefault; after.

治愈方法是调用Screen.Cursor:= crHourGlass;在所有那些SetupXXX调用和Screen.Cursor:= crDefault之前的TfrmJsCv.FormShow()中;后。

Now that OP confirmed that the form was shown modally, I think some more explanation may be beneficial for future readers.

既然OP确认表格是模态显示的,我认为一些更多的解释可能对未来的读者有益。

OP said:

"The hourglass is gone before the form is visible."
"I set the hourglass in my mainform before calling the function that creates the form ..."

“在表格可见之前,沙漏就消失了。” “在调用创建表单的函数之前,我在mainform中设置了沙漏......”

Thus he has something like this in the main form:

因此他在主要形式中有类似的东西:

  Screen.Cursor := crHourGlass;
  frmJsCv.ShowModal

Which ends up in Vcl.Forms.TCustomForm.ShowModal

最终在Vcl.Forms.TCustomForm.ShowModal中

...
SaveCursor := Screen.Cursor;
Screen.Cursor := crDefault;
SaveCount := Screen.CursorCount;
...
try
  Show;  // 
  try
    // message loop
  finally
    Hide;
  end;
finally
  if Screen.CursorCount = SaveCount then
    Screen.Cursor := SaveCursor
  else Screen.Cursor := crDefault;
  ...
end;

The purpose is obviously to let the modal form manipulate the cursor as needed and then to restore the cursor to what it was before the call to ShowModal. This has been the same at least from Delphi 7.

显然,目的是让模态形式根据需要操作光标,然后将光标恢复到调用ShowModal之前的状态。这与Delphi 7至少相同。

Note that ordinary Show doesn't do the above excersize.

请注意,普通Show不会执行以上操作。

Another effect of setting crHourGlass before the call to ShowModal, and not setting crDefaultback afterwards would be that the cursor would still not be shown as an hourglass while the form is shown, but immediately afterwards. This could be as confusing as this explanation :)

在调用ShowModal之前设置crHourGlass,而不是之后设置crDefaultback的另一个影响是,在显示表单时,光标仍然不会显示为沙漏,但之后会立即显示。这可能像这个解释一样令人困惑:)