谢谢DarwinZhang大哥,我按照你的思路做了,但是又遇到新的问题啦,就是DLL中的窗口关闭了,<br>可主程序也跟着关闭了,你帮我看看,到底是怎么回事,我把代码贴出来。<br>//主程序代码<br>unit MainForm;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls, AppEvnts;<br><br>type<br> TShowForm = function(AHandle: THandle; UniqueMsgID: Cardinal): THandle; stdcall;<br> TCloseForm = procedure(FormHandle: THandle); stdcall;<br><br> TfrmMain = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> procedure FormCreate(Sender: TObject);<br> private<br> { Private declarations }<br> UniqueMsgID: Cardinal;<br> DLLHandle, FormHandle: THandle; //DLL的地址,要显示的Form的句柄<br> ShowForm: TShowForm;<br> CloseForm: TCloseForm;<br> public<br> { Public declarations }<br> procedure WndProc(var Msg: TMessage); override;<br> end;<br><br>var<br> frmMain: TfrmMain;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TfrmMain.Button1Click(Sender: TObject);<br>begin<br> { 动态调用的过程 }<br> if DLLHandle = 0 then<br> begin<br> DLLHandle := LoadLibrary('TestDLL.dll');<br> try<br> if DLLHandle = 0 then<br> raise Exception.Create('装入动态链接库DLLFile.dll失败!');<br> { 在这里,将本次要用到的例程全部引入。对于某些必须成功装入的例程来说,如果<br> 装入失败,则立即释放DLL }<br> @ShowForm := GetProcAddress(DLLHandle, 'ShowForm');<br> if @ShowForm = nil then<br> Abort;<br> @CloseForm := GetProcAddress(DLLHandle, 'CloseForm');<br> if @CloseForm = nil then<br> Abort;<br> FormHandle := ShowForm(Application.Handle, UniqueMsgID);<br> except<br> FreeLibrary(DLLHandle);<br> DLLHandle := 0;<br> end;<br> end;<br>end;<br><br>procedure TfrmMain.Button2Click(Sender: TObject);<br>begin<br> if DLLHandle <> 0 then<br> begin<br> CloseForm(FormHandle);<br> FreeLibrary(DLLHandle);<br> DLLHandle := 0;<br> end;<br>end;<br><br>procedure TfrmMain.FormCreate(Sender: TObject);<br>begin<br> DLLHandle := 0;<br> UniqueMsgID := RegisterWindowMessage('shiwei is a girl 10:50:20');<br>end;<br><br>procedure TfrmMain.WndProc(var Msg: TMessage);<br>begin<br> if Msg.Msg = UniqueMsgID then<br> if DLLHandle <> 0 then<br> begin<br> CloseForm(FormHandle);<br> FreeLibrary(DLLHandle);<br> DLLHandle := 0;<br> end;<br> inherited WndProc(Msg);<br>end;<br><br>end.<br>//-----------------------------------------------------------------------------<br>//DLL单元代码<br>unit NonModalForm;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;<br><br>type<br> TfrmNonModal = class(TForm)<br> private<br> { Private declarations }<br> procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;<br> public<br> { Public declarations }<br> end;<br><br>function ShowForm(AHandle: THandle; UniqueMsgID: Cardinal): THandle; stdcall;<br>procedure CloseForm(FormHandle: THandle); stdcall;<br><br>exports<br> ShowForm, CloseForm;<br><br>var<br>// frmNonModal: TfrmNonModal;<br> UniMsgID: Cardinal;<br>//var<br> FormHandle: THandle;<br><br>implementation<br><br>{$R *.DFM}<br><br>function ShowForm(AHandle: THandle; UniqueMsgID: Cardinal): THandle;<br>begin<br> Application.Handle := AHandle;<br> UniMsgID := UniqueMsgID;<br> FormHandle := THandle(TfrmNonModal.Create(Application));<br> Result := FormHandle; //将新建窗体的句柄返回给主程序<br> with TfrmNonModal(FormHandle) do<br> try<br> Show;<br> except<br> Free;<br> Result := 0;<br> end;<br>end;<br><br>procedure CloseForm(FormHandle: THandle);<br>begin<br> if FormHandle <> 0 then //FormHandle <> 0,表示已经建立的实例,则可以执行关闭、释放<br> begin<br> with TfrmNonModal(FormHandle) do<br> begin<br> Close;<br> Free;<br> end;<br> end;<br>end;<br><br>{ TfrmNonModal }<br><br>procedure TfrmNonModal.WMSysCommand(var Msg: TWMSysCommand);<br>begin<br> if Msg.CmdType = SC_CLOSE then<br> SendMessage(HWND_BROADCAST, UniMsgID, 0, 0)<br> else<br> inherited;<br>end;<br><br>end.