2

I want to get the position/placement in pixels of a text (aWord) in a TMemo. My code is something like this:

var
 Size: TSize;
 Pt: Cardinal;
 aWord: string;
 x, y: Integer;
begin
 ...
 Pt := Perform(EM_POSFROMCHAR, aStart-1, 0);
 Windows.GetTextExtentPoint32(DC, PChar(aWord), aLen, Size);
 x:= Smallint(LoWord(Pt));
 y:= Smallint(HiWord(Pt));

Initially it works ok, but when I scroll down the memo, I get a range check error on the last line.

How to I get the position when I scroll down?


The official documentation says:

A returned coordinate can be negative if the character has been scrolled outside the client area of the edit control. The coordinates are truncated to integer values.

I don't know how to interpret this.

Gabriel
  • 20,797
  • 27
  • 159
  • 293
  • 1
    `LoWord` and `HiWord` treat `DWORD` halves as unsigned numbers and return values in the range from 0 to 65535. You get range errors when assigning values greater than 32767 to `Smallint`. You can correct this by assigning `LoWord` to `x` directly, and correct it manually: `if x > 32767 then x = x - 65536;`. – Daniel Sęk Jan 27 '19 at 11:27

1 Answers1

2

Silly. My mind got stuck on "The coordinates are truncated to integer values" - which I still don't know what it means. I was trying to convert those coordinates.

The solution is silly: if we get a negative number, it means that the text is outside the screen. We don't try to extract/convert the Loword and HiWord. We simply exit the procedure.


Also Pt should be "NativeInt".

Gabriel
  • 20,797
  • 27
  • 159
  • 293