如何防止 ListView header 遮挡第一组的顶部?

How do I prevent a ListView header from obscuring the top of the first group?

我使用 Delphi XE,我想让标准的 ListView 表现得像我想要的那样。 我希望列表视图是 vsReport,这样我就可以为我的项目设置群组。 在设计时,我创建了列(一个名为 Topic 的列)、两个组和每个组的一些项目。

在设计时,ListView 看起来不错,但在运行时,我的第一个组以某种方式部分隐藏在列标题下。以下是图片:

在设计时:

运行时:

这是我的 DFM

object Form2: TForm2
  Left = 326
  Top = 150
  Caption = 'Form2'
  ClientHeight = 636
  ClientWidth = 1289
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ListView1: TListView
    Left = 0
    Top = 0
    Width = 205
    Height = 636
    Align = alLeft
    Columns = <
      item
        Caption = 'Topic'
        Width = 200
      end>
    ColumnClick = False
    DoubleBuffered = True
    FullDrag = True
    Groups = <
      item
        Header = 'First group'
        GroupID = 0
        State = []
        HeaderAlign = taLeftJustify
        FooterAlign = taLeftJustify
        Subtitle = 'Options bellow'
        TitleImage = 1
      end
      item
        Header = 'Settings'
        GroupID = 1
        State = [lgsNormal]
        HeaderAlign = taLeftJustify
        FooterAlign = taLeftJustify
        Subtitle = 'Other options here'
        TitleImage = 0
      end>
    HideSelection = False
    HotTrack = True
    HotTrackStyles = [htUnderlineCold, htUnderlineHot]
    Items.ItemData = {
      059E000000030000000000000000000000FFFFFFFF0000000000000000000000
      000A4600690072007300740020006900740065006D000100000001000000FFFF
      FFFF0000000000000000000000000B5300650063006F006E0064002000690074
      0065006D000200000002000000FFFFFFFF000000000100000000000000134600
      69007300720074002000730065007400740069006E0067007300200069007400
      65006D00}
    GroupView = True
    RowSelect = True
    ParentDoubleBuffered = False
    ShowWorkAreas = True
    TabOrder = 0
    ViewStyle = vsReport
    OnClick = ListView1Click
    ExplicitTop = 8
    ExplicitHeight = 497
  end
end

如何防止这种情况发生?

这种行为甚至在 Delphi 10 西雅图也存在。我不确定是什么原因导致的,但您可以通过对列表视图属性进行重大更改,然后恢复该更改来解决它。这似乎足以让列表视图赶上进度。例如,这就足够了:

procedure TForm1.FormCreate(Sender: TObject);
begin
  ListView1.ViewStyle := vsIcon;
  ListView1.ViewStyle := vsReport;
end;

但这有点过头了。深入了解这段代码有什么影响,它的关键是重新创建 window。可以这样做:

type
  TProtectedHackListView = class(TListView);

procedure TForm1.FormCreate(Sender: TObject);
begin
  TProtectedHackListView(ListView1).RecreateWnd;
end;

或者甚至重新创建表单,这反过来将重新创建子项:

procedure TForm1.FormCreate(Sender: TObject);
begin
  RecreateWnd;
end;