LCL, fix stringgrid's autosizecolumn crash, issue #12812

git-svn-id: trunk@18486 -
This commit is contained in:
jesus 2009-01-29 08:04:55 +00:00
parent f858cdf155
commit 8cee5efb85

View File

@ -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);