开发者

Open a Modal dialog Asynchronously in Delphi

Normally when you open a dialog using ShowModal, execution of the current thread halts until the dialog is closed. I want to display a Modal dialog, but continue execution on the current thread while the dialog is still open.

By "Modal" I simply mean that the user can not interact with any of the other forms of the application till the modal dialog is closed.

The Delphi ShowM开发者_如何学Pythonodal function provides a slightly different definition of "Modal" to the one I require:

A modal form is one where the application can't continue to run until the form is closed.

Currently I have code like this:

dialog.Parent:=self;
dialog.Show;
// keep doing stuff...

This works, except I can still interact with the parent window (move it around, close it etc)

How do I show a form that stops the user from interacting with the parent window, without using ShowModal?


Open the source code of Delphi\Source\VCL\Forms.pas and open implementation of ShowModal. Then learn how it works. I can't copy the source code here as it's an IP of CodeGear, but you can do this yourself easily and reuse parts of it's code.


Even with a modal form open, the main thread still executes (otherwise the modal form could not repaint itself).

Modal forms however have their own event loop, preventing the original application event loop from executing.

They have to (just like Windows message boxes have to as well), as otherwise you could have an event sneak back into the main event loop creating yet another modal form or messagebox.

And that kind of negates the whole point of being modal: there can be only one modal form or messagebox per UI thread.

So you need to ask yourself this question:

What actions in the main event loop does this modal form prevent from happening?

Then move those actions into a separate thread.

--jeroen


Disable your parent form as long as your dialog is visible, this will prevent users from interacting it. You can also use DisableTaskWindows to disable all the forms and not just the parent form. It is not documented but you can see how it's used in TCustomForm.ShowModal in 'forms.pas'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