【发布时间】:2013-11-10 21:20:40
【问题描述】:
我需要向应用程序添加一些插件功能,以及动态加载和打开插件的能力。
在我的应用程序(主要形式)中,我有以下代码:
procedure TfrmMain.PluginClick(Sender: TObject);
Var
DllFileName : String;
DllHandle : THandle;
VitoRunPlugin : procedure (AppHandle, FormHandle : HWND);
begin
DllFileName := (Sender AS TComponent).Name + '.dll';
DllHandle := LoadLibrary(PWideChar (DllFileName));
if DllHandle <> 0 then
Begin
@VitoRunPlugin := GetProcAddress (DllHandle, 'VitoRunPlugin');
VitoRunPlugin (Application.Handle, Self.Handle);
End Else Begin
ShowMessage ('Plugin load error');
End;
FreeLibrary (DllHandle);
end;
我的插件库是(现在只用于测试):
library plugintest;
uses
System.SysUtils, WinApi.Windows,
Vcl.Forms,
System.Classes,
Vcl.StdCtrls;
{$R *.res}
Procedure VitoRunPlugin (AppHandle, FormHandle : HWND);
Var F : TForm; B: TButton;
Begin
F := TForm.CreateParented(FormHandle);
F.FormStyle := fsNormal;
B := TButton.Create(F);
B.Left := 5; B.Top := 5; B.Height := 50; B.Width := 50;
B.Caption := 'Touch me!';
B.Parent := F;
F.ShowModal;
F.Free;
End;
exports VitoRunPlugin;
begin
end.
表单打开正常,但没有任何效果:我既不能按下按钮,也不能关闭表单。我只能通过 Alt+F4 关闭它。
怎么了?
【问题讨论】:
-
该按钮没有 OnClick 事件处理程序,这可能是它看起来没有响应的原因。
-
你在DLL中试过
Application.Handle := AppHandle;吗? -
我试过了,但没用。
-
您确实应该将按钮的
ModalResult设置为不同于mrNone(默认值)的值。我会将AppHandle传递给CreateParented,而不是FormHandle(或者在设置Application.Handle后直接调用Create(Application))。 -
设置合适的FormStyle或重新启用windows链qc.embarcadero.com/wc/qcmain.aspx?d=108580
标签: delphi dll delphi-xe2