gpt4 book ai didi

delphi - Stringgrid 自动换行显示文本两次

转载 作者:行者123 更新时间:2023-12-02 04:10:57 26 4
gpt4 key购买 nike

我想以自动换行格式在字符串网格单元格中显示一个长字符串,并找到了以下代码来执行此操作:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
//enable wordwrap in cells
var
S: String;
drawrect :trect;
begin
stringgrid1.Canvas.FillRect (Rect);
S:= (Sender As TStringgrid).Cells [ACol, ARow ];
If Length(S) > 0 Then Begin
drawrect := rect;
DrawText((Sender As TStringgrid).canvas.handle,
Pchar(S), Length(S), drawrect,
dt_calcrect or dt_wordbreak or dt_left );
If (drawrect.bottom - drawrect.top) >
(Sender As TStringgrid).RowHeights[Arow]
Then
(Sender As TStringgrid).RowHeights[Arow] :=
(drawrect.bottom - drawrect.top)
// changing the row height fires the event again!
Else Begin
drawrect.Right := rect.right;

(Sender As TStringgrid).canvas.fillrect( drawrect );
DrawText((Sender As TStringgrid).canvas.handle,
Pchar(S), Length(S), drawrect,
dt_wordbreak or dt_left);
End;
End;
end;

自动换行有效,但单元格同时显示原始文本和换行文本。我假设,由于这是一个 onDrawCell 事件,原始文本已经绘制,我会用第一行代码(stringgrid1.canvas.fillrect(rect))删除它,但这对显示没有影响。我是什么失踪了?

最佳答案

...would erase it with the first line of code (stringgrid1.canvas.fillrect(rect), but this has no effect on the display

这是因为您在调用 FillRect() f 之前没有选择 Brush.Style(也可能是 Brush.Color) .例如。

  stringgrid1.canvas.Brush.Style := bsSolid; // add this line
stringgrid1.canvas.Brush.Color := clWhite; // add this line
stringgrid1.Canvas.FillRect (Rect);

您会注意到单元格左边缘有一些默认绘图的残留物(在下图中不可见,我已经修复了它们)。这是因为 TStringGrid 在内部将单元格绘图偏移了 4 个像素。要更改此设置,您需要在调用 FillRect() 之前将 Rect 参数抵消为 -4,并使用 +4 增加宽度。

经过上述更改后,网格看起来非常平坦和暗淡(在图像中我已经向第二个网格添加了颜色)。要恢复标题列和标题行的某些颜色差异,您需要使用不同的 Brush.Color 处理具有 gdFixed in State 的单元格。对于状态为 gdSelected 的单元格也是如此。

enter image description here

如果取消选中 DefaultDrawing,您还需要执行上述操作。第二个网格和随附的代码演示了这一点。请注意,代码还包括在结束前 gdFocused in State 时对 DrawFocusRect 的调用。

请注意,我将所有这些 Sender as TStringGrid 替换为本地 grid 变量。

procedure TForm4.StringGrid2DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
State: TGridDrawState);
var
grid: TStringGrid;
S: String;
drawrect: TRect;
bgFill: TColor;
begin
grid := Sender as TStringGrid;

if gdFixed in State then
bgFill := $FFF8F8
else
if gdSelected in State then
bgFill := $FFF0D0
else
bgFill := clWhite;

grid.Canvas.Brush.Color := bgFill;
grid.canvas.Brush.Style := bsSolid;
grid.canvas.fillrect(Rect);

S := grid.Cells[ACol, ARow];
if Length(S) > 0 then
begin
drawrect := Rect;
drawrect.Inflate(-4 , 0);
DrawText(grid.canvas.handle, Pchar(S), Length(S), drawrect,
dt_calcrect or dt_wordbreak or dt_left);
If (drawrect.bottom - drawrect.top) > grid.RowHeights[ARow] then
grid.RowHeights[ARow] := (drawrect.bottom - drawrect.top+2)
// changing the row height fires the event again!
else
begin
drawrect.Right := Rect.Right;

// grid.canvas.fillrect(drawrect);
DrawText(grid.canvas.handle, Pchar(S), Length(S), drawrect,
dt_wordbreak or dt_left);
end;
end;

if gdFocused in State then
grid.Canvas.DrawFocusRect(Rect);
end;

第三个网格演示了最简单的解决方案,在我看来也是最好的解决方案,即完全跳过 TStringGrid 并使用 TDrawGrid 代替。您必须将要显示的数据单独保存在网格中的某处。我定义了一个数组:s_arr: array of array of string;

在这种情况下,您可以将 DefaultDrawing 保留为打开状态,因为 TDrawGrid 在默认绘制过程中不会绘制任何文本,内容绘制仅在 OnDrawCell 事件。

procedure TForm4.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
State: TGridDrawState);
var
S: string;
grid: TDrawGrid;
drawrect: TRect;
begin
grid := Sender as TDrawGrid;
S := s_arr[ACol, ARow];
If Length(S) > 0 Then
Begin
drawrect := Rect;
drawrect.Inflate(-4 , 0);
DrawText(grid.canvas.handle, Pchar(S), Length(S), drawrect,
dt_calcrect or dt_wordbreak or dt_left);
If (drawrect.bottom - drawrect.top) > grid.RowHeights[ARow] Then
grid.RowHeights[ARow] := (drawrect.bottom - drawrect.top + 2)
// changing the row height fires the event again!
Else
Begin
drawrect.Right := Rect.Right;

grid.canvas.fillrect(drawrect);
DrawText(grid.canvas.handle, Pchar(S), Length(S), drawrect, dt_wordbreak or dt_left);
End;
End;
end;

保留DefaultDrawing,所有这些与主题相关的功能都由网格本身绘制,我们只在顶部绘制文本。

关于delphi - Stringgrid 自动换行显示文本两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46651224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com