Delphi應用程序架構中有一種模式,采用DLL或Package存儲業務窗體,當需要時從其中載入到主程序中,這時候需要對DLL或Package中窗體進行處理,步驟如下:
1、定義窗體基類
type TfrmBase = class(TForm) Panel1: TPanel; private { Private declarations } protected procedure Loaded;override; public { Public declarations } procedure UpdateActions; override; end; TfrmBaseClass = class of TfrmBase; implementation {$R *.dfm} { TfrmBase } procedure TfrmBase.Loaded; begin inherited;
//將窗體改變為嵌入化 align := alClient; BorderStyle := bsNone; BorderIcons := []; end; procedure TfrmBase.UpdateActions; begin inherited; //將方法Public化 end;
2、創建具體的實例窗體
TfrmDll = class(TfrmBase)
3、通過窗體數據窗體類
function GetDllFormClass : TfrmBaseClass; begin Result := TfrmDll; end; exports GetDllFormClass;
4、主窗體中動態載入DLL和創建DLL中的窗體對象
procedure TfrmMain.Button1Click(Sender: TObject); begin if DllHandle > 0 then Exit; DllHandle := LoadLibrary('FormDll.dll'); if DllHandle > 0 then begin GetDllFormClass := GetProcAddress(DllHandle, 'GetDllFormClass'); ifAssigned(GetDllFormClass) then begin
cfrm := GetDllformClass; vfrm := cfrm.Create(Application);
//設置窗體的Parent,通過ParentWindow方法實現,這點很重要
vfrm.ParentWindow := TabSheet1.Handle;
end; end; end;
5、在主窗體上放置一個TApplicationEvents控件,在OnIdle事件中填寫以下代碼:
procedure TfrmMain.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean); begin //當系統空閑時,處理子窗體的ActionUpdate事件 if Assigned(vfrm) then vfrm.UpdateActions; end;
6、在OnMessage事件中填寫以下代碼:
procedure TfrmMain.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); begin //如果窗體變量存在,則需要對消息進行判斷和處理,如果是窗體變量的消息,則不進一步處理 if Assigned(vfrm) then begin if IsDialogMessage(vfrm.Handle, Msg) then Handled := true; end; end;
至此完成。