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

标签 delphi winapi delphi-6

我正在使用 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, ColumnIdx, 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;

列可以向上和向下排序,但我看不到箭头。

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

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

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

请注意,我没有将 XP list 添加到我的二进制文件中,因此该应用程序看起来像 Win9x。

最佳答案

HDF_SORTDOWNHDF_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 list 。这解释了你所观察到的情况。

解决方案包括:

  • 添加 comctl32 v6 list ,或
  • 自定义绘图标题箭头。

关于delphi - 使用箭头对 ListView 列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32815474/

相关文章:

c++ - RawInput 鼠标 - 无法移动窗口或使用控件

delphi - 我需要在 Delphi 中完成记录数组吗?

delphi - 在Delphi 6中继续吗?

delphi - 对象和引用有什么区别?

python - 在 Python 中,使用 win32api 没有设置正确的日期

c - CMAKE 资源文件中 WINAPI 的对话框资源返回语法错误

c++ - 如何在不使用真正的剪贴板的情况下进行类似剪贴板的操作?

delphi - 乘法整数溢出 - 编译器错误?

delphi - delphi专家中的第三方依赖