加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何将CR / LF放入TStringgrid单元格?

发布时间:2020-12-15 04:06:41 所属栏目:大数据 来源:网络整理
导读:我希望有一个固定的行作为标题,但文本相当长,所以我想增加行高并将CR / LF插入单元格文本. 谷歌搜索显示这是一个解决方案(这是我在谷歌搜索之前的第一个想法),但它没有看到工作.有任何想法吗? Grid.Cells[2,3] := 'This is a sample test' + #13#10 + 'This
我希望有一个固定的行作为标题,但文本相当长,所以我想增加行高并将CR / LF插入单元格文本.

谷歌搜索显示这是一个解决方案(这是我在谷歌搜索之前的第一个想法),但它没有看到工作.有任何想法吗?

Grid.Cells[2,3] := 'This is a sample test' + #13#10 + 'This is the second line';

会发生什么是单元格包含这是一个示例测试这是第二行

(德尔福7如果有任何区别)

[Bounty]“我很糟糕.两年前我实际上已经给了这个答案而没有检查,现在发现答案没有用.对被误导的人说话.这是一个FABOWAQ(经常被问到,经常被错误回答的问题).GINYF ”.

我假设我们正在寻找使用OnDrawCell,但想象我们还必须增加包含单元格的字符串网格行的高度.

我将为代码或FOSS VCL组件授予答案.

[更新]必须与D??elphi XE2 Starter版一起使用

解决方法

TStringGrid使用Canvas.TextRect,它使用 ExtTextOut,而后者又不支持绘制多行文本.

您必须使用WinAPI的DrawText例程在OnDrawCell事件处理程序中自行绘制.有关如何将DrawText用于多行文本,请参阅this answer;有关如何在OnDrawCell中实现自定义绘图,请参阅this recent answer:

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol,ARow: Integer;
      Rect: TRect; State: TGridDrawState);
  private
    procedure FillWithRandomText(AGrid: TStringGrid);
    procedure UpdateRowHeights(AGrid: TStringGrid);
  end;

procedure TForm1.FillWithRandomText(AGrid: TStringGrid);
const
  S = 'This is a sample'#13#10'text that contains'#13#10'multiple lines.';
var
  X: Integer;
  Y: Integer;
begin
  for X := AGrid.FixedCols to AGrid.ColCount - 1 do
    for Y := AGrid.FixedRows to AGrid.RowCount - 1 do
      AGrid.Cells[X,Y] := Copy(S,1,8 + Random(Length(S) - 8));
  UpdateRowHeights(AGrid);
end;

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

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol,ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  with TStringGrid(Sender) do
    if Pos(#13#10,Cells[ACol,ARow]) > 0 then
    begin
      Canvas.FillRect(Rect);
      Inc(Rect.Left,2);
      Inc(Rect.Top,2);
      DrawText(Canvas.Handle,PChar(Cells[ACol,ARow]),-1,Rect,DT_NOPREFIX or DT_WORDBREAK);
    end;
end;

procedure TForm1.UpdateRowHeights(AGrid: TStringGrid);
var
  Y: Integer;
  MaxHeight: Integer;
  X: Integer;
  R: TRect;
  TxtHeight: Integer;
begin
  for Y := AGrid.FixedRows to AGrid.RowCount - 1 do
  begin
    MaxHeight := AGrid.DefaultRowHeight - 4;
    for X := AGrid.FixedCols to AGrid.ColCount - 1 do
    begin
      R := Rect(0,AGrid.ColWidths[X] - 4,0);
      TxtHeight := DrawText(AGrid.Canvas.Handle,PChar(AGrid.Cells[X,Y]),R,DT_WORDBREAK or DT_CALCRECT);
      if TxtHeight > MaxHeight then
        MaxHeight := TxtHeight;
    end;
    AGrid.RowHeights[Y] := MaxHeight + 4;
  end;
end;

还有其他能够绘制多行文本的StringGrid组件.例如,我自己编写的this one(下载源:NLDStringGrid NLDSparseList)可能有以下结果:

var
  R: TRect;
begin
  NLDStringGrid1.Columns.Add;
  NLDStringGrid1.Columns.Add;
  NLDStringGrid1.Cells[1,1] := 'Sample test'#13#10'Second line';
  NLDStringGrid1.Columns[1].MultiLine := True;
  NLDStringGrid1.AutoRowHeights := True;
  SetRect(R,2,3,3);
  NLDStringGrid1.MergeCells(TGridRect(R),True,True);
  NLDStringGrid1.ColWidths[2] := 40;
  NLDStringGrid1.Cells[2,2] := 'Sample test'#13#10'Second line';
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读