Delphi-PRAXiS
Seite 3 von 4     123 4      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi Komponente (TImage & TLabel) -> Image wird nicht angezeigt (https://www.delphipraxis.net/214936-komponente-timage-tlabel-image-wird-nicht-angezeigt.html)

himitsu 15. Mai 2024 16:38

AW: Komponente (TImage & TLabel) -> Image wird nicht angezeigt
 
Ja klar, aber auch dort auch jemand was reingeladen?

KodeZwerg 15. Mai 2024 22:46

AW: Komponente (TImage & TLabel) -> Image wird nicht angezeigt
 
Zitat:

Zitat von himitsu (Beitrag 1536715)
Wozu die leeren Try-Finally?

Bei mir war es nur ein schneller runtime test, schubs einfach "Visible := True" ins finally :D
Zitat:

Zitat von himitsu (Beitrag 1536715)
Die Free im Destroy sind nicht nötig, da du Self als Owner nutzt und somit dein ImageLabel das automatisch freigibt.

Ich räum gerne auf :lol:

NoGAD 16. Mai 2024 08:14

AW: Komponente (TImage & TLabel) -> Image wird nicht angezeigt
 
Hallo nochmal.

Ich habe die Komponente erweitert. Jetzt klappt es mit der Zuweisung "von außen" :-)

Eine weitere Frage habe ich aber: Kann man die Property Picture der Subkomponente entfernen, dass diese nicht mehr sichtbar ist?


Delphi-Quellcode:
unit TRM_ImageLabel;

(*

  TRM_ImageLabel erstellt von Mathias Fiege 2024

  www.nogad.de

  Open Source License
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software component and associated documentation files (the "Software"),
  to deal in the Software without restriction, including without limitation the
  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  sell copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

  Thanks and greetings to www.delphipraxis.net

*)

interface


uses
  System.SysUtils, System.Classes, System.Types, Vcl.Controls, Vcl.ExtCtrls, Vcl.Forms, Vcl.StdCtrls,
  Vcl.Imaging.GIFConsts,
  Vcl.Imaging.GIFImg,
  Vcl.Imaging.JConsts,
  Vcl.Imaging.jpeg,
  Vcl.Imaging.pngimage,
  Vcl.Imaging.pnglang,
  Vcl.Graphics;

const
  _version = '2024.05.16.0913';

type
  TSubPosition = (lpNone, lpTop, lpBottom, lpLeft, lpRight, lpClient, lpCustom);

  TTRM_ImageLabel = class(TCustomPanel)
  private
    FSubImage: TImage;
    FImagePosition: TSubPosition;
    FImagePicture: TPicture;

    FSubLabel: TLabel;
    FLabelCaption: String;
    FLabelPosition: TSubPosition;

    FVersion: String;

    procedure SetImagePosition(const Value: TSubPosition);
    procedure PictureChanged(Sender: TObject);
    procedure SetPicture(Value: TPicture);

    procedure SetLabelCaption(const Value: String);
    procedure SetLabelPosition(const Value: TSubPosition);

    procedure ImageClick(Sender: TObject);
    procedure ImageDblClick(Sender: TObject);
    procedure ImageMouseEnter(Sender: TObject);
    procedure ImageMouseLeave(Sender: TObject);

    procedure LabelClick(Sender: TObject);
    procedure LabelDblClick(Sender: TObject);
    procedure LabelMouseEnter(Sender: TObject);
    procedure LabelMouseLeave(Sender: TObject);
  protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Resize; override;
  published
    property SubImage: TImage read FSubImage;
    property SubLabel: TLabel read FSubLabel;
    property Align;
    property Anchors;

    property BevelInner;
    property BevelKind;
    property BevelOuter;
    property BevelWidth;
    property BorderStyle;
    property BorderWidth;
    property Caption;
    property Ctl3D;
    property Cursor;
    property ParentBackground;
    property PopupMenu;

    property OnClick;
    property OnDblClick;
    property OnMouseEnter;
    property OnMouseLeave;

    property ImagePosition: TSubPosition read FImagePosition write SetImagePosition;
    property ImagePicture: TPicture read FImagePicture write SetPicture;

    property LabelCaption: String read FLabelCaption write SetLabelCaption;
    property LabelPosition: TSubPosition read FLabelPosition write SetLabelPosition;

    property Version: String read FVersion;

  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('TRM', [TTRM_ImageLabel]);

