Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

EM_CHARFROMPOS Access Violation

Posted on 1999-01-25
8
Medium Priority
?
537 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 200 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

722 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