I am getting "Canvas does not allow drawing" on the following line:
Line:
[code]
Result := Round((FWidth / 256) * Canvas.TextWidth('8'));
[/code]
Source:
[code]
function TXLSColumn.GetPixelWidth: integer;
var
Canvas: TCanvas;
F: TFont;
begin
Canvas := TCanvas.Create;
Canvas.Handle := GetDC(0);
try
F := TFont.Create;
try
FFormats.Fonts[0].CopyToTFont(F);
Canvas.Font.Assign(F);
Result := Round((FWidth / 256) * Canvas.TextWidth('8'));
finally
F.Free;
end;
finally
Canvas.Free;
end;
end;
[/code]
Do you know what is causing this and how to fix it?
Maybe ReleaseDC should be called?
Canvas does not allow drawing
Re: Canvas does not allow drawing
I tested and found that the below test code creates the Canvas does not allow drawing error. Adding a ReleaseDC call fixes it.
[code]
procedure TForm10.Button1Click(Sender: TObject);
procedure DoIt;
var
Canvas: TCanvas;
F: TFont;
begin
Canvas := TCanvas.Create;
Canvas.Handle := GetDC(0);
try
F := TFont.Create;
try
Canvas.Font.Assign(F);
Canvas.TextWidth('8');
finally
F.Free;
end;
finally
Canvas.Free;
end;
end;
var
I: Integer;
begin
for I:= 0 to 100000 do
DoIt;
end;
[/code]
[code]
procedure TForm10.Button1Click(Sender: TObject);
procedure DoIt;
var
Canvas: TCanvas;
F: TFont;
begin
Canvas := TCanvas.Create;
Canvas.Handle := GetDC(0);
try
F := TFont.Create;
try
Canvas.Font.Assign(F);
Canvas.TextWidth('8');
finally
F.Free;
end;
finally
Canvas.Free;
end;
end;
var
I: Integer;
begin
for I:= 0 to 100000 do
DoIt;
end;
[/code]