Form in DLL: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Form in DLL
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The following application and library demonstrate you how to show a modal and a non-modal form and create a custom component on application form from a DLL library. Tested on win32 and Linux/Gtk2 with Lazarus 1.7 trunk. | |
| 2 | + | ||
| 3 | + | Main application: | |
| 4 | + | ||
| 5 | + | ``` Pascal | |
| 6 | + | program MainApp; | |
| 7 | + | ||
| 8 | + | uses | |
| 9 | + | Interfaces, Classes, LCLType, Controls, StdCtrls, Forms, ExtCtrls; | |
| 10 | + | ||
| 11 | + | type | |
| 12 | + | TEnableDisableFormsCallBack = procedure(var FormList: Pointer); | |
| 13 | + | TCreateButtonCallBack = procedure(Caption: PChar; OnClick: TProcedure); | |
| 14 | + | ||
| 15 | + | const | |
| 16 | + | {$IFDEF WINDOWS} | |
| 17 | + | DLLDialogLib = 'DLLDialog.dll'; | |
| 18 | + | {$ELSE} | |
| 19 | + | DLLDialogLib = 'DLLDialog.so'; | |
| 20 | + | {$ENDIF} | |
| 21 | + | ||
| 22 | + | procedure DLLDialog_Init(DisableFormsCallBack, EnableFormsCallback: TEnableDisableFormsCallBack); external DLLDialogLib; | |
| 23 | + | procedure DLLDialog_Final; external DLLDialogLib; | |
| 24 | + | procedure DLLDialog_ShowModal(ParentWindow: HWND); external DLLDialogLib; | |
| 25 | + | procedure DLLDialog_Show(ParentWindow: HWND); external DLLDialogLib; | |
| 26 | + | procedure DLLDialog_CreateDLLButton(ParentWindow: HWND); external DLLDialogLib; | |
| 27 | + | procedure DLLDialog_CreateButton(CreateButtonCallBack: TCreateButtonCallBack); external DLLDialogLib; | |
| 28 | + | ||
| 29 | + | type | |
| 30 | + | TMainForm = class(TForm) | |
| 31 | + | private | |
| 32 | + | PnlParent: TPanel; | |
| 33 | + | ||
| 34 | + | procedure BtnAddDLLButtonClick(Sender: TObject); | |
| 35 | + | procedure BtnAddButtonClick(Sender: TObject); | |
| 36 | + | procedure ShowModalDLLDialog(Sender: TObject); | |
| 37 | + | procedure ShowDLLDialog(Sender: TObject); | |
| 38 | + | public | |
| 39 | + | constructor Create(aOwner: TComponent); override; | |
| 40 | + | end; | |
| 41 | + | ||
| 42 | + | var | |
| 43 | + | MainForm: TMainForm; | |
| 44 | + | ||
| 45 | + | { TMainForm } | |
| 46 | + | ||
| 47 | + | procedure DisableFormsCallBack(var FormList: Pointer); | |
| 48 | + | begin | |
| 49 | + | FormList := Screen.DisableForms(nil, TList(FormList)); | |
| 50 | + | end; | |
| 51 | + | ||
| 52 | + | procedure EnableFormsCallback(var FormList: Pointer); | |
| 53 | + | begin | |
| 54 | + | Screen.EnableForms(TList(FormList)); | |
| 55 | + | end; | |
| 56 | + | ||
| 57 | + | constructor TMainForm.Create(aOwner: TComponent); | |
| 58 | + | var | |
| 59 | + | BtnShow, BtnShowModal, BtnAddDLLButton, BtnAddButton: TButton; | |
| 60 | + | begin | |
| 61 | + | inherited CreateNew(aOwner); | |
| 62 | + | ||
| 63 | + | Position := poWorkAreaCenter; | |
| 64 | + | Width := 600; | |
| 65 | + | Height := 200; | |
| 66 | + | ||
| 67 | + | BtnShow := TButton.Create(Self); | |
| 68 | + | BtnShow.Parent := Self; | |
| 69 | + | BtnShow.Caption := 'Show form'; | |
| 70 | + | BtnShow.AutoSize := True; | |
| 71 | + | BtnShow.OnClick := @ShowDLLDialog; | |
| 72 | + | ||
| 73 | + | BtnShowModal := TButton.Create(Self); | |
| 74 | + | BtnShowModal.Parent := Self; | |
| 75 | + | BtnShowModal.Caption := 'Show modal form'; | |
| 76 | + | BtnShowModal.AutoSize := True; | |
| 77 | + | BtnShowModal.OnClick := @ShowModalDLLDialog; | |
| 78 | + | BtnShowModal.AnchorSide[akLeft].Control := BtnShow; | |
| 79 | + | BtnShowModal.AnchorSide[akLeft].Side := asrRight; | |
| 80 | + | BtnShowModal.BorderSpacing.Left := 10; | |
| 81 | + | ||
| 82 | + | BtnAddDLLButton := TButton.Create(Self); | |
| 83 | + | BtnAddDLLButton.Parent := Self; | |
| 84 | + | BtnAddDLLButton.Caption := 'Create real DLL button'; | |
| 85 | + | BtnAddDLLButton.AutoSize := True; | |
| 86 | + | BtnAddDLLButton.OnClick := @BtnAddDLLButtonClick; | |
| 87 | + | BtnAddDLLButton.AnchorSide[akLeft].Control := BtnShowModal; | |
| 88 | + | BtnAddDLLButton.AnchorSide[akLeft].Side := asrRight; | |
| 89 | + | BtnAddDLLButton.BorderSpacing.Left := 10; | |
| 90 | + | ||
| 91 | + | BtnAddButton := TButton.Create(Self); | |
| 92 | + | BtnAddButton.Parent := Self; | |
| 93 | + | BtnAddButton.Caption := 'Create fake DLL button'; | |
| 94 | + | BtnAddButton.AutoSize := True; | |
| 95 | + | BtnAddButton.OnClick := @BtnAddButtonClick; | |
| 96 | + | BtnAddButton.AnchorSide[akLeft].Control := BtnAddDLLButton; | |
| 97 | + | BtnAddButton.AnchorSide[akLeft].Side := asrRight; | |
| 98 | + | BtnAddButton.BorderSpacing.Left := 10; | |
| 99 | + | ||
| 100 | + | PnlParent := TPanel.Create(Self); | |
| 101 | + | PnlParent.Parent := Self; | |
| 102 | + | PnlParent.AnchorSide[akTop].Control := BtnShow; | |
| 103 | + | PnlParent.AnchorSide[akTop].Side := asrBottom; | |
| 104 | + | PnlParent.BorderSpacing.Top := 10; | |
| 105 | + | PnlParent.Width := 220; | |
| 106 | + | end; | |
| 107 | + | ||
| 108 | + | procedure CreateButtonCallBack(ACaption: PChar; AOnClick: TProcedure); | |
| 109 | + | var | |
| 110 | + | Btn: TButton; | |
| 111 | + | MyMethod: TMethod; | |
| 112 | + | begin | |
| 113 | + | Btn := TButton.Create(MainForm); | |
| 114 | + | Btn.Caption := ACaption; | |
| 115 | + | Btn.Left := 100; | |
| 116 | + | Btn.Width := 100; | |
| 117 | + | Btn.Height := 20; | |
| 118 | + | MyMethod.Code := AOnClick; | |
| 119 | + | MyMethod.Data := nil; | |
| 120 | + | Btn.OnClick := TNotifyEvent(MyMethod); | |
| 121 | + | Btn.Parent := MainForm.PnlParent; | |
| 122 | + | end; | |
| 123 | + | ||
| 124 | + | procedure TMainForm.BtnAddButtonClick(Sender: TObject); | |
| 125 | + | begin | |
| 126 | + | DLLDialog_CreateButton(@CreateButtonCallBack); | |
| 127 | + | end; | |
| 128 | + | ||
| 129 | + | procedure TMainForm.BtnAddDLLButtonClick(Sender: TObject); | |
| 130 | + | begin | |
| 131 | + | DLLDialog_CreateDLLButton(PnlParent.Handle); | |
| 132 | + | end; | |
| 133 | + | ||
| 134 | + | procedure TMainForm.ShowDLLDialog(Sender: TObject); | |
| 135 | + | begin | |
| 136 | + | DLLDialog_Show(0); | |
| 137 | + | end; | |
| 138 | + | ||
| 139 | + | procedure TMainForm.ShowModalDLLDialog(Sender: TObject); | |
| 140 | + | begin | |
| 141 | + | DLLDialog_ShowModal(Self.Handle); | |
| 142 | + | end; | |
| 143 | + | ||
| 144 | + | {$R *.res} | |
| 145 | + | ||
| 146 | + | begin | |
| 147 | + | Application.Initialize; | |
| 148 | + | Application.CreateForm(TMainForm, MainForm); | |
| 149 | + | DLLDialog_Init(@DisableFormsCallBack, @EnableFormsCallback); | |
| 150 | + | try | |
| 151 | + | Application.Run; | |
| 152 | + | finally | |
| 153 | + | DLLDialog_Final; | |
| 154 | + | end; | |
| 155 | + | end. | |
| 156 | + | ``` | |
| 157 | + | ||
| 158 | + | DLL with a form: | |
| 159 | + | ||
| 160 | + | ``` Pascal | |
| 161 | + | library DllDialog; | |
| 162 | + | ||
| 163 | + | {$mode objfpc}{$H+} | |
| 164 | + | ||
| 165 | + | uses | |
| 166 | + | Interfaces, Classes, LCLType, StdCtrls, Controls, Forms, Dialogs; | |
| 167 | + | ||
| 168 | + | type | |
| 169 | + | TDLLDialog = class(TForm) | |
| 170 | + | protected | |
| 171 | + | procedure CreateParams(var Params: TCreateParams); override; | |
| 172 | + | public | |
| 173 | + | procedure BtnShowMessageClick(Sender: TObject); | |
| 174 | + | constructor Create(aOwner: TComponent); override; | |
| 175 | + | public | |
| 176 | + | ParentFormHandle: HWND; | |
| 177 | + | end; | |
| 178 | + | ||
| 179 | + | TEnableDisableFormsCallBack = procedure(var FormList: Pointer); | |
| 180 | + | TCreateButtonCallBack = procedure(Caption: PChar; OnClick: TProcedure); | |
| 181 | + | ||
| 182 | + | TApplicationCallback = class(TComponent) | |
| 183 | + | private | |
| 184 | + | DisableFormsCallBack: TEnableDisableFormsCallBack; | |
| 185 | + | EnableFormsCallback: TEnableDisableFormsCallBack; | |
| 186 | + | FormList: Pointer; | |
| 187 | + | public | |
| 188 | + | procedure DisableForms(Sender: TObject); | |
| 189 | + | procedure EnableForms(Sender: TObject); | |
| 190 | + | procedure BtnClick(Sender: TObject); | |
| 191 | + | ||
| 192 | + | constructor Create(aOwner: TComponent); override; | |
| 193 | + | destructor Destroy; override; | |
| 194 | + | end; | |
| 195 | + | ||
| 196 | + | var | |
| 197 | + | ApplicationCallback: TApplicationCallback; | |
| 198 | + | ||
| 199 | + | procedure DLLDialog_Init(DisableFormsCallBack, EnableFormsCallback: TEnableDisableFormsCallBack); | |
| 200 | + | begin | |
| 201 | + | ApplicationCallback := TApplicationCallback.Create(nil); | |
| 202 | + | ApplicationCallback.DisableFormsCallBack := DisableFormsCallBack; | |
| 203 | + | ApplicationCallback.EnableFormsCallback := EnableFormsCallback; | |
| 204 | + | end; | |
| 205 | + | ||
| 206 | + | procedure DLLDialog_Final; | |
| 207 | + | begin | |
| 208 | + | ApplicationCallback.Free; | |
| 209 | + | end; | |
| 210 | + | ||
| 211 | + | procedure DLLDialog_ShowModal(ParentWindow: HWND); | |
| 212 | + | var | |
| 213 | + | DLLDialog: TDLLDialog; | |
| 214 | + | begin | |
| 215 | + | DLLDialog := TDLLDialog.Create(ApplicationCallback); | |
| 216 | + | try | |
| 217 | + | DLLDialog.ParentFormHandle := ParentWindow; | |
| 218 | + | DLLDialog.ShowModal; | |
| 219 | + | finally | |
| 220 | + | DLLDialog.Free; | |
| 221 | + | end; | |
| 222 | + | end; | |
| 223 | + | ||
| 224 | + | procedure DLLDialog_Show(ParentWindow: HWND); | |
| 225 | + | var | |
| 226 | + | DLLDialog: TDLLDialog; | |
| 227 | + | begin | |
| 228 | + | DLLDialog := TDLLDialog.Create(ApplicationCallback); | |
| 229 | + | DLLDialog.ParentFormHandle := ParentWindow; | |
| 230 | + | DLLDialog.Show; | |
| 231 | + | end; | |
| 232 | + | ||
| 233 | + | procedure DLLDialog_CreateDLLButton(ParentWindow: HWND); | |
| 234 | + | var | |
| 235 | + | Btn: TButton; | |
| 236 | + | BtnParentForm: TForm; | |
| 237 | + | begin | |
| 238 | + | BtnParentForm := TForm.CreateNew(ApplicationCallback); | |
| 239 | + | BtnParentForm.ParentWindow := ParentWindow; | |
| 240 | + | BtnParentForm.Width := 100; | |
| 241 | + | BtnParentForm.Height := 20; | |
| 242 | + | BtnParentForm.BorderStyle := bsNone; | |
| 243 | + | BtnParentForm.Visible := True; | |
| 244 | + | ||
| 245 | + | Btn := TButton.Create(ApplicationCallback); | |
| 246 | + | Btn.Caption := 'Real DLL Button'; | |
| 247 | + | Btn.Width := BtnParentForm.Width; | |
| 248 | + | Btn.Height := BtnParentForm.Height; | |
| 249 | + | Btn.OnClick := @ApplicationCallback.BtnClick; | |
| 250 | + | Btn.Parent := BtnParentForm; | |
| 251 | + | end; | |
| 252 | + | ||
| 253 | + | procedure FakeBtnClick; | |
| 254 | + | begin | |
| 255 | + | ShowMessage('You clicked a fake button from a DLL!'); | |
| 256 | + | end; | |
| 257 | + | ||
| 258 | + | procedure DLLDialog_CreateButton(CreateButtonCallBack: TCreateButtonCallBack); | |
| 259 | + | begin | |
| 260 | + | CreateButtonCallBack('Fake DLL Button', @FakeBtnClick); | |
| 261 | + | end; | |
| 262 | + | ||
| 263 | + | exports | |
| 264 | + | DLLDialog_Init, DLLDialog_Final, DLLDialog_ShowModal, DLLDialog_Show, | |
| 265 | + | DLLDialog_CreateButton, DLLDialog_CreateDLLButton; | |
| 266 | + | ||
| 267 | + | { TApplicationCallback } | |
| 268 | + | ||
| 269 | + | constructor TApplicationCallback.Create(aOwner: TComponent); | |
| 270 | + | begin | |
| 271 | + | inherited Create(aOwner); | |
| 272 | + | ||
| 273 | + | Application.AddOnModalBeginHandler(@DisableForms); | |
| 274 | + | Application.AddOnModalEndHandler(@EnableForms); | |
| 275 | + | end; | |
| 276 | + | ||
| 277 | + | procedure TApplicationCallback.BtnClick(Sender: TObject); | |
| 278 | + | begin | |
| 279 | + | ShowMessage('You clicked a real button from a DLL!'); | |
| 280 | + | end; | |
| 281 | + | ||
| 282 | + | destructor TApplicationCallback.Destroy; | |
| 283 | + | begin | |
| 284 | + | Application.RemoveOnModalBeginHandler(@DisableForms); | |
| 285 | + | Application.RemoveOnModalEndHandler(@EnableForms); | |
| 286 | + | ||
| 287 | + | inherited Destroy; | |
| 288 | + | end; | |
| 289 | + | ||
| 290 | + | procedure TApplicationCallback.DisableForms(Sender: TObject); | |
| 291 | + | begin | |
| 292 | + | DisableFormsCallBack(FormList); | |
| 293 | + | end; | |
| 294 | + | ||
| 295 | + | procedure TApplicationCallback.EnableForms(Sender: TObject); | |
| 296 | + | begin | |
| 297 | + | EnableFormsCallback(FormList); | |
| 298 | + | end; | |
| 299 | + | ||
| 300 | + | { TDLLDialog } | |
| 301 | + | ||
| 302 | + | constructor TDLLDialog.Create(aOwner: TComponent); | |
| 303 | + | var | |
| 304 | + | BtnShowMessage: TButton; | |
| 305 | + | begin | |
| 306 | + | inherited CreateNew(aOwner); | |
| 307 | + | ||
| 308 | + | Caption := 'This form is in a DLL !!!'; | |
| 309 | + | Position := poWorkAreaCenter; | |
| 310 | + | Width := 200; | |
| 311 | + | Height := 100; | |
| 312 | + | ||
| 313 | + | BtnShowMessage := TButton.Create(Self); | |
| 314 | + | BtnShowMessage.Parent := Self; | |
| 315 | + | BtnShowMessage.Caption := 'Show message'; | |
| 316 | + | BtnShowMessage.AutoSize := True; | |
| 317 | + | BtnShowMessage.OnClick := @BtnShowMessageClick; | |
| 318 | + | end; | |
| 319 | + | ||
| 320 | + | procedure TDLLDialog.BtnShowMessageClick(Sender: TObject); | |
| 321 | + | begin | |
| 322 | + | ShowMessage('Hello from DLL!'); | |
| 323 | + | end; | |
| 324 | + | ||
| 325 | + | procedure TDLLDialog.CreateParams(var Params: TCreateParams); | |
| 326 | + | begin | |
| 327 | + | inherited CreateParams(Params); | |
| 328 | + | ||
| 329 | + | {$IFDEF LCLWin32} | |
| 330 | + | Params.WndParent := ParentFormHandle; | |
| 331 | + | {$ENDIF} | |
| 332 | + | end; | |
| 333 | + | ||
| 334 | + | begin | |
| 335 | + | Application.Initialize; | |
| 336 | + | end. | |
| 337 | + | ``` | |
| 338 | + | ||
| 339 | + | --- | |
| 340 | + | *From the Free Pascal wiki, page "Form in DLL", as it stood on 2020-02-15 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
