I want to use function lclintf.DrawText() and have a problem with it's 1st parameter, which is a handle to a "device context". When I write:
var h: HDC; {=THandle}
...
h:=Form1.Canvas.Handle;
then it works. But I want to use DrawText() in a common unit, independently from a Form (only to compute the neccessary size of a text). Is this possible?
What I tried:
var CV: TCanvas;
h: HDC; {=THandle}
...
CV:=TCanvas.Create;
CV.Font.Name:='Courier New';
CV.Font.Height:=15;
h:=CV.Handle; {=> Exception "Canvas does not allow drawing"}
...
but this raises an Exception (see above). I tried also:
var LB: TLabel;
h: HDC; {=THandle}
...
LB:=TLabel.Create(nil);
LB.Canvas.Font.Name:='Courier New';
LB.Canvas.Font.Height:=15;
h:=LB.Canvas.Handle; {=> Access Violation}
...
which raises an Access Violation (see above).
What am I doing wrong?
I "want" the handle only to pass a specific font and font-size, nothing more needed.
I use Lazarus 2.0.10 with FPC 3.2.0 on Windows 7 and Linux Ubuntu 18.04. Thanks in advance.