Board index » delphi » Help in Owner Draw ListBox

Help in Owner Draw ListBox

Hi

I wrote this text for Owner Draw ListBox:
(It's only the neccessery parts of the procedure)

procedure TColorForm.ColorListDrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
Var
   Half:TRect;
Begin
     Half:=Rect;
     With Half do
     Begin
          Top:=Top+1;
          Left:=Left+5;
          Bottom:=Bottom-1;
          Right:=Left+20;
     End;
     With ColorList.Canvas do
     Begin
          Brush.Color:=CurrentColor;
          FillRect(Half);
          Brush.Color:=clWhite;
          TextOut(Half.Right+10,Half.Top,CurrentString);
     End;
end;

My problem is that when I select a line in this ListBox the text
becomes unvisible as long as it selected, and a black lines are being
draw in the top and the bottom of that line and the lines stays after
the line is not selected.
after a few wandering between lines the ListBow is full of black
lines...

Please tell me how to fix it
Thanks in advance...
Haim

 

Re:Help in Owner Draw ListBox


Hi,

You need to handle ListBox State when drawing items

if ( odsSelected in State ) then begin
  Brush.Color:= SelectedColor
  Font.Color:= SelectedfontColor

.
.
end
else begin
  Brush.Color:= CurrentColor
  Font.Color:= CurrentFontColor
end
.

Your code

if ( odsFocused in State ) then
  DrawFocusRect....

Best Regards

Jean-Luc

--
-------------------------------------------------
Jean-Luc Mattei
e-mail : mailto:jl...@club-internet.fr
url : http://www.mygale.org/~jlucm
Haim Cohen a crit dans le message <350f64b2.4804...@news.isracom.net.il>...

Quote
>Hi

>I wrote this text for Owner Draw ListBox:
>(It's only the neccessery parts of the procedure)

>procedure TColorForm.ColorListDrawItem(Control: TWinControl;
>  Index: Integer; Rect: TRect; State: TOwnerDrawState);
>Var
>   Half:TRect;
>Begin
>     Half:=Rect;
>     With Half do
>     Begin
>          Top:=Top+1;
>          Left:=Left+5;
>          Bottom:=Bottom-1;
>          Right:=Left+20;
>     End;
>     With ColorList.Canvas do
>     Begin
>          Brush.Color:=CurrentColor;
>          FillRect(Half);
>          Brush.Color:=clWhite;
>          TextOut(Half.Right+10,Half.Top,CurrentString);
>     End;
>end;

>My problem is that when I select a line in this ListBox the text
>becomes unvisible as long as it selected, and a black lines are being
>draw in the top and the bottom of that line and the lines stays after
>the line is not selected.
>after a few wandering between lines the ListBow is full of black
>lines...

>Please tell me how to fix it
>Thanks in advance...
>Haim

begin 666 Jean-Luc Mattei.vcf
M0D5'24XZ5D-!4D0-"DXZ36%T=&5I.TIE86XM3'5C#0I&3CI*96%N+4QU8R!-
M871T96D-"D%$4CM(3TU%.CL[.TUA<G-E:6QL93M";W5C:&5S(&1U(%)H]&YE
M.SM&<F%N8V4-"DQ!0D5,.TA/344[14Y#3T1)3D<]455/5$5$+5!224Y404),
M13I-87)S96EL;&4L($)O=6-H97,@9'4@4F@]1C1N93TP1#TP049R86YC90T*
M55),.FAT=' Z+R]W=W<N;7EG86QE+F]R9R]^:FQU8VT-"D5-04E,.U!2148[
M24Y415).150Z:FQU8VU 8VQU8BUI;G1E<FYE="YF<@T*14U!24P[24Y415).
M150Z:FQU8VU ;7EG86QE+F]R9PT*14U!24P[24Y415).150Z:FQU8VU 8VAE
2>BYC;VT-"D5.1#I60T%21 T*
`
end
end

Re:Help in Owner Draw ListBox


Hi Haim,

What you should do is use the FillRect Method on the Rect var instead of
the Half var. This should do it.

 procedure TColorForm.ColorListDrawItem(Control: TWinControl;
   Index: Integer; Rect: TRect; State: TOwnerDrawState);
 Var
    Half:TRect;
 Begin
      Half:=Rect;
      With Half do
      Begin
           Top:=Top+1;
           Left:=Left+5;
           Bottom:=Bottom-1;
           Right:=Left+20;
      End;
      With ColorList.Canvas do
      Begin
           Brush.Color:=CurrentColor;
           {FillRect(Half);}
             FillRect(Rect);
           Brush.Color:=clWhite;
           TextOut(Half.Right+10,Half.Top,CurrentString);
      End;
 end;

Greets  Aad-Jan

Other Threads