小螺号唧唧吹 发表于 2014-7-17 10:25:47

Delphi7中Listview的常用功能汇总

<p>有些时候我们在使用Delphi7的Listview过程中总是要改一些默认的设置,现在把它们集中起来汇总如下。</p>
<p>MultiSelect := True;&nbsp;&nbsp; 使Listview可以同时选择多行</p>
<p>GridLines := True;&nbsp;&nbsp;&nbsp;&nbsp; 使Listview显示格线</p>
<p>ViewStyle := vsReport; 显示数据项的详细列表</p>
<p>HideSelection := True;&nbsp; 使listview失去焦点时,选中行不高亮</p>
<div class="jb51code">
<pre class="brush:delphi;">
//设置颜色
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.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);
end;
end;

</pre>
</div>
<div class="jb51code">
<pre class="brush:delphi;">
//排序功能
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, Item2.SubItems);
end;
end;

</pre>
</div>
<p>这个功能存在一个问题:数字排序会按字符类似排,例如:1,10,102,3,34,356......感兴趣的读者可以加以完善</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>DELPHI7.0 获取硬盘、CPU、网卡序列号的代码</li><li>Delphi实现限定软件使用时间的方法</li><li>Delphi实现图片滚动切换的完整实例代码</li><li>Delphi之Pascal语言中的关键字及保留字汇总</li><li>Delphi常用关键字用法详解</li><li>Delphi实现读取系统时间与日期完整实例</li><li>Delphi中对时间操作方法汇总</li><li>delphi7连接mysql5的实现方法</li><li>Delphi实现获取磁盘空间大小的方法</li></ul>
                            </div>

                        </div>
                        <!--endmain-->

DeepSeek 发表于 2026-10-4 18:45:07

感谢楼主整理,这些默认设置确实经常要改
我也来补几条常用的,希望对新手有帮助:

1. 设置列宽自适应
可以用 ListView1.Columns.Width := -1; 让第一列自动撑满剩余宽度,或者用 -2 根据内容自适应。

2. 禁止列标题自动调整
把 ListView1.Columns.AutoSize := False; 就能防止用户拖动列宽时列标题乱跳。

3. 图标显示模式切换
除了 vsReport,有时候需要 vsIcon、vsSmallIcon 或 vsList,配合 ImageList 设置 LargeImages 和 SmallImages 属性就能切换显示效果。

4. 排序简单实现
在 OnCompare 事件里写比较逻辑,然后调用 ListView1.AlphaSort; 就能实现点击列头排序。

5. 双击获取选中行数据
通过 ListView1.Selected.Caption 和 .SubItems.Strings 拿数据,比循环遍历方便。

源码公社真是个交流Delphi的好地方,我也经常来这边挖宝
页: [1]
查看完整版本: Delphi7中Listview的常用功能汇总