在 Delphi 11 Alexandria 的 Windows 10 中的 32 位 VCL 应用程序中,我有TListView
两列,客户端与表单对齐。我正在使用此代码来处理 ListView Header 中的排序箭头:
procedure TformMain.ListView1ColumnClick(Sender: TObject; Column: TListColumn);
begin
FSortedColumn := Column.Index;
case FSortedColumn of
0: FColumn0SortedUp := not FColumn0SortedUp;
1: FColumn1SortedUp := not FColumn1SortedUp;
end;
SetListHeaderSortArrow(FSortedColumn);
end;
procedure TformMain.SetListHeaderSortArrow(const aColumnIndex: Integer);
begin
var Header: HWND;
var Item: Winapi.CommCtrl.THDItem;
case aColumnIndex of
0:
begin
Header := Winapi.CommCtrl.ListView_GetHeader(ListView1.Handle);
Winapi.Windows.ZeroMemory(@Item, SizeOf(Item));
Item.Mask := Winapi.CommCtrl.HDI_FORMAT;
Winapi.CommCtrl.Header_GetItem(Header, 0, Item);
Item.fmt := Item.fmt and not (HDF_SORTUP or HDF_SORTDOWN); // remove both flags
if FColumn0SortedUp then
Item.fmt := Item.fmt or HDF_SORTUP // include the sort ascending flag
else
Item.fmt := Item.fmt or HDF_SORTDOWN; // include the sort descending flag
Header_SetItem(Header, 0, Item);
end;
1:
begin
Header := Winapi.CommCtrl.ListView_GetHeader(ListView1.Handle);
Winapi.Windows.ZeroMemory(@Item, SizeOf(Item));
Item.Mask := Winapi.CommCtrl.HDI_FORMAT;
Winapi.CommCtrl.Header_GetItem(Header, 1, Item);
Item.fmt := Item.fmt and not (HDF_SORTUP or HDF_SORTDOWN); // remove both flags
if FColumn1SortedUp then
Item.fmt := Item.fmt or HDF_SORTUP // include the sort ascending flag
else
Item.fmt := Item.fmt or HDF_SORTDOWN; // include the sort descending flag
Header_SetItem(Header, 1, Item);
end;
end;
end;
procedure TformMain.ListView1Resize(Sender: TObject);
begin
// This gets inexplicably automatically executed 3 times at program start!!
// This must be in OnResize, otherwise the sort-arrows get hidden when resizing the ListView:
SetListHeaderSortArrow(FSortedColumn);
end;
当我点击 SECOND 列的列标题时,出现了第二列的排序箭头,但第一列的排序箭头并没有消失!只有当我调整 ListView 的大小(通过调整表单的大小)时,第一列上的排序箭头才会消失。那么如何在单击第二列标题时使第一列上的排序箭头立即消失?