end;

{ TTRM_ImageLabel }

constructor TTRM_ImageLabel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Self.Parent := TWinControl(AOwner);

  FSubImage := TImage.Create(Self);
  FSubImage.SetSubComponent(True);
  FSubImage.Parent := Self;
  FSubImage.Align := alClient;
  FSubImage.Center := True;
  FSubImage.OnClick := ImageClick;
  FSubImage.OnDblClick := ImageDblClick;
  FSubImage.OnMouseEnter := ImageMouseEnter;
  FSubImage.OnMouseLeave := ImageMouseLeave;

  FSubLabel := TLabel.Create(Self);
  FSubLabel.SetSubComponent(True);
  FSubLabel.Parent := Self;
  FSubLabel.Align := alBottom;
  FSubLabel.AutoSize := True;
  FSubLabel.Height := 25;
  FSubLabel.AlignWithMargins := False;
  FSubLabel.Margins.Left := 0;
  FSubLabel.Margins.Right := 0;
  FSubLabel.Margins.Top := 8;
  FSubLabel.Margins.Bottom := 0;
  FSubLabel.Caption := '...';
  FSubLabel.Font.Size := 14;
  FSubLabel.OnClick := LabelClick;
  FSubLabel.OnDblClick := LabelDblClick;
  FSubLabel.OnMouseEnter := LabelMouseEnter;
  FSubLabel.OnMouseLeave := LabelMouseLeave;

  FImagePicture := TPicture.Create;
  FImagePicture.OnChange := PictureChanged;

  Self.Width := 64;
  Self.Height := 89;
  Self.AlignWithMargins := True;
  Self.Margins.Top := 0;
  Self.Margins.Left := 8;
  Self.Margins.Right := 8;
  Self.Margins.Bottom := 0;
  Self.BevelInner := bvNone;
  Self.BevelKind := bkNone;
  Self.BevelOuter := bvNone;
  Self.BevelWidth := 1;
  Self.BorderStyle := bsNone;
  Self.BorderWidth := 0;
  Self.Caption := '';
  Self.Ctl3D := False;
  Self.Cursor := crHandPoint;
  Self.ParentBackground := True;

  FVersion := _version;

  FImagePosition := lpClient;
  FLabelPosition := lpBottom;

  Update;
  Resize;

end;

procedure TTRM_ImageLabel.Resize;
begin
  (* lpNone, lpTop, lpBottom, lpLeft, lpRight, lpClient, lpCustom *)
  inherited;
  case FLabelPosition of
    lpNone:
      begin
        FSubLabel.Align := alNone;
      end;
    lpTop:
      begin
        FSubLabel.Align := alTop;
      end;
    lpBottom:
      begin
        FSubLabel.Align := alBottom;
      end;
    lpLeft:
      begin
        FSubLabel.Align := alLeft;
      end;
    lpRight:
      begin
        FSubLabel.Align := alRight;
      end;
    lpClient:
      begin
        FSubLabel.Align := alClient;
      end;
    lpCustom:
      begin
        FSubLabel.Align := alCustom;
      end;
  end;

  case FImagePosition of
    lpNone:
      begin
        FSubImage.Align := alNone;
      end;
    lpTop:
      begin
        FSubImage.Align := alTop;
      end;
    lpBottom:
      begin
        FSubImage.Align := alBottom;
      end;
    lpLeft:
      begin
        FSubImage.Align := alLeft;
      end;
    lpRight:
      begin
        FSubImage.Align := alRight;
      end;
    lpClient:
      begin
        FSubImage.Align := alClient;
      end;
    lpCustom:
      begin
        FSubImage.Align := alCustom;
      end;
  end;

  FSubLabel.Caption := FLabelCaption;

  Update;

end;

procedure TTRM_ImageLabel.SetImagePosition(const Value: TSubPosition);
begin
  if FImagePosition <> Value then
  begin
    FImagePosition := Value;
    Resize;
  end;

end;

procedure TTRM_ImageLabel.SetPicture(Value: TPicture);
begin
  FImagePicture.Assign(Value);
  FSubImage.Picture.Assign(FImagePicture);

