From 8cee5efb85b77ecb918d14f497afffe2ec462255 Mon Sep 17 00:00:00 2001 From: jesus Date: Thu, 29 Jan 2009 08:04:55 +0000 Subject: [PATCH] LCL, fix stringgrid's autosizecolumn crash, issue #12812 git-svn-id: trunk@18486 - --- lcl/grids.pas | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/lcl/grids.pas b/lcl/grids.pas index b1a9fd74f3..6f836b82bf 100644 --- a/lcl/grids.pas +++ b/lcl/grids.pas @@ -8387,16 +8387,40 @@ procedure TCustomStringGrid.AutoAdjustColumn(aCol: Integer); var i,W: Integer; Ts: TSize; + TmpCanvas: TCanvas; + DC: HDC; begin - if (aCol<0)or(aCol>ColCount-1) then Exit; - W:=0; - For i:=0 to RowCount-1 do begin - Ts:=Canvas.TextExtent(Cells[aCol, i]); - if Ts.Cx>W then W:=Ts.Cx; + if (aCol<0) or (aCol>ColCount-1) then + Exit; + + if not Canvas.HandleAllocated then begin + DC := GetDC(0); + TmpCanvas := TCanvas.Create; + TmpCanvas.Handle := DC; + TmpCanvas.Font.Assign(Font); + end else + TmpCanvas := Canvas; + + try + W:=0; + for i := 0 to RowCount-1 do begin + Ts := TmpCanvas.TextExtent(Cells[aCol, i]); + if Ts.Cx>W then + W := Ts.Cx; + end; + finally + if not Canvas.HandleAllocated then begin + TmpCanvas.Free; + ReleaseDC(0, DC); + end; end; - if W=0 then W:=DefaultColWidth - else W:=W + 8; - ColWidths[aCol]:=W; + + if W=0 then + W := DefaultColWidth + else + W := W + 8; + + ColWidths[aCol] := W; end; procedure TCustomStringGrid.CalcCellExtent(acol, aRow: Integer; var aRect: TRect);