如何在列表视图中省略空单元格的网格线?

时间:2011-02-28 17:25:14

标签: delphi listview gridlines

如何在TListView控件中隐藏将空单元格与相邻单元格分隔开的网格线?它就像HTML colspan表属性或Excel的“合并单元格”命令。我希望其中包含文本的单元格保留其正常边框。

Example

2 个答案:

答案 0 :(得分:3)

您可以使用TVirtualStringTree。它有选项toAutoSpanColumns,它将自动跨越空列。

答案 1 :(得分:1)

似乎没有任何可靠的解决方案来解决这个问题。

然而,一个糟糕的解决方案是

procedure TForm4.FormShow(Sender: TObject);
var
  i: integer;
begin
  ListView1.ViewStyle := vsReport;
  ListView1.Columns.Add.Caption := 'Col 1';
  ListView1.Columns.Add.Caption := 'Col 2';
  ListView1.Columns.Add.Caption := 'Col 3';
  ListView1.GridLines := false; // You cannot have grid lines...
  for i := 0 to 10 do
    with ListView1.Items.Add do
    begin
      if i <> 5 then
      begin
        Caption := 'Test';
        SubItems.Add('test');
        SubItems.Add('test');
      end
      else
        Caption := 'This is a very, very long caption';
    end;
end;

var
  ColWidths: array of integer;

procedure TForm4.ListView1AdvancedCustomDraw(Sender: TCustomListView;
  const ARect: TRect; Stage: TCustomDrawStage; var DefaultDraw: Boolean);
var
  i, j: Integer;
begin
  if Stage <> cdPrePaint then Exit;
  if length(ColWidths) <> TListView(Sender).Columns.Count then
  begin
    SetLength(ColWidths, TListView(Sender).Columns.Count);
    Exit;
  end;
  for i := 0 to length(ColWidths) - 1 do
    if ColWidths[i] <> Sender.Column[i].Width then
    begin
      Sender.Invalidate;
      for j := 0 to length(ColWidths) - 1 do
        ColWidths[i] := Sender.Column[i].Width;
    end;
end;

procedure TForm4.ListView1AdvancedCustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage;
  var DefaultDraw: Boolean);
var
  r: TRect;
begin
  DefaultDraw := (Item.SubItems.Count <> 0);
  if not DefaultDraw then
  begin
    FillRect(Sender.Canvas.Handle, Item.DisplayRect(drBounds), GetStockObject(WHITE_BRUSH));
    r := Item.DisplayRect(drBounds);
    DrawText(Sender.Canvas.Handle, Item.Caption, length(Item.Caption), r, DT_SINGLELINE or DT_LEFT or DT_VCENTER)
  end;
end;

Listview colspan is hard to get right. Don't do it. http://privat.rejbrand.se/listviewcolspan.png

这很糟糕,因为它不健壮。它闪烁,它是马车,它是“hacky”。它在未来的Windows版本中可能无法正常工作。基本上,我认为Windows列表视图控件不应该像HTML一样colspan