dll中怎么加上form,外部程序怎样调用阿

解决方案 »

  1.   

    直接在DLL中NEW一个FORM
    然后在你的接口中写下如下代码就可以了。很简单。
    procedure MyFun;
    var
      frm: TForm1;
    begin
      frm := TForm1.Create(nil);
      try
        frm.ShowModal;
      finally
        frm.Free;
      end;
    end;
      

  2.   

    ////////////////////////////////////////////////////
    library CalendarLib;uses
      ShareMem,
      SysUtils,
      Classes,
      DLLFrm in 'DLLFrm.pas' {DLLForm};exports
      ShowCalendar;
      
    begin
    end.///////////////////////////////////////////////////////
    unit DLLFrm;interfaceuses
      SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
      Forms, Dialogs, Grids, Calendar;type  TDLLForm = class(TForm)
        calDllCalendar: TCalendar;
        procedure calDllCalendarDblClick(Sender: TObject);
      end;{ Declare the export function }
    function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime; StdCall;implementation
    {$R *.DFM}function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime;
    var
      DLLForm: TDllForm;
    begin
      // Copy application handle to DLL's TApplication object
      Application.Handle := AHandle;
      DLLForm := TDLLForm.Create(Application); 
      try
        DLLForm.Caption := ACaption;
        DLLForm.ShowModal;
        Result := DLLForm.calDLLCalendar.CalendarDate; // Pass the date back in Result
      finally
        DLLForm.Free;
      end;
    end;procedure TDLLForm.calDllCalendarDblClick(Sender: TObject);
    begin
      Close;
    end;end.
    ///////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////
    program CalendarTest;uses
      Forms,
      MainFfm in 'MainFfm.pas' {MainForm};{$R *.RES}begin
      Application.CreateForm(TMainForm, MainForm);
      Application.Run;
    end.
    /////////////////////////////////////////////////////////////
    unit MainFfm;interfaceuses
      SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
      Forms, Dialogs, StdCtrls;type
      { First, define a procedural data type, this should reflect the
        procedure that is exported from the DLL. }
      TShowCalendar = function (AHandle: THandle; ACaption: String): TDateTime; StdCall;  { Create a new exception class to reflect a failed DLL load }
      EDLLLoadError = class(Exception);  TMainForm = class(TForm)
        lblDate: TLabel;
        btnGetCalendar: TButton;
        procedure btnGetCalendarClick(Sender: TObject);
      end;var
      MainForm: TMainForm;implementation{$R *.DFM}procedure TMainForm.btnGetCalendarClick(Sender: TObject);
    var
      LibHandle   : THandle;
      ShowCalendar: TShowCalendar;
    begin  { Attempt to load the DLL }
      LibHandle := LoadLibrary('CALENDARLIB.DLL');
      try
        { If the load failed, LibHandle will be zero.
          If this occurs, raise an exception. }
        if LibHandle = 0 then
          raise EDLLLoadError.Create('Unable to Load DLL');
        { If the code makes it here, the DLL loaded successfully, now obtain
          the link to the DLL's exported function so that it can be called. }
        @ShowCalendar := GetProcAddress(LibHandle, 'ShowCalendar');
        { If the function is imported successfully, then set lblDate.Caption to reflect
          the returned date from the function. Otherwise, show the return raise
          an exception. }
        if not (@ShowCalendar = nil) then
          lblDate.Caption := DateToStr(ShowCalendar(Application.Handle, Caption))
        else
          RaiseLastWin32Error;                                                               
      finally
        FreeLibrary(LibHandle); // Unload the DLL.
      end;
    end;end.
      

  3.   

    给你看一段《DELPHI 5 开发人员指南》中的代码:
    把你的EMAIL给我,我发给你吧!
    或者 你上网上去当这本书的源代码, 在code\ch09\modaldll中就是!
    下载地址:http://202.96.70.229/cakk/delphi/