end;

procedure TTRM_ImageLabel.PictureChanged(Sender: TObject);
begin
  Repaint;

end;

procedure TTRM_ImageLabel.SetLabelCaption(const Value: string);
begin
  if FLabelCaption <> Value then
  begin
    FLabelCaption := Value;
    Resize;
  end;

end;

procedure TTRM_ImageLabel.SetLabelPosition(const Value: TSubPosition);
begin
  if FLabelPosition <> Value then
  begin
    FLabelPosition := Value;
    Resize;
  end;

end;

procedure TTRM_ImageLabel.ImageClick(Sender: TObject);
begin
  if Assigned(OnClick) then
    OnClick(Self);

end;

procedure TTRM_ImageLabel.ImageDblClick(Sender: TObject);
begin
  if Assigned(OnDblClick) then
    OnDblClick(Self);

end;

procedure TTRM_ImageLabel.ImageMouseEnter(Sender: TObject);
begin
  if Assigned(OnMouseEnter) then
    OnMouseEnter(Self);

end;

procedure TTRM_ImageLabel.ImageMouseLeave(Sender: TObject);
begin
  if Assigned(OnMouseLeave) then
    OnMouseLeave(Self);
end;

procedure TTRM_ImageLabel.LabelClick(Sender: TObject);
begin
  if Assigned(OnClick) then
    OnClick(Self);

end;

procedure TTRM_ImageLabel.LabelDblClick(Sender: TObject);
begin
  if Assigned(OnDblClick) then
    OnDblClick(Self);

end;

procedure TTRM_ImageLabel.LabelMouseEnter(Sender: TObject);
begin
  if Assigned(OnMouseEnter) then
    OnMouseEnter(Self);

end;

procedure TTRM_ImageLabel.LabelMouseLeave(Sender: TObject);
begin
  if Assigned(OnMouseLeave) then
    OnMouseLeave(Self);

end;

destructor TTRM_ImageLabel.Destroy;
begin
  FSubImage.Free;
  FSubLabel.Free;
  FImagePicture.Free;

  inherited Destroy;

end;

end.

Sinspin 16. Mai 2024 09:52

AW: Komponente (TImage & TLabel) -> Image wird nicht angezeigt
 
Warum hast Du die Subkomponenten eigentlich sichtbar? Was willst Du in denen einstellen was dir nicht woanders Ärger macht?

Bei guten komponenten gäbe es unterhalb von TImage ein TCommonImage. TImage würde keine andere Function haben außer alle Properties auf published zu setzen. Eine Ableitung von TCommonImage würde dir dann erlauben wegzulassen was du nicht brauchst.

Aber hier geht das genauso TImage -> TMyImage. Alle Properties die Du nicht brauchst deklarierst Du unter public und nicht published.

NoGAD 16. Mai 2024 11:25

AW: Komponente (TImage & TLabel) -> Image wird nicht angezeigt
 
Zitat:

Zitat von Sinspin (Beitrag 1536737)
Warum hast Du die Subkomponenten eigentlich sichtbar?

Hallo Stefan,

weil ich nicht weiß, wie es anders gemacht wird und angenommen hatte, dass properties immer in published gehören. Ich mache die Programmiererei als Hobby, bin kein gelernter Programmierer.

Ich versuche nachher nochmals die Subkomponenten nicht ins published zu stecken. :-)

Danke für die Hilfe.

NoGAD 16. Mai 2024 13:13

AW: Komponente (TImage & TLabel) -> Image wird nicht angezeigt
 
Huhu.

Scheinbar habe ich einige Prizipien nicht gelernt/verstanden.

Nachdem ich die beiden Subkomponenten verschoben abe, ist es mir nicht mehr möglich, Standard-Eigenschaften von den Komponenten anzusprechen.

Delphi-Quellcode:
  public
    property SubImage: TImage read FSubImage;
    property SubLabel: TLabel read FSubLabel;
Jetzt müsste ich der Hauptkomponente also alle Eigenschaften, die ich später im Code setzten möchte, noch hinzufügen.

Wo liegt jetzt der Vorteil in dieser Vorgensweise?

LG nach Dubai :-)

Sinspin 16. Mai 2024 14:37

