Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 541
  • Last Modified:

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
0
JimBob091197
Asked:
JimBob091197
1 Solution
 
intheCommented:
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

0
 
ronit051397Commented:
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)));
0
 
LischkeCommented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JimBob091197Author Commented:
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

0
 
LischkeCommented:
FYI: There's a freeware control called RichEdit98 which is, although slightly buggy, very useful and can display unicode characters even under Win9x. You can download it from the major Delphi sites (Torry's, DSP etc.).

JimBob, if you need more help with the richedit then tell me. I've collected some experiences the last weeks just in this field.


Ciao, Mike
0
 
JimBob091197Author Commented:
Thanks Mike.

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

Regards,
Dave
0
 
LischkeCommented:
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
0
 
JimBob091197Author Commented:
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

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now