Canvas does not allow drawing

Questions and answers on how to use XLSReadWriteII 3/4/5.
Post Reply
billegge
Posts: 23
Joined: Fri Feb 08, 2008 8:34 pm

Canvas does not allow drawing

Post by billegge »

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?
billegge
Posts: 23
Joined: Fri Feb 08, 2008 8:34 pm

Re: Canvas does not allow drawing

Post by billegge »

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]
Post Reply