千家信息网

Delphi中动态调用DLL中的窗体

发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,DLL链接库代码Library Project1;usesForms,Unit1 in 'Unit1.pas' {Form1};Function ShowForm(AHandle:THandle):B
千家信息网最后更新 2025年02月02日Delphi中动态调用DLL中的窗体

DLL链接库代码

Library Project1;usesForms,Unit1 in 'Unit1.pas' {Form1};Function ShowForm(AHandle:THandle):Boolean;StdCall;varAForm:TForm1;beginResult:=False;Application.Handle:=AHandle;AForm:=TForm1.Create(Application);TryAForm.ShowModal;Result:=True;FinallyAForm.Free;end;end;{$R *.res}exportsShowForm;beginend.

Form中调用代码

unit Unit2;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)Button1: TButton;procedure Button1Click(Sender: TObject);procedure FormCreate(Sender: TObject);private{ Private declarations }public{ Public declarations }end;TShowForm=Function (AHandle:THandle):Boolean;Stdcall;   //001varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);var mainfrm,DllForm:THandle;                            //002ShowForm:TShowForm;                            //003beginmainfrm:=Form1.Handle;                           //004DllForm:=LoadLibrary('hello.dll');              //005Trybeginif DllForm<>0 thenbegin@ShowForm:=GetProcAddress(DllForm,'ShowForm');ShowForm(mainfrm);endelsebeginRaiseLastWin32Error;end;end;FinallyFreeLibrary(DllForm);end;end;procedure TForm1.FormCreate(Sender: TObject);beginend;end.
0