^_^
Dll中的窗体,显示在调用窗体的某个区域? 如调用Dll中的Form1,并放置panle1中.//可以通过修改以下代码来说明//==================================================================DLLForm.dpr
library DLLForm;{ Important note about DLL memory management: ShareMem must be the
 first unit in your library's USES clause AND your project's (select
 Project-View Source) USES clause if your DLL exports any procedures or
 functions that pass strings as parameters or function results. This
 applies to all strings passed to and from your DLL--even those that
 are nested in records and classes. ShareMem is the interface unit to
 the BORLNDMM.DLL shared memory manager, which must be deployed along
 with your DLL. To avoid using BORLNDMM.DLL, pass string information
 using PChar or ShortString parameters. }uses
  Windows,
  SysUtils,
  Classes,
  Forms,
  dialogs,
  Messages,
  Help in '..\新建文件夹\Help.pas' {FrmHelp};var
 DLLAPP: TApplication;
 DLLScreen: TScreen;
 TheForm: TForm;
 TheClass: TPersistentClass;{$R *.res}procedure RunDLL(DLLName, FormName, FormCaption: String; APP: TApplication; SC: TScreen) stdcall;
begin
 Application := App;
 Screen := SC;
 RegisterClasses([TFrmHelp]);
 TheClass := GetClass(PChar('T' + FormName)); if (TheClass <> nil) and TheClass.InheritsFrom(TForm) then
   begin
     TheForm := TForm(TheClass.Create).Create(nil);
     TheForm.Show;
   end;
end;procedure DLLUnLoadProc(Reason: Integer);
begin
 if Reason = DLL_PROCESS_DETACH then
   begin
     Application := DLLApp;
     Screen := DLLScreen;
     SendMessage(Application.Handle, WM_CLOSE, 0, 0);
     FreeLibrary(Application.Handle);
   end;
end;exports
RunDLL;begin
 DLLAPP := Application;
 DLLScreen := Screen;
 DLLProc := @DLLUnLoadProc;
end.
//==================================================================Help.pas
unit Help;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;type
  TFrmHelp = class(TForm)
    Button1: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  FrmHelp: TFrmHelp;implementation{$R *.dfm}procedure TFrmHelp.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  action := caFree
end;end.
//==================================================================调用窗体 Unit1.pas
unit Unit1;interfaceuses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, Menus, ExtCtrls;type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    dll1: TMenuItem;
    dll2: TMenuItem;
    Panel1: TPanel;
    procedure dll2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
Type
  TRunDLL = procedure(DLLName, FormName,FormCaption: String; Application: TApplication; Screen: TScreen); stdcall;procedure RunDLLForm(DLLName, FormName,FormCaption: String; App: TApplication; SC: TScreen); stdcall;var
  GetDLLHWND: HWND;
  Form1: TForm1;implementation{$R *.dfm}procedure RunDLLForm(DLLName, FormName,FormCaption: String; App: TApplication; SC: TScreen); stdcall;
var
 RunDLL: TRunDLL;
begin
 GetDLLHWND := LoadLibrary(PChar(DLLName));
 if GetDLLHWND < 32 then
   begin
     MessageBox(0, PChar('未找到DLL'), PChar('加载DLL失败'), MB_OK);
     Exit;
   end;
 @RunDLL := GetProcAddress(GetDLLHWND, PChar('RunDLL'));
 if @RunDLL <> nil then
   begin
     Try
       RunDLL(PChar(UpperCase(Trim(DLLName))), PChar(UpperCase(Trim(FormName))),PChar(FormCaption), APP, SC);
     Except
       Raise Exception.Create('T' + FormName + '不存在');
     End;
   end;
end;procedure TForm1.DLL2Click(Sender: TObject);
begin
 RunDLLForm('DLLForm', 'FrmHelp', 'FrmHelp', Application, Screen);
end;end.

