4

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,

Unit4
  • 85
  • 1
  • 5

3 Answers3

6

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • You guy’s are grate, I will play around with the code and post the results, thanks – Unit4 Jun 04 '13 at 08:47
  • Actually i am able to create and display all that I want in the dll forms using procedures and or functions, all I want to do is show the completed dll form inside my main form ScrollBox. I can show the dll form using a show procedure and or ShowModal as a function. I am unable to show the form inside a delphi ScrollBOX on my EXE Main Form. – Unit4 Jun 04 '13 at 09:43
  • I don't understand what you mean – David Heffernan Jun 04 '13 at 09:47
  • @DavidHeffernan, to me it seems he doesn't understand how to pass the scrollbox window handle as the parameter ParentWindow of the dll function showMyForm. – Peter Jun 04 '13 at 11:27
  • 1
    @PeterVonča Maybe. I added a sample call. – David Heffernan Jun 04 '13 at 11:28
  • 1
    already added +1 before, would give you another one if possible just for responding so dam fast, I like just wrote the comment and like 5 seconds later you already edited answer and replied to comment, like wtf fast fingers :D – Peter Jun 04 '13 at 11:32
  • OK, I have already created forms in a dll that i can call and display as Show and ShowModal, i am using Delphi 7. I have added databases to the forms where i can read, write, delete etc. All seem to work grate. Not using any dll files, i would normally use a TscrollBox to display most of my forms in my project example: Form1.Parent:= ScrollBox; Form1.Show; I want to call the forms from a dll file and display them the same way as the above code but with the necessary code structure. – Unit4 Jun 04 '13 at 12:28
  • What are you struggling with. You cannot call `Form.Show` on a `Form` variable that was created in your DLL. I explained that. You need to do it the way I said in the answer. – David Heffernan Jun 04 '13 at 12:41
  • I know i can't call the show or showmodal from the dll, i used: procedure ShowForm;stdcall;external 'Project1dll.dll' name 'ShowDllForm'; but i want to displayed it in a ScrollBox – Unit4 Jun 04 '13 at 12:53
  • So pass in the window handle of the scroll box like I said. Try and work it out in process first. – David Heffernan Jun 04 '13 at 13:06
2

problem solved and here is the code:

//This is the DLL

library Project1dll;

uses    
  SysUtils,    
  Windows,    
  Classes,    
  DllForm in 'DllForm.pas' {frmDllForm}; // this is the other form

procedure Create_Form(ph: HWND);    
begin    
  frmDllForm:= TfrmDllForm.CreateParented(Ph);    
  frmDllForm.Show;    
end;

Exports    
Create_Form;

begin    
end.

//---------------------END--------------------------------------

//This is the project

unit Unit1;

interface

uses    
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type    
  TForm1 = class(TForm)    
    Button1: TButton;    
    ScrollBox: TScrollBox;    
    procedure Button1Click(Sender: TObject);    
  private    
  end;

procedure Create_Form(ph: HWND) ; external 'Project1dll.dll' name 'Create_Form';

var    
  Form1: TForm1;

implementation

{$R *.DFM}

function ScrollBoxDll(ph: HWND): Pointer; stdcall;    
begin    
  Create_Form(ph);    
end;

procedure TForm1.Button1Click(Sender: TObject);    
begin    
  ScrollBoxDll(ScrollBox.Handle);    
end;

end.
Kenneth Cochran
  • 11,954
  • 3
  • 52
  • 117
Unit4
  • 85
  • 1
  • 5
0

firstly the idea of and constructing the code was my idea, it was intended to be used as a simple way of showing forms stored in a dll file.

The main idea was to put what ever you wanted in the dll, call and show it in a TscrollBox, this can be actually a fully working database or some other path of a program that you would not really want to pass parameters to and from after it was started or closed.

I posted the question online and many did not really understand what I was trying to explain or wanted to do, they seem to think I wanted to created a from in a dll using a scrollbox, but my form or forms were already created and saved in the dll file, the scroll box was external to the dll in my main project.

All I wanted to do was call the forms and show in the scrolbox as parent to it.

I am not claiming any path of this code because many presented their ideas and I thank them all.

The code presented was already constructed long before I posted the question online but did not work as I intended because the form was only showing outside the scrollbox.

I then posted the entire project online with Board4All when a friend pointed out that I should alter a line of code.

He only has a nickname and said he was not sure if it would work depending on what version of delphi I used.

I then adjusted the line of code and it work so he the one who deserves all credits, I then decided to post the code so others would be able to use the code in their projects.

Unit4
  • 85
  • 1
  • 5