Mirror

Create and Manage Modal and Modeless forms in a DLL (Views: 699)


Problem/Question/Abstract:

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)

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:

MyForm := TMyForm.create(fWinControl);

The three items can be placed confortably in the initialization section of the DLL:

MainApplicationHandle := GetActiveWindow;
fWinControl := FindControl(MainApplicationHandle);
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:

MyForm.Repaint;
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:

MyForm.Visible := true;
while {some condition set} do
begin
  {change something in the window}
  MyForm.repaint;
end;
MyForm.Hide;

<< Back to main page