在第一次测试中,我(快速和肮脏)从几个来源复制了一些源代码,并做了一些小的调整:
这就是我到目前为止(现在只有快速和肮脏):
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.
解决方法
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清单.这解释了你观察到的.
方案包括: