本文介绍了如何将CR / LF放入TStringgrid单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想要有一个固定的行作为标题,但文本相当长,所以我想增加行的高度,并将CR / LF插入单元格文本。 Googling将此显示为解决方案(这是我在谷歌搜索之前首先想到的),但它看不到工作。任何想法? Grid.Cells [2,3]:='这是一个示例测试'+#13#10 + 这是第二行; 发生什么是单元格包含这是一个示例testThis是第二个 (Delphi 7如果有任何差异) [赏金] 我的坏,我实际上在两年前给了这个答案,没有检查,现在发现答案不起作用,对任何被误导的人来说,这是一个FABOWAQ(经常被问到,经常被错误地回答的问题)GINYF / p> 我假设我们正在寻求使用OnDrawCell,但是想像我们还必须增加包含单元格的字符串网格行的高度。 我将为代码或FOSS VCL组件颁发答案。 [更新]必须与Delphi XE2 Starter Edition 解决方案 TStringGrid 使用 Canvas.TextRect ,它使用(下载来源: NLDStringGrid + NLDSparseList ),可能这个结果: var R:TRect; begin NLDStringGrid1.Columns.Add; NLDStringGrid1.Columns.Add; NLDStringGrid1.Cells [1,1]:='Sample test'#13#10'Secode line'; NLDStringGrid1.Columns [1] .MultiLine:= True; NLDStringGrid1.AutoRowHeights:= True; SetRect(R,2,2,3,3); NLDStringGrid1.MergeCells(TGridRect(R),True,True); NLDStringGrid1.ColWidths [2]:= 40; NLDStringGrid1.Cells [2,2]:='Sample test'#13#10'Secode line'; 结束 I want to have one fixed row as a header, but the texts are rather long, so I'd like to increase the row height and insert CR/LF into the cell text.Googling shows this as a solution (and it's the first thing I thought fo before googling), but it down't see to work. Any ideas?Grid.Cells[2,3] := 'This is a sample test' + #13#10 + 'This is the second line';What happens is that the cell contains This is a sample testThis is the second line(Delphi 7 if it makes any difference)[Bounty] "My bad. I actually awarded this an answer two years ago without checking and now find that the answer did not work. Aplogies to anyone who was misled. This is a FABOWAQ (frequently asked, often wrongly answered question). GINYF".I presume that we are looking to use OnDrawCell, but imagine that we would also have to increase the height of the string grid row which contains the cell.I will award the answer for either code or a FOSS VCL component.[Update] must work with Delphi XE2 Starter edition 解决方案 TStringGrid uses Canvas.TextRect, which uses ExtTextOut, which in turn does not support drawing of multiline text.You have to draw this yourself in an OnDrawCell event handler with WinAPI's DrawText routine. See for example this answer on how to use DrawText for multiline text, and this recent answer on how to implement custom drawing in OnDrawCell: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, 0, AGrid.ColWidths[X] - 4, 0); TxtHeight := DrawText(AGrid.Canvas.Handle, PChar(AGrid.Cells[X, Y]), -1, R, DT_WORDBREAK or DT_CALCRECT); if TxtHeight > MaxHeight then MaxHeight := TxtHeight; end; AGrid.RowHeights[Y] := MaxHeight + 4; end;end;There are also other StringGrid components able of drawing multiline text. For instance, this one which I wrote myself (download sources: NLDStringGrid + NLDSparseList) with possibly this result: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, 2, 3, 3); NLDStringGrid1.MergeCells(TGridRect(R), True, True); NLDStringGrid1.ColWidths[2] := 40; NLDStringGrid1.Cells[2, 2] := 'Sample test'#13#10'Second line';end; 这篇关于如何将CR / LF放入TStringgrid单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-06 03:18