zoukankan      html  css  js  c++  java
  • 调用Dll里面的窗体

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



    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.




  • 相关阅读:
    (转)如何在一台电脑上开启多个tomcat 和配置让系统识别哪个具体的tomcat
    Moccakids-Tangram Puzzle 限免啦!
    iOS:OC Lib:MagicalRecord
    iOS Vuforia:TextReco 增加自己的单词库
    iOS:Tools:快速注释Doxygen
    聊聊分布式事务,再说说解决方案
    .NET Core 事件总线,分布式事务解决方案:CAP
    Glob 模式
    基于 Kong 和 Kubernetes 的 WebApi 多版本解决方案
    ASP.NET Core 身份验证(一)
  • 原文地址:https://www.cnblogs.com/xe2011/p/3876117.html
Copyright © 2011-2022 走看看