作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要一个固定行作为标题,但文本相当长,所以我想增加行高并在单元格文本中插入 CR/LF。
谷歌搜索显示这是一个解决方案(这是我在谷歌搜索之前想到的第一件事),但它似乎不起作用。有什么想法吗?
Grid.Cells[2,3] := 'This is a sample test' + #13#10 + 'This is the second line';
发生的情况是单元格包含这是一个示例测试这是第二行
如果有什么不同的话,我正在使用 Delphi 7。
[赏金]“我的错。事实上,我在两年前没有检查就给了这个答案,现在发现这个答案不起作用。向任何被误导的人道歉。这是一个经常被问到、经常被错误回答的问题。”
我认为我们希望使用 OnDrawCell,但想象一下我们还必须增加包含单元格的字符串网格行的高度。
我将奖励代码或 FOSS VCL 组件的答案。
[更新]必须与 Delphi XE2 Starter 版本配合使用
最佳答案
TStringGrid
使用 Canvas.TextRect
,它使用 ExtTextOut
,这又不支持多行文本的绘制。
您必须使用 WinAPI 的 DrawText
在 OnDrawCell
事件处理程序中自行绘制此内容。常规。例如,参见this answer关于如何将 DrawText
用于多行文本,以及 this recent answer关于如何在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;
<小时/>
还有其他 StringGrid 组件能够绘制多行文本。例如,this one这是我自己写的(下载来源:NLDStringGrid),结果可能是这样的:
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;
关于delphi - 如何将 CR/LF 放入 TStringgrid 单元格中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4414870/
我是一名优秀的程序员,十分优秀!