解决方案 »

  1.   

    {   Important   note   about   DLL   memory   management:   ShareMem   must   be   the 
      first   unit   in   your   library's   USES   clause   AND   your   project's   (select 
      Project-View   Source)   USES   clause   if   your   DLL   exports   any   procedures   or 
      functions   that   pass   strings   as   parameters   or   function   results.   This 
      applies   to   all   strings   passed   to   and   from   your   DLL--even   those   that 
      are   nested   in   records   and   classes.   ShareMem   is   the   interface   unit   to 
      the   BORLNDMM.DLL   shared   memory   manager,   which   must   be   deployed   along 
      with   your   DLL.   To   avoid   using   BORLNDMM.DLL,   pass   string   information 
      using   PChar   or   ShortString   parameters.   } 你需要
    uses ShareMem;
    在dll中最好不要使用string类型,最好使用pchar
      

  2.   

    uses 
      ShareMem,//放到最上边
      Windows, 
      SysUtils, 
      Classes, 
      Forms, 
      dialogs, 
      Messages, 
      Help   in   '..\新建文件夹\Help.pas'   {FrmHelp}; 
      

  3.   

    To hongqi162 
    能说的详细一些吗
    + ShareMem之后,其他内容如何调整才能得到我想要的结果?
      

  4.   

    这样应该可以。library DllForm;{ Important note about DLL memory management: ShareMem must be the
      first unit in your library's USES clause AND your project's (select
      Project-View Source) USES clause if your DLL exports any procedures or
      functions that pass strings as parameters or function results. This
      applies to all strings passed to and from your DLL--even those that
      are nested in records and classes. ShareMem is the interface unit to
      the BORLNDMM.DLL shared memory manager, which must be deployed along
      with your DLL. To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. }uses
      FSwifte in 'FSwifte.pas',
      SysUtils,
      Classes,
      Forms,
      FHelp in 'FHelp.pas' {frmHelp};{$R *.res}function GetDllForm(AApplication: TApplication; AFormClass: PChar;
      AFormName: PChar):  THandle; stdcall;
    var
      oForm: TForm;
    begin
      Result := 0;
      Application := AApplication;
      oForm := TForm(TComponentClass(FindClass(AFormClass)).Create(Application.MainForm));
      if oForm <> nil then
      begin
        oForm.Name := AFormName;
        Result := Integer(oForm);
      end;
    end;exports
      GetDllForm;begin
    end.unit FHelp;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls;type
      TfrmHelp = class(TForm)
        pnlMain: TPanel;
        Button1: TButton;
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      frmHelp: TfrmHelp;implementation{$R *.dfm}initialization
      RegisterClass(TfrmHelp);finalization
      UnregisterClass(TfrmHelp);end.unit FSwifte;interfaceimplementationinitialization
      IsLibrary := False;end.
    program ExeForm;uses
      Forms,
      FMain in 'FMain.pas' {Form1};{$R *.res}begin
      Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.unit FMain;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, StdCtrls;type
      TCallDllForm = function (AApplication: TApplication; AFormClass: PChar;
        AFormName: PChar): THandle; stdcall;type
      TForm1 = class(TForm)
        pnlMain: TPanel;
        Load: TButton;
        procedure LoadClick(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      FDllHandle: THandle;implementation{$R *.dfm}procedure TForm1.LoadClick(Sender: TObject);
    var
      oCall: TCallDllForm;
      hFormHandle: THandle;
      oForm: TForm;
      oPanel: TPanel;
    begin
      FDllHandle := LoadLibrary('DllForm.dll');
      if FDllHandle < 32 then
      begin
        MessageBox(0, PChar('未找到DLL'),   PChar('加载DLL失败'),   MB_OK);
        Exit;
      end;
      @oCall := GetProcAddress(FDllHandle, PChar('GetDllForm'));
      if @oCall <> nil then
      begin
        hFormHandle := oCall(Application, 'TfrmHelp', 'frmHelp');
        if hFormHandle > 0 then
        begin
          oForm := TForm(hFormHandle);
          if oForm <> nil then
          begin
            oForm.Align := alClient;
            oForm.BorderStyle := bsNone;
            Windows.SetParent(oForm.Handle, pnlMain.Handle);
            oForm.Visible := True;
            SetForegroundWindow(Handle);
          end;
        end;
      end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      FDllHandle := 0;
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
      if FDllHandle > 0 then
        FreeLibrary(FDllHandle);
    end;end.
      

  5.   

    上面那些代码还有个问题:当嵌入的窗体得到焦点时,父窗体将失去焦点,你自己解决吧。要是你的项目足够大的话,建议还是使用包,可以看下这篇文件。
    http://blog.csdn.net/mr_liyouliang/
      

  6.   

    library DLLForm;{ Important note about DLL memory management: ShareMem must be the
     first unit in your library's USES clause AND your project's (select
     Project-View Source) USES clause if your DLL exports any procedures or
     functions that pass strings as parameters or function results. This
     applies to all strings passed to and from your DLL--even those that
     are nested in records and classes. ShareMem is the interface unit to
     the BORLNDMM.DLL shared memory manager, which must be deployed along
     with your DLL. To avoid using BORLNDMM.DLL, pass string information
     using PChar or ShortString parameters. }uses
      ShareMem,
      Windows,
      SysUtils,
      Classes,
      Forms,
      dialogs,
      Messages,
      Controls,
      Help in '..\新建文件夹\Help.pas' {FrmHelp};var
     DLLAPP: TApplication;
     DLLScreen: TScreen;
     TheForm: TForm;
     TheClass: TPersistentClass;
     iApp: TApplication;
    {$R *.res}
    procedure DLLUnLoadProc(Reason: Integer);
    begin
     if Reason = DLL_PROCESS_DETACH then
       begin
         Application := DLLApp;
         Screen := DLLScreen;
         SendMessage(Application.Handle, WM_CLOSE, 0, 0);
         FreeLibrary(Application.Handle);
       end;
    end;procedure RunDLL(DLLName, FormName, FormCaption: String; APP: TApplication; SC: TScreen; Parent: TCustomControl) stdcall;
    begin
     Application := App;
     Screen := SC;
     RegisterClasses([TFrmHelp]);
     TheClass := GetClass(PChar('T' + FormName));
     iApp := APP;
     if (TheClass <> nil) and TheClass.InheritsFrom(TForm) then
       begin
         if not Assigned(TheForm) then
         begin
           TheForm := TForm(TheClass.Create).Create(nil);
           windows.SetParent(TheForm.Handle,parent.Handle); //!!!!
           TheForm.WindowState := wsMaximized;
         end;
         TheForm.Show;
       end;
    end;exports
    RunDLL,DLLUnLoadProc;begin
     DLLAPP := Application;
     DLLScreen := Screen;
     DLLProc := @DLLUnLoadProc;
    end.