How can I move a Delphi form in front of another?

I have an application that always shows at least two forms at a time.

As a rule, I have a list of tasks in a child form of the main form with the corresponding data specified in the main form. Other parts can be controlled using ShowModal.

The current problem is that users recently asked to drag the main form in front of the task list.

As far as I can see, since the task list form is created after the main form and is called for display from the main form, it gets a "front" position.

Can this be changed at runtime i.e. Is it possible to set the active form to the fore?

+4
source share
2 answers

Note For the rest of this answer, the terminology used uses the meaning used by the Windows documentation . This is different from the meaning of the same term in VCL.

What happens is that your job list window is a top-level window.

Ownership places several restrictions on the window.

  • A native window is always located above its owner in z-order.
  • The system automatically destroys the owned window when its owner is destroyed.
  • The own window is hidden when its owner is minimized.

If you want the window of your task list to be under the main window in z-order, it cannot belong to the main window. You can achieve this as follows:

class TJobListForm = class(...) protected procedure CreateParams(var Params: TCreateParams); override; ... procedure TJobListForm.CreateParams(var Params: TCreateParams); begin inherited; Params.WndParent := Application.Handle; end; 

This makes the task list window a top-level window located in a hidden application window. Or alternatively:

 procedure TJobListForm.CreateParams(var Params: TCreateParams); begin inherited; Params.WndParent := 0; end; 

This makes the job list window an unoccupied top-level window. So now he gets a taskbar button.

Such changes will have far-reaching consequences for your application. The second and third bullet points in the list above are obvious consequences. The behavior of your windows while minimizing and their interaction with the taskbar will also be affected. I just scratched the surface. You will probably find that turning the window of your task list into the main window does not affect your program. You will find that Windows does a lot of work behind the scenes for the window owned. You may need to reproduce some of these works if you go to an unoccupied window.

+5
source

While the job list form is not a child of any other window (i.e. its Parent property is not set, and the CreateParams() method is not overridden to set the TCreateParams.WndParent field), and this is displayed using the Show() method instead ShowModal() method, then the user should be able to freely switch between the two windows as desired.

+2
source

Source: https://habr.com/ru/post/1379163/


All Articles