将ListBox 的Style属性设置为lbOwnerDrawFixed 在其OnDrawItem事件中编写代码如下:( 我实现了加红选中项的功能,但却没有将非选中项显示出来, 我忘记了一些东西,你自己琢磨看看吧! )procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin if odSelected in state then begin (control as TListbox).Canvas.Font.Color:=clwhite; (control as TListbox).Canvas.brush.Color:=clred; end else begin (control as TListbox).Canvas.Font.Color:=clblack; (control as TListbox).Canvas.Brush.Color:=clwhite; end; (control as TListbox).Canvas.FillRect(Rect); (control as TListbox).Canvas.TextRect(Rect,2,2,(control as TListbox).Items[Index]); end;
1.listbox.style=lbOwnerDrawFixed 2. procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin if (odSelected in state) then begin ListBox1.Canvas.Brush.Color:=clBlue; ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]); end; end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin if odSelected in state then begin listbox1.Canvas.Font.Color:=clwhite; listbox1.Canvas.Brush.Color:=clred; end else begin listbox1.Canvas.Font.Color:=clblack; listbox1.Canvas.Brush.Color:=clwhite; end; end;
在其OnDrawItem事件中编写代码如下:(
我实现了加红选中项的功能,但却没有将非选中项显示出来,
我忘记了一些东西,你自己琢磨看看吧!
)procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
if odSelected in state then
begin
(control as TListbox).Canvas.Font.Color:=clwhite;
(control as TListbox).Canvas.brush.Color:=clred;
end
else
begin
(control as TListbox).Canvas.Font.Color:=clblack;
(control as TListbox).Canvas.Brush.Color:=clwhite;
end;
(control as TListbox).Canvas.FillRect(Rect);
(control as TListbox).Canvas.TextRect(Rect,2,2,(control as TListbox).Items[Index]);
end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
if (odSelected in state) then
begin
ListBox1.Canvas.Brush.Color:=clBlue;
ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]);
end;
end;
Rect: TRect; State: TOwnerDrawState);
begin
if odSelected in state then
begin
listbox1.Canvas.Font.Color:=clwhite;
listbox1.Canvas.Brush.Color:=clred;
end
else
begin
listbox1.Canvas.Font.Color:=clblack;
listbox1.Canvas.Brush.Color:=clwhite;
end;
end;