8

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?

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for that. I was just wondering if I need to set the MainFormOnTaskBar property to true on the application object contained within the DLL?
Since the application object in the DLL has no main form then I guess we don't need to or shouldn't be setting MainFormOnTaskBar to true on the application object
agreed, there is no need for that, not that it would actually matter since I presume Application.MainForm is not assigned because you never call Application.CreateForm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.