[SOLVED] Problem with the handle when using function lclintf.DrawText lazarus / 31-LCL ---------------------------------------------------------------------- Hartmut · 2022-11-18 18:19 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: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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. ---------------------------------------------------------------------- jamie · 2022-11-18 21:42 TLabel is not a windows type control, its a graphic control You would use GetDC(0) maybe but the issue here is it uses the font size selected in the context. So using GetDC(0) is going to use the system's wide font.  Or I suppose you could use a TPanel which has a handle.  BUt you still need to set Font size etc. ---------------------------------------------------------------------- KodeZwerg · 2022-11-18 22:39 For Windows I would do it like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  Windows... function GetTextDimension(const AForm: TForm; const AString: string): TRect;var  r: TRect;begin  r := Rect(5, 5, 5, 5);  DrawText(AForm.Canvas.Handle, PChar(AString), Length(AString), r, DT_LEFT or DT_CALCRECT);  Result := r;end; and call like that Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  r: TRect;begin  r := GetTextDimension(Self, s);  Memo1.Lines.Add(Format('%d x %d', [r.Height, r.Width]));end; I hope it helps. ---------------------------------------------------------------------- jamie · 2022-11-18 23:14 The problem with that is it still requires a distant handle outside the unit.  I suppose a function could be written up so that it requires you to pass a handle to it of a known canvas when making the call. Remember, the poster stated that it lives in a unit away from all others so there is no form in there.  After thinking about this, it may actually be better if a function did require a handle from some remote point within the calling parameters.  At least it is still isolated in a unit that way. ---------------------------------------------------------------------- wp · 2022-11-18 23:46 Quote from: Hartmut on November 18, 2022, 07:19:54 pmBut 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? I would create a temporary bitmap and use its canvas: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function MeasureText(AText: String; AFont: TFont; MaxWidth: Integer): TSize;var  bmp: TBitmap;  R: TRect;begin  bmp := TBitmap.Create;  try    bmp.SetSize(1, 1);    bmp.Canvas.Font.Assign(AFont);    R := Rect(0, 0, MaxWidth, 0);    DrawText(bmp.Canvas.Handle, PChar(AText), Length(AText), R, DT_CALCRECT or DT_WORDBREAK);    Result := TSize(R.BottomRight);  finally    bmp.Free;  end;end; ---------------------------------------------------------------------- Hartmut · 2022-11-19 16:17 Thanks a lot to all for your answers, especially to wp for your demo. It works perfectly with a Bitmap as handle source, so I implemented this. Maybe it should be mentioned, that function DrawText makes one more difference in the LCL between Windows and Linux: if 'MaxWidth' is smaller than the longest word, then on Windows the result-width is increased to the neccessary width for this word (so it is not truncated), while on Linux this is not done (this word is truncated "in the middle" of the word).