问题
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,
回答1:
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.
回答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.
回答3:
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.
来源:https://stackoverflow.com/questions/16912393/show-form-from-dll-in-tscrollbox