Mega Code Archive

 
Categories / Delphi / System
 

How To Create and Manage Modal and Modeless forms in a DLL

Title: How To Create and Manage Modal and Modeless forms in a DLL Question: 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: This article is writenn in response to Mr. Fernando Silva request The first thing you will need a Handle to the applications main window. Assuming that the application is running on top, you can get the windows handle using GetActiveWindow, like this: MainApplicationHandle:= GetActiveWindow; In order to be able to take advantage of Delphis 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 youd 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;