Link to home
Start Free TrialLog in
Avatar of JimBob091197
JimBob091197

asked on

EM_CHARFROMPOS Access Violation

Hi

I'm having trouble getting the character index in a Rich Edit control at the mouse pos.

Here's my code (x & y are client mouse co-ords relative to the client):
var
  LP, RchPos: DWORD;
begin
  LP := MakeLParam(x, y);
  RchPos := LOWORD(rchTest.Perform(EM_CHARFROMPOS, 0, LP));
end;

It gives the following error: "Access Violation at address XXXXX in module 'RichEd32.dll'."

Any solutions would be much appreciated!

Regards,
JB
Avatar of inthe
inthe

hi jimbob
Is this a help had it in my codestore:

procedure TForm1.REditMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
    CurPos: Integer;
    ptrToCurPos : ^Integer;
begin
    if Button = mbRight then begin
         CurPos := (MAKELPARAM (X, Y));     //MAKELPARAM is a macro
that converts the X,Y-coordinates to a Longint
         ptrToCurPos := @CurPos;
         SendMessage(REdit.Handle, EM_CHARFROMPOS, 0,
Integer(ptrToCurPos));
    end;
end;

i need some sleep :-)
Regards Barry zzzzz

Try this:
      to get the character index by coordinates, call:
      Lo(Memo1.perform(EM_CHARFROMPOS,0, makelparam(X, Y)));

      to get the line index by coordinates, call:
      Hi(Memo1.perform(EM_CHARFROMPOS,0, makelparam(X, Y)));
Hi JimBob,

interesting answers from experts were given ;-)))

The problem is a wrong documentation of the EM_CHARFROMPOS/EM_POSFROMCHAR messages.

For EM_CHARFROMPOS: The lParam is not the position but a pointer (casted to a longint for Perform()) to a TPoint structure, which actually contains the position of the character to be retrieved. Since the MakeLParam seldom constructs a valid memory address, the result is an av.

Read more about it at the (very recommendable) Delphi Bug List site: http://www.dataweb.nl/~r.p.sterkenburg/generated/vcl-Win32.htm#77.

Ciao, Mike
Avatar of JimBob091197

ASKER

Hi all

Barry: Thanks for your reply, but using MakeLParam instead of TPoint (see Mike's comment above & my comment below) will get rid of the Access Violation, but it always returns the max number of chars, NOT the char at X:Y.

Ronit: Your comment is suspiciously similar to my question...  Have you tested this?

Mike: You provided the crucial part to the problem: error in help file.  Thanks.  You may answer the question.

For everybody's info, the solution (expanded from Mike's comment) is as follows:
var
  Pt: TPoint;
  ChIx: Integer;
begin
  Pt := Point(x, y);
  ChIx := LOWORD(MyRichEd.Perform(EM_CHARFROMPOS, 0, Integer(@Pt)));
end;

JB

ASKER CERTIFIED SOLUTION
Avatar of Lischke
Lischke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Mike.

You mention you have some experience with RichEdits.  ;-)  I have another question: https://www.experts-exchange.com/topics/comp/lang/delphi/Q.10121097

Regards,
Dave
I already saw it this mornig, but have only the same solution to offer as the other experts (save SelStart, query and restore etc.). It's quite unsual what you want (but don't we do that all the time :-)?).

Ciao, Mike
Yeah, it is unusual.  The reason saving & restoring SelStart isn't such a good idea (in my case) is I'm trying to establish the text style (the other question) under the mouse cursor (this question).  Saving & restoring SelStart results in too much flicker as the mouse moves over the control, even if I use LockWindowUpdate, WM_SETREDRAW, etc.

Dave