Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show form from DLL in TScrollBox

I posted this online: Show form from DLL in TScrollBox

What i am trying to do is call and show a form in a Delphi TScrollBox. Not as Show or ShowModal

Example but not with any DLL:

Form1.Parent:= ScrollBox;
 Form1.Show;

How do i use this example from a DLL with a form inside

Can anyone provide an example?

Regards,

like image 958
Unit4 Avatar asked Feb 16 '23 08:02

Unit4


1 Answers

You cannot pass a Delphi object between a DLL and a host executable. That's because objects can only be operated on in the module in which they are created. Now, if you were using runtime packages, you'd be able to escape that limitation.

You could export a function from your DLL that created and showed the form. The function might look like this:

function ShowMyForm(ParentWindow: HWND): Pointer; stdcall;

Note that you cannot pass the parent as a Delphi object for exactly the same reasons as I describe above.

You also cannot specify that the parent of the form be a control in your executable. So you have to pass the parent's window handle.

The implementation would be like so:

function ShowMyForm(ParentWindow: HWND): Pointer; stdcall;
var
  Form: TMyForm;
begin
  Form := TMyForm.CreateParented(ParentWindow);
  Form.Show;
  Result := Pointer(Form);
end;

You would call it like this:

Form := ShowMyForm(ScrollBox.Handle);

You'd also need to supply a function to destroy the form when you are done:

procedure DestroyMyForm(Form: Pointer); stdcall;
begin
  TMyForm(Form).Free;
end;

And you need to watch out for window re-creation. If the host window is re-created then you need to manually re-create the child form.

In short, what you are attempting is rather brittle. If I were you I would look for a different approach.

like image 85
David Heffernan Avatar answered Feb 24 '23 00:02

David Heffernan