Solved

EM_CHARFROMPOS Access Violation

Posted on 1999-01-25
8
531 Views
Last Modified: 2010-04-06
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
Comment
Question by:JimBob091197
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 1363197
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
 
LVL 5

Expert Comment

by:ronit051397
ID: 1363198
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
 
LVL 10

Expert Comment

by:Lischke
ID: 1363199
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!

 
LVL 5

Author Comment

by:JimBob091197
ID: 1363200
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
 
LVL 10

Accepted Solution

by:
Lischke earned 50 total points
ID: 1363201
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
 
LVL 5

Author Comment

by:JimBob091197
ID: 1363202
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
 
LVL 10

Expert Comment

by:Lischke
ID: 1363203
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
 
LVL 5

Author Comment

by:JimBob091197
ID: 1363204
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question