Einzelnen Beitrag anzeigen

Benutzerbild von Bummi
Bummi

Registriert seit: 15. Jun 2010
Ort: Augsburg Bayern Süddeutschland
3.470 Beiträge
 
Delphi XE3 Enterprise
 
#4

AW: Möchte TPanel mit Image von ImageList koppeln

  Alt 9. Apr 2013, 13:10
ganz kurz angerissen

Delphi-Quellcode:
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,ExtCtrls, StdCtrls, ImgList;

type
  TMyPanel=Class(TCustomPanel)
    Procedure Paint;override;
    private
      FImageList:TImageList;
    FImageIndex: Integer;
    procedure SetImageIndex(const Value: Integer);
    procedure SetImageList(const Value: TImageList);
    published
    Property ImageList:TImageList read FImageList Write SetImageList;
    Property ImageIndex:Integer Read FImageIndex Write SetImageIndex;

  End;

  TForm2 = class(TForm)
    ImageList1: TImageList;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

{ TMyPanel }

procedure TMyPanel.Paint;
const
 C_LOFS=10;
var
 ico:TIcon;
 Toffs:Integer;
begin
 // inherited; nicht aufrufen
  Canvas.Brush.Color := clSilver;
  Canvas.FillRect(ClientRect);
  Toffs := C_LOFS;
  if Assigned(FImageList) and (FImageList.Count>ImageIndex )then
    try
      ico:=TIcon.Create;
      FImageList.GetIcon(ImageIndex,ico);
      Canvas.Draw(C_LOFS,(Height - FImageList.Height) div 2,ico);
      Toffs := Toffs + FImageList.Width + 5;
    finally
       ico.Free;
    end;
  Canvas.TextOut(Toffs,(Height - Canvas.TextHeight('X')) div 2, Caption);
end;

procedure TMyPanel.SetImageIndex(const Value: Integer);
begin
  FImageIndex := Value;
  invalidate;
end;

procedure TMyPanel.SetImageList(const Value: TImageList);
begin
  FImageList := Value;
  invalidate;
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
   With TMyPanel.Create(self) do
    begin
      Parent := Self;
      Caption := 'Mein Testpanel';
      Width := 200;
      Height := 50;
      Left := 50;
      Top := 50;
      Imagelist := IMagelist1;
      ImageIndex := 0;
    end;
end;

end.
Thomas Wassermann H₂♂
Das Problem steckt meistens zwischen den Ohren
DRY DRY KISS
H₂ (wenn bei meinen Snipplets nichts anderes angegeben ist Lizenz: WTFPL)
  Mit Zitat antworten Zitat