Autor Beitrag
mathias
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 58
Erhaltene Danke: 3



BeitragVerfasst: Do 20.06.02 21:53 
Zebrapapier-Effekt in TListBox einbauen

Die ListBox ist gestreift wie Zebrapapier.
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  ListColor : TColor;
  ListBrush : TBrush;
begin
  ListBrush := TBrush.Create;
  with (Control as TListBox).Canvas do begin       // in ListBox zeichnen
    if odSelected in State then begin  // Eintrag markiert
      ListColor := $FF0000;
    end else begin
      if Index and 1 = 1 then ListColor := $BBFFBB   // wenn gerade dann grün,
                         else ListColor := $FFFFFF;  // sonst weiss
    end;
    ListBrush.Style := bsSolid;
    ListBrush.Color := ListColor;
    Windows.FillRect(Handle, Rect, ListBrush.Handle);                 // Rechteck ausmalen
    Brush.Style := bsClear;
    TextOut(Rect.Left, Rect.Top, (Control as TListBox).Items[Index]); // Text schreiben
    ListBrush.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i : Integer;
begin
  for i := 0 to 100 do ListBox1.Items.Add(IntToStr(i));  // ListBox mit Werten auffüllen
  ListBox1.Style := lbOwnerDrawFixed;  // kann auch im ObjectInspector gesetzt werden
end;

end.


Aus PC-Magazin
Popov Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1655
Erhaltene Danke: 13

WinXP Prof.
Bei Kleinigkeiten D3Pro, bei größeren Sachen D6Pro oder D7
BeitragVerfasst: Do 12.02.04 21:08 
Der Effekt, sich abwechselnder Zeilenfarben[meta]Zeile, Farbe[/meta], ist dann sinnvoll, wenn die die Zeilen besonders lang sind. Dann ist es leichter dem Text zu folgen. Hier ein Tipp wie man diesen Effekt bei einer ListBox nutzt. Dazu nutzt man das Ereignis OnDrawItem der ListBox. Diese bietet ein Canvas zum Zeichnen und ein Rect-Bereich in den gezeichnet werden darf.

Zuerst muß allerdings die TListBox.Style Eigenschaft auf lbOwnerDrawFixed (oder lbOwnerDrawVariable bei variablen Höhen) gestellt werden. Wenn man das nicht macht, dann wird nicht gezeichnet.

Wichtig!

ausblenden Delphi-Quelltext
1:
2:
3:
  ...
  ListBox1.Style := lbOwnerDrawFixed;
  ...


Entweder im Code einstellen oder bereits in Objektinspektor.

Die Prozedur ist allgemein aufgebaut und kann aus jeder OnDrawItem Prozedur aufgerufen werden. Die Parameter der Prozedur sind die gleichen wie bei der OnDrawItem Prozedur. Man muß die Variablenbezeichnungen einfach nur übertragen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
procedure DrawListBoxZC(Control: TWinControl; Index: Integer; Rect: TRect;
  State: TOwnerDrawState);
const
  Col1: array [Boolean] of TColor = ($00F8F8F8, clWindow); // <<< Wunschfarben
  Col2: array [Boolean] of TColor = (clInactiveCaptionText, clWindowText);
var
  TopDif: Integer; // Gleicht die Höhendifferenz aus
begin
  with (Control as TListbox) do
  begin
    if odSelected in State then 
      Canvas.Font.Color := clCaptionText 
    else 
    begin
      Canvas.Brush.Color := Col1[Odd(Index)];
      Canvas.Font.Color := Col2[(Control as TListbox).Enabled];
    end;
    TopDif := (ItemHeight div 2) - (Canvas.TextHeight(#32div 2);
    Canvas.TextRect(Rect, Rect.Left, Rect.Top + TopDif, Items[Index]);
  end;
end{Popov}


Beispiel:

Die DrawListBox Prozedur (oben) ist universal einsetzbar und kann aus verschieden Prozeduren gleichzeitig aufgerufen werden. Hier aus der OnDrawItem Ereignisprozedur der ListBox1:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  DrawListBoxZC(Control, Index, Rect, State);
end;

Moderiert von user profile iconjasocul: Anpassungen an den Style-Guide
Moderiert von user profile iconjasocul: Beitrag geprüft am 13.05.2006

_________________
Popov