Einzelnen Beitrag anzeigen

Benutzerbild von NoGAD
NoGAD

Registriert seit: 31. Jan 2006
Ort: Weimar
345 Beiträge
 
Delphi 10.4 Sydney
 
#14

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

  Alt 14. Mai 2024, 10:43
Ich habe eine Rückfrage zu SetSubComponent.

Die Komponente funktioniert sehr gut, jedoch besteht jetzt das Problem, dass das Label (SubComponente) einige Eigenschaften nicht speichert.

Z.B. Align. Ich setze die Eigenscahft der Subcomponente auf alClient, speichere das Project, schließe und lade es erneut. Dann ist die Eigenschaft wieder auf alRight.

Hier der Code der aktuellen Version

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, Vcl.Controls, Vcl.Forms, Vcl.ExtCtrls, Vcl.StdCtrls,
  Vcl.Imaging.GIFConsts,
  Vcl.Imaging.GIFImg,
  Vcl.Imaging.JConsts,
  Vcl.Imaging.jpeg,
  Vcl.Imaging.pngimage,
  Vcl.Imaging.pnglang;

type
  TLabelPosition = (lpTop, lpBottom, lpLeft, lpRight);

  TTRM_ImageLabel = class(TCustomPanel)
  private
    FSubImageComponent: TImage;
    FSubLabelComponent: TLabel;

    FLabelPosition: TLabelPosition;

    FVersion: String;

    procedure SetLabelPosition(const Value: TLabelPosition);

    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
    procedure Resize; override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property SubImageComponent: TImage read FSubImageComponent;
    property SubLabelComponent: TLabel read FSubLabelComponent;
    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 LabelPosition: TLabelPosition 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.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;

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

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

  FLabelPosition := lpBottom;

  FVersion := '2024.05.14.1038';

  Resize;
end;

procedure TTRM_ImageLabel.Resize;
begin
  inherited;
  case FLabelPosition of
    lpTop:
      begin
        FSubLabelComponent.Align := alTop;
        // FSubLabelComponent.Alignment := taCenter;
      end;
    lpBottom:
      begin
        FSubLabelComponent.Align := alBottom;
        // FSubLabelComponent.Alignment := taCenter;
      end;
    lpLeft:
      begin
        FSubLabelComponent.Align := alLeft;
        // FSubLabelComponent.Alignment := taCenter;
      end;
    lpRight:
      begin
        FSubLabelComponent.Align := alRight;
        // FSubLabelComponent.Alignment := taCenter;
      end;
  end;

  Update;
end;

procedure TTRM_ImageLabel.SetLabelPosition(const Value: TLabelPosition);
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;

end.
Mathias
Ich vergesse einfach zu viel.
  Mit Zitat antworten Zitat