Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   ListBox Weiterentwicklung (https://www.delphipraxis.net/23060-listbox-weiterentwicklung.html)

Alex_ITA01 28. Mai 2004 08:21


ListBox Weiterentwicklung
 
Hallo erstmal,
ich habe eine Frage zur Komponentenentwicklung.Und zwar weiß ich das man mit folgendem Source die Farbe des Markierungsrahmens einer ListBox ändert nur würde ich gerne mir ne eigene Komponente schreiben die eine weitere Eigenschaft DrawColor besitzt und nach dieser Farbe wird dann der Rahmen angepasst.Kann mir jemand sagen wie dies funktioniert.Wäre echt klasse,
Danke schonmal im voraus.Übrigens die Eigenschaft hinzugefügt habe ich schon nur eben das setzen des Rahmens klappt nicht.
MFG Alex

Die Eigenschaft „Style" auf „lbOwnerDraw*" setzen und folgende Zeilen ins OnDrawItem-Ereignis schreiben:
Delphi-Quellcode:
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  with (Control as TListbox) do
  begin
    if odSelected in State then //Prüft, ob ein Item markiert ist
      Canvas.Brush.Color := clRed; //Canvas-Hintergrundfarbe auf Rot setzen
    Canvas.FillRect(Rect); //schön ausmalen
    Canvas.TextOut(Rect.Left, Rect.Top, Items[Index]); //Text drüberpinseln
  end;
end;
[edit=Sharky]Delphi-Tags gesetzt. Mfg, Sharky[/edit]

Jens Schumann 28. Mai 2004 08:47

Re: ListBox Weiterentwicklung
 
Hallo,
ein Blick in die VCL-Sourcen zeigt, dass in der virtuellen Methode DrawItem von TCustomListBox
das OnDrawItem-Event ausgelöst wird.
In deinem TListBoxnachfahren musst Du diese Methode überschreiben.

Alex_ITA01 28. Mai 2004 14:06

Re: ListBox Weiterentwicklung
 
Hi hier mal mein ausschnitt aus dem Source.Ich habe es versucht mit dem überschreiben der Methode aber irgendwie gehts trotzdem nicht.Könnt ihr mir bitte helfen?
Mfg Alex

Delphi-Quellcode:
unit ColorListBox;

interface

uses
  SysUtils, Classes, Controls, StdCtrls, Graphics, Windows, Messages;

type
  MyColorListBox = class(TListBox)
  private
    { Private-Deklarationen }
    FColor : TColor;
    FRect : TRect;
    FIndex : Integer;
    procedure SetColor(Value:TColor);
  protected
    { Protected-Deklarationen }
  public
    { Public-Deklarationen }
    constructor Create(AOwner:TComponent); override;
    destructor destroy; override;

    procedure DrawItem(Index: Integer; Rect: TRect;
                       State: TOwnerDrawState); override;

  published
    { Published-Deklarationen }
    property DrawColor : TColor read FColor write SetColor;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Beispiele', [MyColorListBox]);
end;

{ ColorListBox }

constructor MyColorListBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Width := 100;
  Height := 100;
end;

destructor MyColorListBox.destroy;
begin
  inherited Destroy;
end;

procedure MyColorListBox.DrawItem(Index: Integer; Rect: TRect;
  State: TOwnerDrawState);
begin
  inherited;
  FRect := Rect;
  FIndex := Index;
end;

procedure MyColorListBox.SetColor(Value: TColor);
begin
  FColor := Value;
  if ItemIndex = -1 then exit;

  Canvas.Brush.Color := FColor;
  Canvas.FillRect(FRect);
  Canvas.TextOut(FRect.Left,FRect.Top, Items[FIndex]);
end;

Jens Schumann 28. Mai 2004 16:37

Re: ListBox Weiterentwicklung
 
Hallo,
das mit Canvas.TextOut usw muss ja auch in die DrawItem Methode

Alex_ITA01 1. Jun 2004 15:59

Re: ListBox Weiterentwicklung
 
Hallo,
danke für den Hinweis aber das hatte ich auch schon probiert und es will einfach nicht gehen.

Delphi-Quellcode:
unit ColorListBox;

interface

uses
  SysUtils, Classes, Controls, StdCtrls, Graphics, Windows, Messages;

type
  MyColorListBox = class(TListBox)
  private
    { Private-Deklarationen }
    FColor : TColor;
    procedure SetColor(Value:TColor);
  protected
    { Protected-Deklarationen }
  public
    { Public-Deklarationen }
    constructor Create(AOwner:TComponent); override;
    destructor Destroy; override;

    procedure DrawItem(Index: Integer; Rect: TRect;
                       State: TOwnerDrawState); override;

  published
    { Published-Deklarationen }
    property DrawColor : TColor read FColor write SetColor;
  end;

procedure Register;
{ ----------------------------------------------------------------------------- }
implementation
{ ----------------------------------------------------------------------------- }
procedure Register;
begin
  RegisterComponents('Beispiele', [MyColorListBox]);
end;
{ ----------------------------------------------------------------------------- }
{ ColorListBox }
{ ----------------------------------------------------------------------------- }
constructor MyColorListBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Width := 100;
  Height := 100;
end;
{ ----------------------------------------------------------------------------- }
destructor MyColorListBox.Destroy;
begin
  inherited Destroy;
end;
{ ----------------------------------------------------------------------------- }
procedure MyColorListBox.DrawItem(Index: Integer; Rect: TRect;
  State: TOwnerDrawState);
begin
  inherited;
  if ItemIndex = -1 then exit;

  Canvas.Brush.Color := FColor;
  Canvas.FillRect(Rect);
  Canvas.TextOut(Rect.Left,Rect.Top, Items[Index]);
end;
{ ----------------------------------------------------------------------------- }
procedure MyColorListBox.SetColor(Value: TColor);
begin
  FColor := Value;
end;
{ ----------------------------------------------------------------------------- }
end.
jedesmal wenn ich ein element markiere habe ich ein Blauen Rahmen obwohl ich bei DrawColor rot eingestellt habe.Hast du eine Ahnung Jens?
Thx Alex

Keldorn 1. Jun 2004 16:32

Re: ListBox Weiterentwicklung
 
Hallo, ich glaube nicht, das deine ondrawroutine überhaupt aufgerufen wird. Dazu müßte der style erstmal auf ownerdraw*** stehen., kannst du auch im construcor erledigen. Außerdem wird dein gesamter Hintergrund rot sein, nicht nur die selektion, da du den Drawstate nicht beachstest, in deinem ersten Post hattest du das doch schon drin ...


Mfg Frank

Gollum 1. Jun 2004 16:50

Re: ListBox Weiterentwicklung
 
Hallo,

ausserdem fehlt in Deiner SetColor noch die Anweisung zum Neuzeichnen der Liste, damit die Farbänderung auch wirksam wird:

Delphi-Quellcode:
procedure MyColorListBox.SetColor(Value: TColor);
begin
  if (FColor<>Value) then
  begin
    FColor:=Value;
    Invalidate;
  end; // if
end;

Alex_ITA01 2. Jun 2004 13:40

Re: ListBox Weiterentwicklung
 
Danke für die Tips
Es lag daran das der Style nicht gesetzt war im Constructor.
Ich habe da aber mal noch ne Frage und zwar will ich eine Eigenschaft hinzufügen,
vom Name : ScrollBar. Diese soll mit einem 'Plus' versehen sein und wenn ich darauf klicke bekomme ich zwei weitere Eigenschaften. Einmal Visible und einmal Length.Wie funktioniert das?Wie mache ich das, wenn ich Visible auf True setze,das mit folgendem Source:
SendMessage(Handle, LB_SetHorizontalExtent,1000,Longint(0));
eine Horizontale ScrollBar eingefügt wird.

Hoffe ihr könnte mir helfen
Danke Alex

Alex_ITA01 2. Jun 2004 15:34

Re: ListBox Weiterentwicklung
 
OK das mit den 2 Eigenschaften und dem + davor habe ich hinbekommen nur will ich jetzt wenn visible auf True gesetzt wird, auch die Horizontale Scrollbar angezeigt wird
Nur bei mir kennt er Handle nicht und ich weiß nicht wie ich es 'bekomme'

Delphi-Quellcode:
unit ColorListBox1;

interface

uses
  SysUtils, Classes, Controls, StdCtrls, Graphics, Windows, Messages;

type
  TScroll = class(TGraphicsObject)
  private
    FVisible : Boolean;
    FLength  : Integer;
    procedure SetVisible(Value : Boolean);
    procedure SetLength (Value : Integer);
  protected

  public
    constructor Create;
  published
    property Visible : Boolean read FVisible write SetVisible default False;
    property Length : Integer read FLength write SetLength default 0;
  end;

  MyColorListBox1 = class(TListBox)
  private
    { Private-Deklarationen }
    FScrollbar : TScroll;
    procedure SetScrollBar (Value : TScroll);
  protected
    { Protected-Deklarationen }
  public
    { Public-Deklarationen }
    constructor Create(AOwner:TComponent); override;
    destructor Destroy; override;
  published
    { Published-Deklarationen }
    property Scrollbar : TScroll read FScrollbar write SetScrollBar;
  end;

procedure Register;
{ ----------------------------------------------------------------------------- }
implementation
{ ----------------------------------------------------------------------------- }
procedure Register;
begin
  RegisterComponents('Beispiele', [MyColorListBox1]);
end;
{ ----------------------------------------------------------------------------- }
{ ColorListBox1 }
{ ----------------------------------------------------------------------------- }
constructor MyColorListBox1.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Width  := 100;
  Height := 100;
  FScrollbar := TScroll.Create;
end;
{ ----------------------------------------------------------------------------- }
destructor MyColorListBox1.Destroy;
begin
  FScrollbar.Free;
  inherited Destroy;
end;
{ ----------------------------------------------------------------------------- }
{ TScroll }
{ ----------------------------------------------------------------------------- }
constructor TScroll.Create;
begin
  inherited Create;
  FVisible := False;
  FLength := 0;
end;
{ ----------------------------------------------------------------------------- }
procedure TScroll.SetLength(Value: Integer);
begin
  if Value <> 0 then
    FLength := Value;
end;
{ ----------------------------------------------------------------------------- }
procedure TScroll.SetVisible(Value: Boolean);
begin
  if Value = True then
  begin
    FVisible := Value;
  end;
  if FLength = 0 then
    FLength := 100;
//  SendMessage(FHandle, LB_SetHorizontalExtent,FLength,Longint(0));
{ Handle undefiniert !!!!! }
end;
{ ----------------------------------------------------------------------------- }
procedure MyColorListBox1.SetScrollBar(Value: TScroll);
begin
  if Value.FVisible = True then
  begin
    FScrollbar := Value;
    if Value.FLength = 0 then
      Value.FLength := 10;
    SendMessage(Handle, LB_SetHorizontalExtent,Value.FLength,Longint(0));
  end;
end;
{ ----------------------------------------------------------------------------- }
end.
Könnte ihr mir bitte helfen???


Alle Zeitangaben in WEZ +1. Es ist jetzt 07:49 Uhr.

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