Einzelnen Beitrag anzeigen

Alter Mann

Registriert seit: 15. Nov 2003
Ort: Berlin
947 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#8

Re: Komponentenentwicklung für Fortgeschrittene

  Alt 26. Okt 2006, 13:11
Hallo Trantor,

kleine Anregung, Änderungen erwünscht:
Delphi-Quellcode:

unit Unit2;

...

type
  TBorderImage = class(TCustomControl)
  private
     FFileName : String;
     FImage : TImage;
     procedure SetFileName(Value : String);
  protected
     procedure Paint; override;
  public
     constructor Create(AOwner: TComponent); override;
     destructor Destroy; override;
  published
     property FileName : String read FFileName write SetFileName;
  end;

implementation

constructor TBorderImage.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Width := 130;
  Height := 100;
  Visible := True;
  FFileName := '';
  FImage := TImage.Create(self);
  with FImage do
  begin
    AutoSize := False;
    Center := True;
    Proportional := True;
    Picture := TPicture.Create;
    Left := 2;
    Top := 2;
  end;
  InsertControl(FImage);
  FImage.Width := Width - 4;
  FImage.Height := Height - 4;
end;

destructor TBorderImage.Destroy;
begin
  if FImage <> nil then FImage.Free;
  inherited Destroy;
end;

procedure TBorderImage.SetFileName(Value : String);
begin
  if (Value <> FFileName) and FileExists(Value) then
  begin
    FFileName := Value;
    FImage.Picture.LoadFromFile(FFileName);
  end;
end;

procedure TBorderImage.Paint;
var
  PaintRect: TRect;
begin
  PaintRect := GetClientRect;
  inherited Paint;
  Canvas.Pen.Color := clBlue;
  Canvas.Rectangle(0, 0, PaintRect.Right, PaintRect.Bottom);
end;
Und verwendet wird es wie folgt:

Delphi-Quellcode:
unit Unit1;
...
implementation

uses unit2;

var
  BI : TBorderImage;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    BI.FileName := OpenDialog1.FileName;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  BI := TBorderImage.Create(Form1);
  Form1.InsertControl(BI);
  BI.Left := 10;
  BI.Top := 10;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  BI.Free;
end;
  Mit Zitat antworten Zitat