Recent

Author Topic: Call TCanvas.TextExtent() when component Canvas handle is not allocated  (Read 902 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2622
    • UVviewsoft
In ATSynEdit component, I need to get TextExtent for a string. But even when TATSynEdit.Canvas handle is not yet allocated (HandleAllocated=False yet). I can use any other ready TCanvas, but how to get it?

- Maybe I can create temporary TCanvas and set its Handle to GetDC(0)? This is current approach and I have mem leaks with it (even with call ReleaseDC() after GetDC() ).
- Or maybe I can use some global Canvas, e.g. Application.MainForm.Canvas? Is it always ready to use?

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
You can create temporary TBitmap, I saw it somewhere in Lazarus, IIRC.
Code: Pascal  [Select][+][-]
  1. var BMP: TBitmap;
  2. begin
  3.   BMP:=TBitmap.Create;
  4.   //do the job with BMP.Canvas
  5.   BMP.Free;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Or maybe...
Code: Pascal  [Select][+][-]
  1. uses Types;
  2.  
  3. function GetTextExtend(const AString: string; const AFont: TFont): TSize;
  4. var
  5.   LCanvas: TCanvas;
  6. begin
  7.   LCanvas := TCanvas.Create;
  8.   try
  9.     LCanvas.Font := AFont;
  10.     Result := LCanvas.TextExtent(AString);
  11.   finally
  12.     LCanvas.Free;
  13.   end;
  14. end;
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
@KodeZwerg: This does not work because TCanvas has no handle (at least on Qt).
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

jamie

  • Hero Member
  • *****
  • Posts: 7310
TControlcanvas.

Create it, and then set a control to it.
The only true wisdom is knowing you know nothing

AlexTP

  • Hero Member
  • *****
  • Posts: 2622
    • UVviewsoft
I tried first with the Bitmap, seems working. I only fear that macOS HiDPI canvas will be not the same as Bitmap's canvas, so I may get not OK TextExtent. I will test it.

lagprogramming

  • Sr. Member
  • ****
  • Posts: 407
In ATSynEdit component, I need to get TextExtent for a string. But even when TATSynEdit.Canvas handle is not yet allocated (HandleAllocated=False yet). I can use any other ready TCanvas, but how to get it?

- Maybe I can create temporary TCanvas and set its Handle to GetDC(0)? This is current approach and I have mem leaks with it (even with call ReleaseDC() after GetDC() ).
- Or maybe I can use some global Canvas, e.g. Application.MainForm.Canvas? Is it always ready to use?

You've mentioned "TextExtent", "GetDC" and "ReleaseDC", which reminded me of the forum subject at
Lazarus » Forum » Miscellaneous » Suggestions » LCL » lcl/include/canvas.inc function TCanvas.TextExtent ARect variable
https://forum.lazarus.freepascal.org/index.php/topic,62633.0.html

The last message in that topic might be of interest to you because it looks like there is a bug in LCL. TCanvas.TextExtent doesn't SaveDC and RestoreDC properly:
https://forum.lazarus.freepascal.org/index.php/topic,62633.msg474095.html#msg474095

 

TinyPortal © 2005-2018