Einzelnen Beitrag anzeigen

Benutzerbild von NoGAD
NoGAD

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

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

  Alt 15. Mai 2024, 08:13
Hier ist die aktuelle Variante der Komponente. Jetzt klappt es auch mit den Zuweisungen. Der Fehler war, dass ich TSubPosition nur als (lpTop, lpBottom, lpLeft, lpRight) deklariert hatte. Wenn ich dann die Subkomponente (Label) aber z.B. als Client eingestellt hatte, gab es diesen ominösen Fehler beim Laden. Jetzt, nachdem ich TSubPosition als (lpNone, lpTop, lpBottom, lpLeft, lpRight, lpClient, lpCustom) deklariert habe, klappt es.

Was ich jetzt noch gern machen würde, wäre eine Abkürzung, um ein Bild (png,bmp,gif,...) über den ObjectInspector als Property bereitstellen zu können.

Aber ich scheitere an der korrekten Zuweisung



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,
  Vcl.Graphics;

const
  _version = '2024.05.15.0901';

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

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

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

    FVersion: String;

    procedure SetImagePosition(const Value: TSubPosition);

    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 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);
  try
    FSubImage.SetSubComponent(True);
    FSubImage.Parent := Self;
    FSubImage.Align := alClient;
    FSubImage.OnClick := ImageClick;
    FSubImage.OnDblClick := ImageDblClick;
    FSubImage.OnMouseEnter := ImageMouseEnter;
    FSubImage.OnMouseLeave := ImageMouseLeave;
  finally
  end;

  FSubLabel := TLabel.Create(Self);
  try
    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;
  finally
  end;

  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.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;

  inherited Destroy;

end;

end.


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