Thema: Delphi DLL mit Form

Einzelnen Beitrag anzeigen

Capa

Registriert seit: 26. Okt 2003
102 Beiträge
 
RAD-Studio 2010 Arc
 
#2

AW: DLL mit Form

  Alt 27. Feb 2011, 16:55
Nun hab ich doch noch ein Problembei untenstehendem Code
wenn ich die Form1 beende beendet sich das Programm nicht
wirklich ich muss es dann per TaskManager Killen :/



Code der Exe-File:
Delphi-Quellcode:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

procedure CreateForm2(appHandle: THandle); stdcall; external 'DLL1.dllname 'CreateForm2';
procedure ShowForm2; stdcall; external 'DLL1.dllname 'ShowForm2';
procedure HideForm2; stdcall; external 'DLL1.dllname 'HideForm2';
procedure FreeForm2; stdcall; external 'DLL1.dllname 'FreeForm2';
procedure Test(text1,text2,text3: string); stdcall; external 'DLL1.dllname 'Test';


{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowForm2;
  Test('Need Help','Capa','DLL Form');
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := False;
  try
    FreeForm2;
  finally
    CanClose := True;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CreateForm2(Application.Handle);
end;

end.
Code der DLL:
Delphi-Quellcode:
library DLL1;

uses
  SysUtils,
  Classes,
  Forms,
  Sharemem,
  windows,
  DLLUnit1 in 'DLLUnit1.pas{Form2};

{$R *.res}

procedure ShowForm2; stdcall;
begin
  Form2.Show();
end;

procedure HideForm2; stdcall;
begin
  Form2.Hide();
end;

procedure CreateForm2(appHandle: THandle); stdcall;
begin
  if appHandle = 0 then apphandle := GetActiveWindow;
  Application.Handle := appHandle;
  try
    if Form2=nil then
      Form2 := TForm2.Create(Application);
  except
    On E: Exception Do Application.HandleException(E);
  end;
  Application.Handle := 0;
end;

procedure FreeForm2; stdcall;
begin
  FreeAndNil(Form2);
end;

procedure Test(text1,text2,text3: string); stdcall;
begin
  Form2.Listbox1.Items.Add(text1);
  Form2.Listbox1.Items.Add(text2);
  Form2.Listbox1.Items.Add(text3);
end;

exports CreateForm2,
          ShowForm2,
          HideForm2,
          FreeForm2,
               Test;

begin
end.
Code der DLL Form:
Delphi-Quellcode:
unit DLLUnit1;

interface

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

type
  TForm2 = class(TForm)
    ListBox1: TListBox;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caHide;
end;

end.

Geändert von Capa (27. Feb 2011 um 16:58 Uhr)
  Mit Zitat antworten Zitat