很想实现像Windows资源管理器在使用详细资料方式查看文件时,点击列标题自动排序的功能。看了Delphi6的帮助,基本的排序功能是实现了,但不能显示表示升/降序的“小三角”。因为技术有限,找不出实现列标题显示的代码 ,也不会重写显示“小三角”的代码,所以只好使用两个特殊符号中的箭头来表示,哪位高手能实现“小三角”的请指教指教!代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls;
const
AscText = '↑';
DescText = '↓';
type
TForm1 = class(TForm)
ListView1: TListView;
procedure ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn);
procedure ListView1CustomDraw(Sender: TCustomListView;
const ARect: TRect; var DefaultDraw: Boolean);
private
{ Private declarations }
ColumnToSort: Integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetSortType(S: String): Integer;
begin
Result:= 0;
if pos(AscText, S) > 0 then
Result:= 1
else if pos(DescText, S) > 0 then
Result:= -1;
end;
procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
var
ix: Integer;
begin
if GetSortType(Item1.ListView.Column[ColumnToSort].Caption) >= 0 then
begin
if ColumnToSort = 0 then
Compare := CompareText(Item1.Caption,Item2.Caption)
else begin
ix := ColumnToSort - 1;
Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
end;
end
else begin
if ColumnToSort = 0 then
Compare := CompareText(Item2.Caption,Item1.Caption)
else begin
ix := ColumnToSort - 1;
Compare := CompareText(Item2.SubItems[ix],Item1.SubItems[ix]);
end;
end;
end;
procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
var
i: Integer;
begin
case GetSortType(Column.Caption) of
-1: Column.Caption:= StringReplace(column.Caption, DescText, AscText, []);
0: Column.Caption:= Column.Caption + AscText;
1: Column.Caption:= StringReplace(column.Caption, AscText, DescText, []);
end;
ColumnToSort := Column.Index;
(Sender as TCustomListView).AlphaSort;
end;
procedure TForm1.ListView1CustomDraw(Sender: TCustomListView;
const ARect: TRect; var DefaultDraw: Boolean);
var
i: Integer;
List: TListView;
begin
if Sender is TListView then
begin
List:= (Sender as TListView);
for i:= 0 to List.Columns.Count -1 do
begin
if i <> ColumnToSort then
begin
List.Columns[i].Caption:=
StringReplace(List.Columns[i].Caption, AscText, '', []);
List.Columns[i].Caption:=
StringReplace(List.Columns[i].Caption, DescText, '', []);
end;
end;
end;
end;
end.