Author: Manuel Sarmiento
Displaying and using forms from a DLL can be difficult if you don't know the way.
Fortunately enough, Delphi is quite flexible and the managing forms from a DLL is
quite easy.
Answer:
The first thing you will need a Handle to the application’s main window. Assuming
that the application is running on top, you can get the window’s handle using
GetActiveWindow, like this:
MainApplicationHandle := GetActiveWindow;
In order to be able to take advantage of Delphi’s window controlling features,
however, you need a window control, not a window handle. You can get a window
control uising FindControl (Controls Unit)
1 2 fWinControl := FindControl(MainApplicationHandle);
fWinControl is assumed to be of TwinControl type..
I have found it useful to create the Modal (or modeless window) since the
begiinning:
3 4 MyForm := TMyForm.create(fWinControl);
The three items can be placed confortably in the initialization section of the DLL:
5 MainApplicationHandle := GetActiveWindow;
6 fWinControl := FindControl(MainApplicationHandle);
7 MyForm := TMyForm.create(fWinControl);
Finally, when you need the Form, you just show it, but be carfeul to redraw it if
something changed within it:
8 MyForm.Repaint;
9 winresult := MyForm.showmodal
(winresult is defined to be of typo longint) You just have to be sure that your
form includes wither buttons that produce a modal result (by setting ModalResult to
something else than mrNone)
If you’d rather prefer a modeless window (for example, to display a progress bar) ,
you can use the following approach:
10 MyForm.Visible := true;
11 while{some condition set}do12 begin13 {change something in the window}14 MyForm.repaint;
15 end;
16 MyForm.Hide;