Solved

Problem calling ::SendMessage on a CRichEditCtrl

Posted on 2000-04-26
11
344 Views
Last Modified: 2013-11-20
I am trying to find out the location of the pointer within a CRichEditCtrl without clicking within the control.  I need to know the character index it is on, not just the coordinates of the pointer.  To do this I'm calling
int charIndex = ::SendMessage(theCtrl, EM_CHARFROMPOS, 0, MAKELPARAM(pt.x, pt.y));

from within the view's OnMouseMove() function, to convert the mouse position to a character index.  theCtrl is the richeditctrl instance, and pt is a CPoint object that is based on the point passed into OnMouseMove, but might be a few pixels shifted (for a reason, but I won't bother explaining why unless someone needs to know.)

I get an access violation when I hit this code.  Not every time, most often when I am in a part of the richeditctrl that I had to scroll down to get to, but also in places at the top if I go over them a few times.  It will work a few times then give the access violation eventually.

Any ideas?  Other ways to get the character index I'm pointing at without using this code?
0
Comment
Question by:appleby
11 Comments
 
LVL 3

Expert Comment

by:GlennDean
ID: 2753282
You need to pass a handle to SendMessage.  If I understood you right, instead of passing theCtrl pass
theCtrl.m_hWnd to SendMessage.
0
 
LVL 3

Expert Comment

by:mnewton022700
ID: 2753761
According to MSDN the lparam parameter should be a pointer to a POINTL structure. Try this:

POINTL pointl;
pointl.x = pt.x;
pointl.y = pt.y;

int charIndex = ::SendMessage(theCtrl, EM_CHARFROMPOS, 0, &pointl);
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2754011
int RichEditCharFromPos(CRichEditCtrl& theCtrl, const CPoint& pt) {
  ASSERT(::IsWindow(theCtrl.m_hWnd));
  return (int)::SendMessage(theCtrl.m_hWnd, EM_CHARFROMPOS, 0, MAKELPARAM(pt.x, pt.y));
}

This should work fine

0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 200 total points
ID: 2754020
Indeed .. tanks mnewton.

Richedit is indeed different from Edit control in this respect (why did MS do this?????).

So lets make it:

int RichEditCharFromPos(CRichEditCtrl& theCtrl, const CPoint& pt) {
  ASSERT(::IsWindow(theCtrl.m_hWnd));
  POINTL pointl;
  pointl.x = pt.x;
  pointl.y = pt.y;
  return (int)::SendMessage(theCtrl.m_hWnd, EM_CHARFROMPOS, 0, &pointl);
}

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2754026
Interestingly, the latest WTL from MS doesn't seem to know about this ... it uses this code for rich edit controls:

  int CharFromPos(POINT pt) const
  {
    ATLASSERT(::IsWindow(m_hWnd));
    return (int)::SendMessage(m_hWnd, EM_CHARFROMPOS, 0, MAKELPARAM(pt.x, pt.y));
  }

Either MS is wrong, or MS is wrong.  How are we mere mortals supposed to know what is right if MS gets it wrong themselves??? :-(
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:NickRepin
ID: 2754514
<<lParam
Specifies the coordinates of a point in the control's client area. The coordinates are in screen units and are relative to the upper-left corner of the control's client area.

Rich edit controls: This is a pointer to a POINTL structure that contains the horizontal and vertical coordinates.

Edit controls: The low-order word contains the horizontal coordinate. The high-order word contains the vertical coordinate. >>

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2754707
thats what mnewton and I said.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2754892
Yes, I just want to say that it is documented in the new platform sdk.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2754959
Yes .. it is documented.  But the latest SDK also includes WTL, which has a bug in that it uses the wrong one.  So either the MS docs are wrong (unlikely) or the MS WTL library is wrong (quite likely).

It seems a very poor design decision to give the same message different parameters.  Surely separate messages would have been better.  One of the great mysteries of life :-)
0
 

Author Comment

by:appleby
ID: 2755144
I had to explicitly cast the &pointl param to long, but otherwise it worked fine.  I'm accepting RONSLOW's comment as the answer since it put together the two pieces I was missing.

Thank you all for the help.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2757839
BTW: the nice way to do the cast is

reinterpret_cast<LPARAM>(&pointl)

this makes it obvious that you are passing a pointer as an LPARAM.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand macro to ask for filename column 8 37
Detect CR LF to each line 12 153
method notes when mouse over in eclipse 5 83
fix34  challenge 9 105
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

15 Experts available now in Live!

Get 1:1 Help Now