AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

ListBox Weiterentwicklung

Ein Thema von Alex_ITA01 · begonnen am 28. Mai 2004 · letzter Beitrag vom 2. Jun 2004
Antwort Antwort
Alex_ITA01

Registriert seit: 22. Sep 2003
1.115 Beiträge
 
Delphi 12 Athens
 
#1

ListBox Weiterentwicklung

  Alt 28. Mai 2004, 08:21
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]
  Mit Zitat antworten Zitat
Benutzerbild von Jens Schumann
Jens Schumann

Registriert seit: 27. Apr 2003
Ort: Bad Honnef
1.644 Beiträge
 
Delphi 2009 Professional
 
#2

Re: ListBox Weiterentwicklung

  Alt 28. Mai 2004, 08:47
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.
I come from outer space to save the human race
  Mit Zitat antworten Zitat
Alex_ITA01

Registriert seit: 22. Sep 2003
1.115 Beiträge
 
Delphi 12 Athens
 
#3

Re: ListBox Weiterentwicklung

  Alt 28. Mai 2004, 14:06
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;
  Mit Zitat antworten Zitat
Benutzerbild von Jens Schumann
Jens Schumann

Registriert seit: 27. Apr 2003
Ort: Bad Honnef
1.644 Beiträge
 
Delphi 2009 Professional
 
#4

Re: ListBox Weiterentwicklung

  Alt 28. Mai 2004, 16:37
Hallo,
das mit Canvas.TextOut usw muss ja auch in die DrawItem Methode
I come from outer space to save the human race
  Mit Zitat antworten Zitat
Alex_ITA01

Registriert seit: 22. Sep 2003
1.115 Beiträge
 
Delphi 12 Athens
 
#5

Re: ListBox Weiterentwicklung

  Alt 1. Jun 2004, 15:59
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
  Mit Zitat antworten Zitat
Keldorn

Registriert seit: 6. Mär 2003
Ort: Meißen
876 Beiträge
 
Delphi 10.1 Berlin Professional
 
#6

Re: ListBox Weiterentwicklung

  Alt 1. Jun 2004, 16:32
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

Lükes Grundlage der Programmierung:
Es wird nicht funktionieren
(Murphy)
  Mit Zitat antworten Zitat
Benutzerbild von Gollum
Gollum

Registriert seit: 14. Jan 2003
Ort: Boxberg
456 Beiträge
 
Delphi 10.1 Berlin Professional
 
#7

Re: ListBox Weiterentwicklung

  Alt 1. Jun 2004, 16:50
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;
  Mit Zitat antworten Zitat
Alex_ITA01

Registriert seit: 22. Sep 2003
1.115 Beiträge
 
Delphi 12 Athens
 
#8

Re: ListBox Weiterentwicklung

  Alt 2. Jun 2004, 13:40
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
  Mit Zitat antworten Zitat
Alex_ITA01

Registriert seit: 22. Sep 2003
1.115 Beiträge
 
Delphi 12 Athens
 
#9

Re: ListBox Weiterentwicklung

  Alt 2. Jun 2004, 15:34
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???
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 20: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