Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it not advisable to change the formstyle at runtime?

In the Delphi documentation about the TCustomForm.FormStyle a note is added that states: "It is not advisable to change FormStyle at runtime."

I would like to know why it is not advisable to change the formstyle at runtime. Is it not advisable from a user interface point of view or are there some technical issues that could occur? And what kind of issues could occur after changing the formstyle at runtime?

Use case

We would like to improve the multi monitor support of our MDI application. Currently all child windows are opened within the main window (MDI Parent window). What the users would like is to be able to open/move a child window outside the main window so that the child window will be opened as separate floating window that can be located anywhere at the Windows desktop.

I have seen examples of this functionality in Adobe Photoshop, Google chrome and Microsoft Internet Explorer. (Tabs can be moved from the tabbar and will be opened in a separate floating window)

like image 460
Lebbers Avatar asked Feb 14 '13 09:02

Lebbers


2 Answers

Changing the FormStyle at runtime from fsNormal to fsMDIform or fsMDIChild requires re-creating the window handle, and that in turn requires the re-creation of all window handles for all controls on your form. There are lot's of things outside of Delphi's control when that happens: maybe you're using some 3rd party control that relies on some Windows control yet the 3rd party control doesn't know how to save it's state. The user would loose all work when the Window handle is re-created.

Fortunately you can work around the problem:

  • Stop using MDI, make all your windows "float" and deal with them some other way. MDI has been out-of-fashion for some time.
  • Just do it, depending on the kind of controls you have, it might work fine. If it doesn't, see what went wrong and work around it (example: you might need to save some control's state yourself and restore it after the swap)
  • Try putting everything on a frame; When the user wants to move, create a new empty non-MDI/MDI form, re-parent the frame, destroy the old Form.
like image 68
Cosmin Prund Avatar answered Nov 15 '22 07:11

Cosmin Prund


Most likely it's because a form style change will lead to window handle re-creation. And this will force re-creation of the handles of all child windows too.

Window re-creation is by and large something that can happen without you noticing. But there are controls which have problems with re-creation. I've suffered from this with the toolbar control in the past, for example. More recent versions of Delphi are more resilient to re-creation.

like image 24
David Heffernan Avatar answered Nov 15 '22 07:11

David Heffernan