delphi – 使用箭头对ListView列进行排序

前端之家收集整理的这篇文章主要介绍了delphi – 使用箭头对ListView列进行排序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Delphi 6并希望添加对ListView进行排序的功能,就像在 Windows资源管理器中完成一样.

在第一次测试中,我(快速和肮脏)从几个来源复制了一些源代码,并做了一些小的调整:

这就是我到目前为止(现在只有快速和肮脏):

uses
  CommCtrls;

var
  Descending: Boolean;
  SortedColumn: Integer;

const
  { For Windows >= XP }
  {$EXTERNALSYM HDF_SORTUP}
  HDF_SORTUP              = $0400;
  {$EXTERNALSYM HDF_SORTDOWN}
  HDF_SORTDOWN            = $0200;

procedure ShowArrowOfListViewColumn(ListView1: TListView; ColumnIdx: integer; Descending: boolean);
var
  Header: HWND;
  Item: THDItem;
begin
  Header := ListView_GetHeader(ListView1.Handle);
  ZeroMemory(@Item,SizeOf(Item));
  Item.Mask := HDI_FORMAT;
  Header_GetItem(Header,ColumnIdx,Item);
  Item.fmt := Item.fmt and not (HDF_SORTUP or HDF_SORTDOWN);//remove both flags
  if Descending then
    Item.fmt := Item.fmt or HDF_SORTDOWN
  else
    Item.fmt := Item.fmt or HDF_SORTUP;//include the sort ascending flag
  Header_SetItem(Header,Item);
end;

procedure TUD2MainForm.ListView3Compare(Sender: TObject; Item1,Item2: TListItem; Data: Integer; var Compare: Integer);
begin
  if SortedColumn = 0 then
    Compare := CompareText(Item1.Caption,Item2.Caption)
  else
    Compare := CompareText(Item1.SubItems[SortedColumn-1],Item2.SubItems[SortedColumn-1]);
  if Descending then Compare := -Compare;
end;

procedure TUD2MainForm.ListView3ColumnClick(Sender: TObject;
  Column: TListColumn);
begin
  TListView(Sender).SortType := stNone;
  if Column.Index<>SortedColumn then
  begin
    SortedColumn := Column.Index;
    Descending := False;
  end
  else
    Descending := not Descending;
  ShowArrowOfListViewColumn(TListView(Sender),column.Index,Descending);
  TListView(Sender).SortType := stText;
end;

colums可以上下排序,但我看不到箭头.

根据this question,我的函数ShowArrowOfListViewColumn()应该已经解决了这个问题.

Delphi 6是否可能不支持功能,或者我的代码中是否存在问题?另一方面,ListView是IIRC a Windows control,因此我希望WinAPI呈现箭头图形,而不是(非常旧的)VCL.

我在German website读到必须手动添加箭头图形,但该网站的解决方案需要更改Delphi的CommCtrl.pas(因为调整列大小时出现故障).但我真的不喜欢修改VCL源代码,特别是因为我开发OpenSource,我不希望其他开发人员改变/重新编译他们的Delphi Sources.

请注意,我没有向我的二进制文件添加XP清单,因此该应用程序看起来像Win9x.

解决方法

HDF_SORTDOWN和HDF_SORTUP需要comctl32 v6.这在 HDITEM的文档中说明:

HDF_SORTDOWN Version 6.00 and later. Draws a down-arrow on this item. This is typically used to indicate that information in the current window is sorted on this column in descending order. This flag cannot be combined with HDF_IMAGE or HDF_BITMAP.

HDF_SORTUP Version 6.00 and later. Draws an up-arrow on this item. This is typically used to indicate that information in the current window is sorted on this column in ascending order. This flag cannot be combined with HDF_IMAGE or HDF_BITMAP.

正如您在评论中所解释的那样,您没有包含comctl32 v6清单.这解释了你观察到的.

方案包括

>添加comctl32 v6清单,或>自定义绘图标题箭头.

猜你在找的Delphi相关文章