1.DLL子窗口的式样必须设置为无框(BORDERSTYLE:=BSNONE;FORMSTYLE:=FSNORMAL)可以在DLL.DPR中代码控制
2.DLL.DPR代码
{$R *.res}
var
DLLApp: TApplication;
DLLScr: TScreen;
function CreateDLLForm(App: TApplication;Scr:TScreen;Left:Integer=0;Top:Integer=0;Width:Integer=0;Height:Integer=0):TForm;
var
begin
//
end;
procedure ExitDLL(Reason: Integer);
begin
if Reason = DLL_PROCESS_DETACH then
begin
Application := DLLApp;
Screen := DLLScr;
end;
end;
begin
end;
exports
begin
DLLApp := Application;
DLLScr := Screen;
DLLProc := @ExitDLL;
end.
3.Exe.DPR主窗口调用代码
type
InvokeDLLForm = function(App: TApplication;Scr: TScreen): TForm;
var
implementation
{$R *.dfm}
procedure TForm1.Panel1Click(Sender:TObject);
var
DLLHandle: THandle;
DLLSub: InvokeDLLForm;
begin
DLLHandle :=LoadLibrary('SysNewsRead.dll');
if DLLHandle <> 0 then
begin
@DLLSub :=GetProcAddress(DLLHandle, 'CreateDLLForm');
if Assigned(DLLSub) then
begin
if not Assigned(DllForm) then
begin
DLLForm:= DLLSub(Application,Screen,Panel1.Left,Panel1.Top,Panel1.Width,Panel1.Height);
SetWindowPos(DLLForm.Handle, 0, 0, 0, Panel1.Width ,Panel1.Height, SWP_NOZORDER);//可以去掉
DllForm.ParentWindow:=Panel1.Handle;//将容器设置为父窗口
windows.SetParent(DLLForm.Handle,Panel1.Handle);
DllForm.Show;//这里显示出窗口
end;
end;
end;
end;
var
begin
end;
procedure TForm1.ApplicationEvents1Messag
e(varMsg: tagMSG;var Handled:Boolean);
begin
if Assigned(DllForm) then
ifIsDialogMessage(DLLForm.Handle,Msg)thenHandled:=True
;//通过该代码转发DLL窗口的消息。
end;
begin
end;
procedure TForm1.Panel1Enter(Sender:TObject);
begin
//将DLL窗口设置为前景窗口
SetForegroundWindow(DllForm.Handle);
Windows.SetFocus(DllForm.Handle);
begin
//将DLL窗口设置为前景窗口
end;
procedure TForm1.Panel1Resize(Sender:TObject);
begin
if Assigned(DllForm) then
SetWindowPos(DLLForm.Handle, 0, 0,0, Panel1.Width , Panel1.Height, SWP_NOZORDER);
end;
begin