Thanks taazz!
I have transplanted this into my source, but it seems to be stuck in some sort of loop when run - constantly flickering the dbgrid?
here's my translation:
function WrapText(Canvas: TCanvas; const Text: string; MaxWidth: integer): string;
var
DC: HDC;
TextExtent: TSize;
S, P, E: PChar;
Line: string;
IsFirstLine: boolean;
begin
Result := '';
DC := Canvas.Handle;
IsFirstLine := True;
P := PChar(Text);
while P^ = ' ' do
Inc(P);
while P^ <> #0 do
begin
S := P;
E := nil;
while (P^ <> #0) and (P^ <> #13) and (P^ <> #10) do
begin
LCLIntf.GetTextExtentPoint(DC, S, P - S + 1, TextExtent);
if (TextExtent.CX > MaxWidth) and (E <> nil) then
begin
if (P^ <> ' ') and (P^ <> ^I) then
begin
while (E >= S) do
case E^ of
'.', ',', ';', '?', '!', '-', ':',
')', ']', '}', '>', '/', '\', ' ':
break;
else
Dec(E);
end;
if E < S then
E := P - 1;
end;
Break;
end;
E := P;
Inc(P);
end;
if E <> nil then
begin
while (E >= S) and (P^ = ' ') do
Dec(E);
end;
if E <> nil then
SetString(Line, S, E - S + 1)
else
SetLength(Line, 0);
if (P^ = #13) or (P^ = #10) then
begin
Inc(P);
if (P^ <> (P - 1)^) and ((P^ = #13) or (P^ = #10)) then
Inc(P);
if P^ = #0 then
Line := Line + LineEnding;
end
else if P^ <> ' ' then
P := E + 1;
while P^ = ' ' do
Inc(P);
if IsFirstLine then
begin
Result := Line;
IsFirstLine := False;
end
else
Result := Result + LineEnding + Line;
end;
end;
function QueryTextRect(const vCanvas:TCanvas; const aText:String; const MaxRect: TRect):TRect;
const
DrawTextFlags = DT_NOPREFIX or DT_EDITCONTROL or DT_CALCRECT;
begin
if (aText <> '') then
begin
//Canvas.Font := aFont;
Result := MaxRect;
LCLIntf.DrawText(vCanvas.Handle, PChar(aText), Length(aText), //calculate the text's rectangle required for painting.
Result, DrawTextFlags);
end
else
FillChar(Result, SizeOf(Result), 0);
end;
procedure TForm1.DBGrid2ColumnSized(Sender: TObject);
var
MaxRect : TRect;
TextRect : TRect;
bkm : TBookmark;
Fs : longword = 0;
examine : String;
x, i : Integer;
begin
bkm := SQLQuery4.GetBookmark;
SQLQuery4.First;
repeat
MaxRect := classes.Rect(0, DBGrid2.Columns[3].Width,0,4096);
examine := SQLQuery4.fields[3].AsString;
examine := WrapText(DBGrid2.Canvas,examine,dbgrid2.Columns[3].Width);
TextRect := querytextrect(dbgrid2.canvas, examine, MaxRect);
x := (TextRect.Bottom - TextRect.Top) + 3;
if fs < x then fs := x;
SQLQuery4.Next;
until SQLQuery4.EOF;
SQLQuery4.GotoBookmark(bkm);
SQLQuery4.FreeBookmark(bkm);
for i := 1 to tstringgrid(dbgrid2).rowcount do begin
tstringgrid(dbgrid2).RowHeights[i-1] := x;
end;
end;
procedure TForm1.DBGrid2DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
textstyle: ttextstyle;
i,x: integer;
examine: string;
TextRect, MaxRect: Trect;
begin
DBGrid2.Canvas.FillRect(Rect);
TextStyle := Canvas.TextStyle;
TextStyle.Wordbreak := True;
TextStyle.SingleLine := false;
examine := WrapText(DBGrid2.Canvas, DBGrid2.DataSource.DataSet.Fields[DataCol].AsString, (Rect.Right - Rect.Left) - 2);
if DataCol = 3 then begin
MaxRect := classes.Rect(rect.Left,rect.right,0,4096);
TextRect := querytextrect(dbgrid2.canvas, examine, MaxRect);
x := (TextRect.Bottom - TextRect.Top) + 3;
for i := 1 to tstringgrid(dbgrid2).rowcount do begin
TStringGrid(dbgrid2).RowHeights[i-1] := x;
end;
end;
DBGrid2.Canvas.TextRect(Rect, Rect.Left+1, Rect.Top+1, examine, textstyle);
end;
I can't see how I have gone wrong!? Being the author of the code is there something obvious I have missed?
with some texts it appears to flicker straight away but with others it only flickers when you scroll horizontally or vertically?