Folgender Code liefert den Fehler bei Zeile:
MyElement.Parent := Form1;
... eine
Exception der Klasse EConverError aufgetreten. Meldung; 'TFont kann nicht zu TFont zugewisen werden'.....
Wenn ich die Zeile weglasse, läuft der Code jedoch ohne das Objekt anzuzeigen.
Mein Eindruck:
Delphi geht nicht davon aus, dass beide OberKlasse dieselben sind, da an zwei Orten (
DLL und Programm) deklariert und implementiert. Die Oberklassen könnten, müssen aber nicht, gleich sein.
Delphi-Quellcode:
unit programm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
TYPE
TOberKlasse =
CLASS(TCustomControl)
PUBLIC
CONSTRUCTOR Create(AOwner: TComponent);
OVERRIDE;
DESTRUCTOR Destroy;
OVERRIDE;
END;
FUNCTION GetElement(MyForm : TForm):TOberKlasse;
EXTERNAL '
Project2.dll';
type
TForm1 =
class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
MyElement : TOberKlasse;
implementation
{$R *.dfm}
////////////////////////////////////////////////////////////////////////////////
// Konstruktor
CONSTRUCTOR TOberKlasse.Create(AOwner: TComponent);
BEGIN
INHERITED Create(AOwner);
Visible := True;
END;
////////////////////////////////////////////////////////////////////////////////
// Destruktor
DESTRUCTOR TOberKlasse.Destroy();
BEGIN
INHERITED Destroy;
END;
procedure TForm1.Button1Click(Sender: TObject);
begin
MyElement := GetElement(Form1);
MyElement.Parent := Form1;
MyElement.Top := 26;
MyElement.Left := 10;
MyElement.Height := 25;
MyElement.Width := 25;
end;
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
DLL
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Delphi-Quellcode:
LIBRARY Project2;
uses
SysUtils, Classes, Forms, Controls;
{$R *.RES}
TYPE
TOberKlasse = CLASS(TCustomControl)
PUBLIC
CONSTRUCTOR Create(AOwner: TComponent); OVERRIDE;
DESTRUCTOR Destroy; OVERRIDE;
END;
TYPE
TUnterKlasse = CLASS(TOberKlasse)
PUBLIC
CONSTRUCTOR Create(AOwner: TComponent); OVERRIDE;
DESTRUCTOR Destroy; OVERRIDE;
END;
////////////////////////////////////////////////////////////////////////////////
// Konstruktor
CONSTRUCTOR TUnterKlasse.Create(AOwner: TComponent);
BEGIN
INHERITED Create(AOwner);
END;
////////////////////////////////////////////////////////////////////////////////
// Destruktor
DESTRUCTOR TUnterKlasse.Destroy;
BEGIN
INHERITED Destroy;
END;
////////////////////////////////////////////////////////////////////////////////
// Konstruktor
CONSTRUCTOR TOberKlasse.Create(AOwner: TComponent);
BEGIN
INHERITED Create(AOwner);
Visible := True;
END;
////////////////////////////////////////////////////////////////////////////////
// Destruktor
DESTRUCTOR TOberKlasse.Destroy();
BEGIN
INHERITED Destroy;
END;
FUNCTION GetElement(MyForm :TForm) : TOberKlasse;
BEGIN
result := TUnterKlasse.Create(MyForm);
END;
EXPORTS
GetElement;
BEGIN
END.
[edit=Daniel B]Delphi-Tags gesetzt. MfG Daniel B.[/edit]