码迷,mamicode.com
首页 > Windows程序 > 详细

调用Dll里面的窗体

时间:2014-07-29 21:08:42      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   os   strong   文件   

将窗体资源分装到DLL中并且调用 
用Delphi生成DLL并封装窗体的示例
 调用Dll里面的窗体
bubuko.com,布布扣


DLL文件

library Project2;

{ 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
  SysUtils,
  Classes,
  Unit2 in ‘Unit2.pas‘ {Form1};

{$R *.res}
exports
    Dll_Showform,
    Dll_CreateForm,
    DLL_GetValue,
    DLL_SetTitle;


begin
end.


unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    BitBtn1: TBitBtn;
  private
    { Private declarations }
  public
    { Public declarations }


  end;

var
    Form1: TForm1;
    //Ready for DLl Export list
    procedure Dll_Showform();stdcall;
    procedure Dll_Createform(Left,Top,Width,Height:Integer);stdcall;
    function DLL_GetValue():string;stdcall;
    procedure  DLL_SetTitle(text:string); stdcall;
implementation

{$R *.dfm}

//无参数
procedure Dll_Showform();stdcall;
var frm:TForm1;
begin
   frm:=TForm1.Create(Application);
   frm.Position:=poMainFormCenter;
   frm.ShowModal;
end;

//带参数
procedure Dll_Createform(Left,Top,Width,Height:Integer);stdcall;
var frm:TForm1;
begin
   frm:=TForm1.Create(Application);
   frm.Left:=Left;
   frm.Top:=Top;
   frm.Width:=Width;
   frm.Height:=Height;
   frm.BorderStyle:=bsNone;

   frm.ShowModal;
end;

//获得返回值
function DLL_GetValue():string;stdcall;
var
  frm:TForm1;
begin
   Result:=‘‘;
   frm:=TForm1.Create(Application);
   frm.Position:=poMainFormCenter;
   if frm.ShowModal = mrok then
      Result:=frm.Memo1.Lines.Text;
end;


//回调
procedure  DLL_SetTitle(text:string); stdcall;
var frm:TForm1;
begin
   frm:=TForm1.Create(Application);
   frm.Position:=poMainFormCenter;
   frm.Caption:=text;
   frm.ShowModal;
end;


end.

 

 

调用Dll 

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
//-------------------------------------------------------------------------------
procedure Dll_Showform();stdcall;external ‘project2.dll‘; //1无参数 直接调用
procedure Dll_Createform(Left,Top,Width,Height:Integer);stdcall;external ‘project2.dll‘;//2带参数了
function DLL_GetValue():string;stdcall;external ‘project2.dll‘;//3获得返回值
//-------------------------------------------------------------------------------

type
  TfrmMain1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Memo1: TMemo;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain1: TfrmMain1;

implementation

{$R *.dfm}
//------------------------------------------------------------------------------
procedure TfrmMain1.Button1Click(Sender: TObject);
begin
    Dll_Showform;
end;
//------------------------------------------------------------------------------
procedure TfrmMain1.Button2Click(Sender: TObject);
var
     x,y:Integer;
begin
    x:=frmMain1.left+Button2.left+8; //Left
    y:=frmMain1.Top+Button2.Top +Button2.Height+28;      //Top
    Dll_Createform(x,y,400,400);
    //  Dll_Createform(0,0,100,200);
end;
//------------------------------------------------------------------------------
procedure TfrmMain1.Button3Click(Sender: TObject);
begin
     Memo1.Text:=DLL_GetValue;
end;

//------------------------------------------------------------------------------
type
    TSetCaption = procedure(text:string); stdcall;//4回调
procedure TfrmMain1.Button4Click(Sender: TObject);
var
    pSetTitle: TSetCaption;
    h:THandle;
begin
    h:=LoadLibrary(PChar(‘project2.dll‘));
    pSetTitle := TSetCaption( GetProcAddress(h, PChar(‘DLL_SetTitle‘)) );
    if h = 0 then Exit;
    if Assigned(pSetTitle) then
        pSetTitle(‘Hello World!‘);
    FreeLibrary(h);
end;


end.
bubuko.com,布布扣




调用Dll里面的窗体,布布扣,bubuko.com

调用Dll里面的窗体

标签:style   blog   http   java   color   os   strong   文件   

原文地址:http://www.cnblogs.com/xe2011/p/3876117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!