6
votes

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 ShowModal 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?

3

3 Answers

6
votes

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.

6
votes

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

5
votes

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'.