Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi XE2 assigning Application.MainForm.Handle to Application.Handle inside a DLL

I have a small issue with forms that are created from inside a DLL.

Basically what happens is when a form (Form1) from a dll is showing (I think it has to be stay on top) and you open another form (Form2) which is apart of the main application (i.e. does not live inside the dll). If you put your cursor over a control on Form2 so that the hint is displayed, Form2 will immediately go behind Form1.

This only happens with MainFormOnTaskBar is true. At the moment we are passing the main application's Application.Handle to the DLL and assigning that to Application.Handle of the DLL.

I have managed to resolve the issue by instead passing Application.MainForm.Handle to the DLL to be assigned to Application.Handle in the DLL.

Is this safe? does anyone know the proper way to fix this problem?

like image 469
There is no spoon Avatar asked Mar 01 '12 06:03

There is no spoon


1 Answers

Your solution is perfectly reasonable. I have an Excel COM add-in that does something very similar. In that code I set Application.Handle in the DLL to be the window handle of the Excel main window. That's analagous to what you are doing.

The issue is that you need to get the window ownership set correctly. You need the chain of ownership to reach all the way back to your app's main form. Forms in a DLL have no knowledge of what the main form is, and so you have to provide that knowledge.

Note that I am talking about the concept of window owner as used by Windows and not the VCL concept of owner which is totally different. In VCL terminology this is known as popup parent and you could solve your problem by explicitly setting the DLL form's popup parent to be the main form. The relevant properties are PopupMode and PopupParent. For the forms that live in the main app, the VCL will naturally make their popup parent be the main form.

However, having talked about explicitly setting popup parent, I would stress that your current solution is simpler and more convenient.

What both of these solutions do is to make sure that all auxiliary forms are owned by the main form. That means that these forms are always on top of the main form. It means that the auxiliary forms will be minimized if the main form is minimized. Read about owned windows here: Window Features.

Incidentally, if you had been using runtime packages rather than a DLL, the code in the package would be connected to the same VCL as the main form. So the packaged code would be able to see the main form and set the window owner appropriately. This is certainly one advantage of using packages. Of course, there may very well be a good reason why you need to use DLLs rather than packages.

like image 176
David Heffernan Avatar answered Oct 16 '22 15:10

David Heffernan