AW: Komponente (TImage & TLabel) -> Image wird nicht angezeigt
 
Zitat:

Zitat von NoGAD (Beitrag 1536761)
Jetzt müsste ich der Hauptkomponente also alle Eigenschaften, die ich später im Code setzten möchte, noch hinzufügen.

Wo liegt jetzt der Vorteil in dieser Vorgensweise?

Das hängt davon ab was Du vorhast, ob das ein Vorteil ist oder nicht.
Ich habe Komponenten die fassen 5 oder 6 andere in einer zusammen. Die möchte ich nicht alle im Property Editor haben.
Ich lege für die dann aber garkein Property an. Ich habe ja die lokale Variable im Object.

Aber, für alles was Du nur im Code setzen willst brauchst Du ja nicht wirklich Properties. Nur was über den Editor zur Designzeit einstellbar sein soll muss zwingend ein Property bekommen.
Durch Getter und Setter hat man bestimmte Vorteile. Ich liebe Properties dafür. Aber die sind bei mir trotzdem nur public oder protected deklariert. Nicht published. Denn ich brauche sie nicht sichtbar zur Designzeit.

Zitat:

Zitat von NoGAD (Beitrag 1536747)
weil ich nicht weiß, wie es anders gemacht wird und angenommen hatte, dass properties immer in published gehören. Ich mache die Programmiererei als Hobby, bin kein gelernter Programmierer.

Keine Sorge, es ist absolut egal warum du programmierst. Wer nett fragt bekommt Hilfe.

NoGAD 17. Mai 2024 08:16

AW: Komponente (TImage & TLabel) -> Image wird nicht angezeigt
 
Guten Morgen.

Hier ist ein Test, damit ich das Prinzip verstehen kann.

Was mir jetzt fehlt, sind folgende Properties:
Falls möglich, hätte ich die gerne gebündelt dargestellt, will heißen, im ObjectExplorer sollte z.B.:

Code:
aImage + (aufklappbar)
  - Caption
  - Visible
aImage + (aufklappbar)
  - Picture
  - Visible
[..] (weitere property)
erreichbar sein.

Code:
TMyLabel ->
    property Caption;
    property Visible; // als Test

TMyImage ->
    property Picture;
    property Visible; // auch ein Test
Delphi-Quellcode:
unit ImageLabelTEST;

interface

uses
  System.SysUtils, System.Classes, Vcl.Controls, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Graphics, Vcl.Forms;

type
  TMyImage = class(TImage)
  private
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Picture;
    property Visible;
  end;

  TMyLabel = class(TLabel)
  private
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Caption;
    property Visible;
  end;

  TImageLabelTEST = class(TCustomControl)
  private
    FImage: TMyImage;
    FLabel: TMyLabel;

    function GetImage: TPicture;
    procedure SetImage(const Value: TPicture);

    function GetLabelCaption: TCaption;
    procedure SetLabelCaption(const Value: TCaption);
    function GetLabelFont: TFont;
    procedure SetLabelFont(const Value: TFont);
  protected
    procedure Resize; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property aImage: TMyImage read FImage;
    property aLabel: TMyLabel read FLabel;
  published
// folgende properties würde ich gerne mit den properties aus den eigenen Subkomponenten ersetzen
    property I_Picture: TPicture read GetImage write SetImage;

    property L_Caption: TCaption read GetLabelCaption write SetLabelCaption;
    property L_Font: TFont read GetLabelFont write SetLabelFont;

    property Align;
    property Anchors;
    property Visible;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('TRM', [TImageLabelTEST]);
end;

{ TMyImage }
constructor TMyImage.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

end;

destructor TMyImage.Destroy;
begin
  Self.Free;
  inherited Destroy;

end;

{ TMyLabel }
constructor TMyLabel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

end;

destructor TMyLabel.Destroy;
begin
  Self.Free;
  inherited Destroy;

end;

{ TImageLabel }
constructor TImageLabelTEST.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  FImage := TMyImage.Create(Self);
  FImage.Parent := Self;
  FImage.Align := alTop;
  FImage.Height := 100;

  FLabel := TMyLabel.Create(Self);
  FLabel.Parent := Self;
  FLabel.Align := alClient;
  FLabel.Alignment := taCenter;
end;

