2011. január 20., csütörtök

How to load a frame from a DLL


Problem/Question/Abstract:

I want load a TFrame from a DLL in the main program. I always get the error "No parent window", although I already have the main form's handle.

Answer:

library DLLFrame;

uses
  SysUtils, Classes, Controls, Forms, Windows,
  DllFrameFrame in 'DllFrameFrame.pas' {Frame1: TFrame};

procedure AddFrame(ApplicationHandle, ParentHandle: THandle); stdcall;
var
  Frame1: TFrame1;
  AppHandle: THandle;
begin
  AppHandle := Application.Handle;
  Application.Handle := ApplicationHandle;
  Frame1 := TFrame1.Create(Application);
  Frame1.ParentWindow := ParentHandle;
  SetParent(Frame1.Handle, ParentHandle);
  Frame1.Align := alClient;
  Frame1.Visible := True;
  Application.Handle := AppHandle;
end;

exports
  AddFrame;

begin
end.

unit DllFrameFrame;

interface

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

type
  TFrame1 = class(TFrame)
    Label1: TLabel;
    Label2: TLabel;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

implementation

{$R *.dfm}

end.

program LoadDLLFrame;

uses
  Forms, LoadDLLFrameMn in 'LoadDLLFrameMn.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

unit LoadDLLFrameMn;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

procedure AddFrame(ApplicationHandle, ParentHandle: THandle); stdcall;

implementation

{$R *.dfm}

procedure AddFrame(ApplicationHandle, ParentHandle: THandle); stdcall;
  external 'DLLFrame.dll';

procedure TForm1.Button1Click(Sender: TObject);
begin
  AddFrame(Application.Handle, Panel1.Handle);
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése