StringGrid1.Cells[1, 1]:='riga1' + LineEnding + 'riga2';
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 DrawSGCell(Sender : TObject; C, R : integer; Rect : TRect;
Style : TFontStyles; Wrap : boolean; Just : TAlignment;
CanEdit : boolean);
{ draws formatted contents in string grid cell at col C, row R;
Style is a set of fsBold, fsItalic, fsUnderline and fsStrikeOut;
Wrap invokes word wrap for the cell's text; Just is taLeftJustify,
taRightJustify or taCenter; if CanEdit false, cell will be given
the background color of fixed cells; call this routine from
grid's DrawCell event }
var
S : string;
DrawRect : TRect;
begin
with (Sender as tStringGrid), Canvas do begin
{ erase earlier contents from default drawing }
if (R >= FixedRows) and (C >= FixedCols) and CanEdit then
Brush.Color:= Color
else
Brush.Color:= FixedColor;
FillRect(Rect);
{ get cell contents }
S:= Cells[C, R];
if length(S) > 0 then begin
case Just of
taLeftJustify : S:= ' ' + S;
taRightJustify : S:= S + ' ';
end;
{ set font style }
Font.Style:= Style;
{ copy of cell rectangle for text sizing }
DrawRect:= Rect;
if Wrap then begin
{ get size of text rectangle in DrawRect, with word wrap }
DrawText(Handle, PChar(S), length(S), DrawRect,
dt_calcrect or dt_wordbreak or dt_center);
if (DrawRect.Bottom - DrawRect.Top) > RowHeights[R] then begin
{ cell word-wraps; increase row height }
RowHeights[R]:= DrawRect.Bottom - DrawRect.Top;
SetGridHeight(Sender as tStringGrid);
end
else begin
{ cell doesn't word-wrap }
DrawRect.Right:= Rect.Right;
FillRect(DrawRect);
case Just of
taLeftJustify : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_wordbreak or dt_left);
taCenter : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_wordbreak or dt_center);
taRightJustify : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_wordbreak or dt_right);
end;
end
end
else
{ no word wrap }
case Just of
taLeftJustify : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_singleline or dt_vcenter or dt_left);
taCenter : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_singleline or dt_vcenter or dt_center);
taRightJustify : DrawText(Handle, PChar(S), length(S), DrawRect,
dt_singleline or dt_vcenter or dt_right);
end;
{ restore no font styles }
Font.Style:= [];
end;
end;
end;
SetGridHeight(Sender as tStringGrid);
{Permette di scrivere una cella su più righe tramite l'utilizzo del LineEnding.
Aggiungere il richiamo della presente "procedure" all'interno della "DrawCell"}
procedure ScriviCellaSuPiuRighe(Sender:TObject; aCol, aRow:Integer; Rect:TRect);
var WrkStr:String;
DrawRect:TRect;
procedure SetGridHeight(const WrkGrid: TStringGrid);
var Counter, NewHeight: Integer;
begin
NewHeight:=0;
with WrkGrid do begin
for Counter:=0 to RowCount - 1 do begin
NewHeight:=NewHeight +
RowHeights[Counter] +
GridLineWidth;
end;
ClientHeight:=NewHeight -
GridLineWidth;
end;
end;
begin
with (Sender as tStringGrid), Canvas do begin
{ erase earlier contents from default drawing }
if (aRow >= FixedRows) and (aCol >= FixedCols) then begin
Brush.Color:= Color
end else begin
Brush.Color:= FixedColor;
end;
FillRect(Rect);
{ get cell contents }
WrkStr:=Cells[aCol, aRow];
if Length(WrkStr) > 0 then begin
{ copy of cell rectangle for text sizing }
DrawRect:=Rect;
if (Pos(LineEnding, WrkStr) > 0) then begin
//Se la riga da emettere contiene "LineEnding", riformatto la cella
{ get size of text rectangle in DrawRect, with word wrap }
DrawText(Handle, PChar(WrkStr), Length(WrkStr), DrawRect, DT_CALCRECT or DT_WORDBREAK or DT_CENTER);
if ((DrawRect.Bottom - DrawRect.Top) > RowHeights[aRow]) then begin
{ cell word-wraps; increase row height }
RowHeights[aRow]:=DrawRect.Bottom - DrawRect.Top;
SetGridHeight(Sender as tStringGrid);
end else begin
{ cell doesn't word-wrap }
DrawRect.Right:=Rect.Right;
FillRect(DrawRect);
DrawText(Handle, PChar(WrkStr), Length(WrkStr), DrawRect, DT_WORDBREAK);
end;
end else begin
//La riga da emettere non contiene "LineEnding": scrivo direttamente
DrawText(Handle, PChar(WrkStr), Length(WrkStr), DrawRect, DT_SINGLELINE or DT_VCENTER);
end;
end;
end;
end;
https://svn.code.sf.net/p/lazarusiug/liug/trunk/demos/TStringGrid-Cella-RigheMultiple
prova con http:
http://svn.code.sf.net/p/lazarusiug/liug/trunk/demos/TStringGrid-Cella-RigheMultiple