Solved

EM_CHARFROMPOS Access Violation

Posted on 1999-01-25
8
521 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
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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now