Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2017-05

Measuring text width in a TEdit

4 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Torsten Bonde Christiansen

Hi List.

I want to know the placement of the caret (in pixels, not character count) in a TEdit. However i am struggeling to fint a method that will allow me to do so.

If i try to calculate it using the assigned font, I have the problem that TEdit.Font.GetTextWidth always returns the default size (16), since the Font doesn't have a canvas assigned to it.

The TEdit itself does not really have any methods for calculation the caret position in pixels either.

All suggestions are welcome. :)

Kind regards,
Torsten Bonde Christiansen.

Reply by email

Vojtěch Čihák

Hi,
 
maybe you can create some temporary canvas, assign TEdit.Font to TCanvas.Font, do the measurement and free that canvas.  
V.

Reply by email

Martin Frb
to Torsten Bonde Christiansen: Hi List. I want to know the placement of the caret (in pixels, not character count) in a…
Torsten Bonde Christiansen wrote 9 lines
Hi List. I want to know the placement of the caret (in pixels, not character count) in a TEdit. However i am struggeling to fint a method that will allow me to do so. If i try to calculate it using the assigned font, I have the problem that TEdit.Font.GetTextWidth always returns the default size (16), since the Font doesn't have a canvas assigned to it.

If you are on windows (and win only), you may be able to use GetCaretPos https://msdn.microsoft.com/en-us/library/windows/desktop/ms648402(v=vs.85).aspx (That is, if the edit is focused, and has the caret)

On Windows there is only one caret per App, so if the TEdit has it, then
GetCaretPos should get the pos for that Edit.

Of course win only, and the Edit needs the focus. (And not tested at all)

Reply by email

zeljko
to Torsten Bonde Christiansen: Hi List. I want to know the placement of the caret (in pixels, not character count) in a…
Torsten Bonde Christiansen wrote 14 lines
Hi List. I want to know the placement of the caret (in pixels, not character count) in a TEdit. However i am struggeling to fint a method that will allow me to do so. If i try to calculate it using the assigned font, I have the problem that TEdit.Font.GetTextWidth always returns the default size (16), since the Font doesn't have a canvas assigned to it. The TEdit itself does not really have any methods for calculation the caret position in pixels either. All suggestions are welcome. :)

I'm using TBitmap which assigns TEdit.Font for such cases.Measurement is correct.

zeljko

Reply by email