再次求原因。为什么调用DLL然后关闭, 主程序会自动置后。
DLL点关闭的时候,主程序就自动置后了,不知为什么。代码如下,
这个是普通窗体调用DLL。
procedure TForm1.Button1Click(Sender: TObject);
type
TGetForm = function (H: THandle) : Integer; cdecl;
var
DllForm : TGetForm;
DllHandle : THandle;
nn : integer;
begin
DllHandle := LoadLibrary(PChar('DebtMoney.dll'));
try
if DllHandle <> 0 then
begin
DllForm := GetProcAddress(DllHandle, 'EDebtMoney');
nn := DllForm(Application.Handle) ;
form1.Caption := inttostr(nn);
end;
finally
FreeLibrary(DllHandle);
end;
end;
这个是无边框窗体的DLL:
//DLL入口
Function EDebtMoney(H: THandle):integer;
begin
Application.Handle := H; //当我把这句去掉的时候,窗体就不会自动置后了,
with TForm1.Create(Application) do try
KeyPreview :=True;
ShowModal;
Result := MySelect ;
finally
Free; { 调用结束时销毁窗口 }
end;
end;
Application.Handle := H; //当我把这句去掉的时候,窗体就不会自动置后了,
也就是说,不传递Handle 就不会出现这问题。但是我需要传递Handle ,请有时间的老师看下,到底是什么原因,有没办法解决 谢谢
[解决办法]
//试试把句柄赋回来
procedure TForm1.Button1Click(Sender: TObject);
type
TGetForm = function (H: THandle) : Integer; cdecl;
var
DllForm : TGetForm;
DllHandle, exeHandle : THandle;
nn : integer;
begin
DllHandle := LoadLibrary(PChar('DebtMoney.dll'));
exeHandle := Application.Handle;
try
if DllHandle <> 0 then
begin
DllForm := GetProcAddress(DllHandle, 'EDebtMoney');
nn := DllForm(exeHandle) ;
form1.Caption := inttostr(nn);
end;
finally
FreeLibrary(DllHandle);
Application.Handle := exeHandle;
end;
end;