查看: 135|回复: 0

[教程] Delphi7中Listview的常用功能汇总

[复制链接]

3

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2010-3-19
发表于 2014-7-17 10:25:47 | 显示全部楼层 |阅读模式

有些时候我们在使用Delphi7的Listview过程中总是要改一些默认的设置,现在把它们集中起来汇总如下。

MultiSelect := True;   使Listview可以同时选择多行

GridLines := True;     使Listview显示格线

ViewStyle := vsReport; 显示数据项的详细列表

HideSelection := True;  使listview失去焦点时,选中行不高亮

//设置颜色
procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
 Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
 subRect, itemRect: TRect;
 i, SubItem: Integer;
begin
 DefaultDraw := False;
 if Item.Selected then begin
  Sender.Canvas.Font.Color := clRed; //选中行字体颜色
  Sender.Canvas.Brush.Color := clgray; //clGreen; 选中行高亮颜色
 end else begin
  Sender.Canvas.Font.Color := clNavy; //正常行字体颜色
  Sender.Canvas.Brush.Color := clWhite; //正常行高亮颜色
 end;
 itemRect := Item.DisplayRect(drLabel);
 subRect := itemRect;
 for SubItem := 0 to (Sender as TListView).Columns.Count - 1 do
 begin
  subRect.Left := itemRect.Left;
  for i := 1 to SubItem do
  begin
   subRect.Left := subRect.Left + (Sender as TListView).Column[i - 1].Width;
   subRect.Right := subRect.Right + SubRect.Left + (Sender as TListView).Column.Width;
  end;
  if SubItem = 0 then
  begin
   subRect.Right := subRect.Right + 2;
   Sender.Canvas.TextRect(subRect, subRect.Left, subRect.Top, Item.Caption);
  end else
   Sender.Canvas.TextRect(subRect, subRect.Left, subRect.Top, Item.SubItems[SubItem - 1]);
 end;
end;

//排序功能
 private
  { Private declarations }
  SortCol: Integer;
  SortWay: Integer;
 
procedure TForm1.ListView1ColumnClick(Sender: TObject;
 Column: TListColumn);
begin
 SortCol := Column.Index;
 if (SortWay = 1) then SortWay := -1 else SortWay := 1;
 (Sender as TCustomListView).AlphaSort;
end;

procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
 Data: Integer; var Compare: Integer);
var
 t: Integer;
begin
 if (SortCol = 0) then
 begin
  Compare := SortWay * CompareText(Item1.Caption, Item2.Caption);
 end else
 begin
  t := SortCol - 1;
  Compare := SortWay * CompareText(Item1.SubItems[t], Item2.SubItems[t]);
 end;
end;

这个功能存在一个问题:数字排序会按字符类似排,例如:1,10,102,3,34,356......感兴趣的读者可以加以完善

您可能感兴趣的文章:
  • DELPHI7.0 获取硬盘、CPU、网卡序列号的代码
  • Delphi实现限定软件使用时间的方法
  • Delphi实现图片滚动切换的完整实例代码
  • Delphi之Pascal语言中的关键字及保留字汇总
  • Delphi常用关键字用法详解
  • Delphi实现读取系统时间与日期完整实例
  • Delphi中对时间操作方法汇总
  • delphi7连接mysql5的实现方法
  • Delphi实现获取磁盘空间大小的方法
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

圆梦公社,专注于为全球华人提供纯粹技术交流的地方,请勿发布任何政治及违法的言论。如有相关侵权、举报、投诉及建议等,请发 E-mail:dzh188@hotmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部