procedure TImageLabelTEST.Resize;
begin
  inherited;

end;

function TImageLabelTEST.GetImage: TPicture;
begin
  Result := FImage.Picture;

end;

procedure TImageLabelTEST.SetImage(const Value: TPicture);
begin
  FImage.Picture.Assign(Value);

end;

function TImageLabelTEST.GetLabelCaption: TCaption;
begin
  Result := FLabel.Caption;

end;

procedure TImageLabelTEST.SetLabelCaption(const Value: TCaption);
begin
  FLabel.Caption := Value;

end;

function TImageLabelTEST.GetLabelFont: TFont;
begin
  Result := FLabel.Font;

end;

procedure TImageLabelTEST.SetLabelFont(const Value: TFont);
begin
  FLabel.Font.Assign(Value);

end;

destructor TImageLabelTEST.Destroy;
begin
  FImage.Free;
  FLabel.Free;
  inherited Destroy;

end;

end.

Sinspin 17. Mai 2024 12:31

AW: Komponente (TImage & TLabel) -> Image wird nicht angezeigt
 
Zitat:

Zitat von NoGAD (Beitrag 1536785)
Delphi-Quellcode:
// folgende properties würde ich gerne mit den properties aus den eigenen Subkomponenten ersetzen
    property I_Picture: TPicture read GetImage write SetImage;

    property L_Caption: TCaption read GetLabelCaption write SetLabelCaption;
    property L_Font: TFont read GetLabelFont write SetLabelFont;
Delphi-Quellcode:
type
  TMyImage = class(TImage)
  private
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Picture;
    property Visible;
  end;

  TMyLabel = class(TLabel)
  private
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Caption;
    property Visible;
  end;

Wenn die Komponenten in Delphi schöner implementiert wären, wäre das so einfach wie in deinem Beispiel. Leider ist es nicht so einfach.
Alles was Du nicht unter published haben willst must Du in deiner Ableitung als public deklarieren. Weglassen bedeutet das es in der gleichen Sichtbarkeit bleibt wie es ist.

Dann könntest du das so machen:
Delphi-Quellcode:
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property ImageProps: TMyImage read FImage;
    property LabelProps: TMyLabel read FLabel;

    property Align;
    property Anchors;
    property Visible;
  end;

NoGAD 17. Mai 2024 20:04

AW: Komponente (TImage & TLabel) -> Image wird nicht angezeigt
 
Ich verzweifle bald ;-)

Testweise habe ich in den SubComponenten Anchors beim TImage und Caption beim TLabel in public verschoben.

Dennoch sind die weiterhin im Objectexplorer sichtbar und auch änderbar.

Wo ist denn mein Fehler im Code, damit die beiden Properties nicht mehr angezeigt werden?


Delphi-Quellcode:
unit ImageLabelTEST;

interface

uses
  System.SysUtils, System.Classes, Vcl.Controls, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Graphics, Vcl.Forms;

type
  { TMyImage }
  TMyImage = class(TImage)
  private
  protected
  public
    property Anchors;
  published
  end;

  { TMyLabel }
  TMyLabel = class(TLabel)
  private
  protected
  public
    property Caption;
  published
  end;

  { TIMageLabelTest }
  TImageLabelTEST = class(TCustomControl)
  private
    MyImage: TMyImage;
    MyLabel: TMyLabel;
  protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Resize; override;
  published
    property MySubImage: TMyImage read MyImage;
    property MySubLabel: TMyLabel read MyLabel;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('TRM', [TImageLabelTEST]);
end;

{ TMyImage }

{ TMyLabel }

{ TImageLabel }
constructor TImageLabelTEST.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Self.Parent := TWinControl(AOwner);

  MyImage := TMyImage.Create(Self);
  MyImage.SetSubComponent(True);
  MyImage.Parent := Self;

  MyLabel := TMyLabel.Create(Self);
  MyLabel.SetSubComponent(True);
  MyLabel.Parent := Self;

end;

procedure TImageLabelTEST.Resize;
begin
  inherited;

end;

destructor TImageLabelTEST.Destroy;
begin
  MyImage.Free;
  MyLabel.Free;
  inherited Destroy;

end;

end.
LG Mathias


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:57 Uhr.
Seite 3 von 4     123 4      

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz