读书人

请问:关于ListView

发布时间: 2012-03-04 11:13:34 作者: rapoo

请教:关于ListView
我想在ListView中的某个cell或某行中显示不同的颜色,该如何实现,谢谢。

[解决办法]
unit Unit1;

interface

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

type
TForm1 = class(TForm)
ListView1: TListView;
procedure ListView1AdvancedCustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage;
var DefaultDraw: Boolean);
procedure FormCreate(Sender: TObject);
procedure ListView1CustomDrawSubItem(Sender: TCustomListView;
Item: TListItem; SubItem: Integer; State: TCustomDrawState;
var DefaultDraw: Boolean);
procedure ListView1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
uses CommCtrl;

procedure TForm1.ListView1AdvancedCustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage;
var DefaultDraw: Boolean);
var
c: TCanvas;
s: string;
r: TRect;
i: integer;
begin

c := Sender.Canvas;
if (Item.SubItems[0] = '1 ') then
c.Brush.Color := clYellow
else
c.Brush.Color := clWindow;
r := Item.DisplayRect(drLabel);
c.FillRect(r);
s := Item.Caption;

c.Font.Assign(Font);

DrawText(c.Handle, pchar(s), length(s), r, dt_left or DT_VCENTER or DT_SINGLELINE or DT_END_ELLIPSIS);
for i := 0 to Item.SubItems.Count - 1 do
begin
if Boolean(ListView_GetSubItemRect(sender.Handle, item.Index, i, LVIR_ICON, @r)) then
begin

c.FillRect(r);
s := Item.SubItems[i];

DrawText(c.Handle, pchar(s), length(s), r, dt_left or DT_VCENTER or DT_SINGLELINE or DT_END_ELLIPSIS)
end;

end;
// DefaultDraw:=false;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
Item: TListItem;
begin
Item := ListView1.Items.Add;
Item.Caption := '111111 ';
Item.SubItems.Text := '1 '#13 '2 '#13 '3 ';
Item := ListView1.Items.Add;
Item.Caption := '222222 ';
Item.SubItems.Text := '1 '#13 '2 '#13 '3 ';
Item := ListView1.Items.Add;
Item.Caption := '222222 ';
Item.SubItems.Text := '0 '#13 '2 '#13 '3 ';
Item := ListView1.Items.Add;
Item.Caption := '222222 ';
Item.SubItems.Text := '0 '#13 '2 '#13 '3 ';
Item := ListView1.Items.Add;
Item.Caption := '222222 ';
Item.SubItems.Text := '1 '#13 '2 '#13 '3 ';
end;

procedure TForm1.ListView1CustomDrawSubItem(Sender: TCustomListView;
Item: TListItem; SubItem: Integer; State: TCustomDrawState;
var DefaultDraw: Boolean);
var
c: TCanvas;
s: string;
r: TRect;
i: integer;
begin

c := Sender.Canvas;
if (Item.SubItems[0] = '1 ') then
c.Brush.Color := clYellow
else
c.Brush.Color := clWindow;


c.Font.Assign(TListView(Sender).Font);

c.Font.Assign(Font);
i := SubItem;
if Boolean(ListView_GetSubItemRect(sender.Handle, item.Index, i, LVIR_LABEL, @r)) then
begin

c.FillRect(r);


s := Item.SubItems[i];
DrawText(c.Handle, pchar(s), length(s), r, dt_left or DT_VCENTER or DT_SINGLELINE or DT_END_ELLIPSIS)
end;
DefaultDraw := false;
end;

procedure TForm1.ListView1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
i: integer;
begin
i := ListView1.ItemIndex;
if i < 0 then
exit;
if (ListView1.Items[i].SubItems[0] = '1 ') then
(ListView1.Items[i].SubItems[0] := '0 ')
else
(ListView1.Items[i].SubItems[0] := '1 ');

ListView1.Repaint;
end;

end.


[解决办法]
给你个简单些的例子,斑马线:

procedure TfrmMain.ListView1CustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
if (Item.Index mod 2) = 1 then Sender.Canvas.Brush.Color := $ECECEC;
end;

至于某个cell,那你就在ListView1CustomDrawSubItem里自由发挥吧。

读书人网 >.NET

热点推荐