With enaabled DefaultDrawing
the text will be already rendered if you enter OnDrawCell
.
Since you are calculating the needed rowheight in painting using DT_CALCRECT
of DrawText
you will have to calculated the Rect
wich shall be filled/cleared with FillRect
.
You can use UnionRect
to get the final Rect which has to be filled (FillRect
in the example).
procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.Cells[1,1] := 'Hallo'#13'World';
StringGrid1.Cells[2,2] := 'اهای' +13# + 'جهان';
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
S:String;
drawrect,Fillrect : TRect;
begin
s := (Sender as TStringGrid).Cells[ACol, ARow];
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);
UnionRect(FillRect,Rect,DrawRect);
(Sender as TStringGrid).Canvas.FillRect(FillRect);
DrawText((Sender as TStringGrid).Canvas.handle, Pchar(s), Length(s),
drawrect, DT_WORDBREAK or DT_LEFT);
end;