1、代码来自于“C:\Program Files (x86)\Borland\Delphi7\Demos\Virtual Listview”
1.1、是使用 ListView来显示数据
1.2、自己管理 ListView里面显示的信息(也就是说,不是像StringGrid那样一格一格内容往里面插入)
2、ListView 控件,属性设置:
2.1、ViewStyle 设置为 vsReport
2.2、OwnerData 设置为 true
2.3、双击 ListView,添加 几个列
2.4、GridLines 设置为 true
2.5、RowSelect 设置为 true
2.6、ReadOnly 设置为 true
3、测试程序界面:
4、测试代码:(Delphi7)
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls;type TForm1 = class(TForm) ListView1: TListView; btnListViewTest01: TButton; btnListViewTest02: TButton; procedure btnListViewTest01Click(Sender: TObject); procedure ListView1Data(Sender: TObject; Item: TListItem); procedure btnListViewTest02Click(Sender: TObject); private Flist :TList; public { Public declarations } end; Pmsg =^Rmsg; Rmsg = record Fi1 :integer; Fi2 :integer; Fi3 :integer; end;var Form1: TForm1;implementation{ $R *.dfm}procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);var p :Pmsg;begin if ListView1.ViewStyle <> vsReport then Exit; if (Item.Index >= Flist.Count) then Exit; p := Flist.Items[Item.Index]; Item.Caption := IntToStr(p.Fi1); Item.SubItems.Add(IntToStr(p.Fi2)); Item.SubItems.Add(IntToStr(p.Fi3));end;procedure TForm1.btnListViewTest01Click(Sender: TObject);var i :Integer; p :Pmsg;begin if not Assigned(Flist) then Flist := TList.Create; if (Flist.Count <> 0) then begin for i:=0 to (Flist.Count-1) do FreeMem(Flist.Items[i], SizeOf(Rmsg)); Flist.Clear; end; for i:=0 to 5 do begin GetMem(p, SizeOf(Rmsg)); p.Fi1 := i; p.Fi2 := 10 + i; p.Fi3 := 20 + i; Flist.Add(p); end; ListView1.Items.Count := Flist.Count; ListView1.Repaint;end;procedure TForm1.btnListViewTest02Click(Sender: TObject);var i :Integer; p :Pmsg;begin if not Assigned(Flist) then Flist := TList.Create; if (Flist.Count <> 0) then begin for i:=0 to (Flist.Count-1) do FreeMem(Flist.Items[i], SizeOf(Rmsg)); Flist.Clear; end; for i:=0 to 50 do begin GetMem(p, SizeOf(Rmsg)); p.Fi1 := 100 + i; p.Fi2 := 200 + i; p.Fi3 := 300 + i; Flist.Add(p); end; ListView1.Items.Count := Flist.Count; ListView1.Repaint;end;end.
5、运行情况:
5.1、
5.2、
6、
7、
8、