• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

How do I trap CRichEditCtrl messages?

I am running VC++ 4.0, Win 98.

I'm something of a newbie and suspect I'm missing something obvious.  If not, I'd be glad to increase the points on this.

I have a dialog box with a RichEdit control.  I am able to put the text in the control, format it, etc. with no trouble.

When the user clicks (or double-clicks) on a word in the control, I want to trap which word he clicked on and act on it in my dialog class.

I would like to either trap the X, Y point of the mouse cursor when the user clicked, or possibly just grab the word (if he double-clicked on it).

How can I trap LBUTTONDOWN, LBUTTONUP, SELCHANGE, etc. messages in the dialog class, or is there another way to do this?

  • 3
  • 3
1 Solution
start with subclass !
create new class using the wizard, derive it from CRichEdit.
in your dialog InitDialo do this:
ctrlclass= instance of your new class (CRichEdit)
IDC_MYRICHEDIT_CTRL= the richedit resource name.

now all the messages you handle in this new class, same way as u do in a view or a dialog, go to classwizard, and create new message, lets say WM_LBUTTONDOWN, everytime u click the left button, you'll run the code from within your richedit class, and not the dialog clas


SixStringAuthor Commented:
Ok, I tried that, but get a Debug Assertion Fail.  Here is the code.  (I had to create a CEdit control, then "overwrite" it with a CRichEdit control, since VC++ 4 studio doesn't let me create a RichEditCtrl.)

m_richEdit is of class CMyRichEditCtrl, which is derived from CRichEditCtrl.

// Get a pointer to the existing plain edit window
CWnd* pWnd = GetDlgItem (IDC_EDIT_ARRANGE);       

// Find its location
CRect rect;
pWnd->GetWindowRect (rect);
ScreenToClient (rect);

// Destroy the existing window

// Create the new window
m_richEdit.Create (dwStyle, rect, this, IDC_EDIT_ARRANGE);

VERIFY (m_richEdit.SubclassDlgItem (IDC_EDIT_ARRANGE, this));

I get a Debug Assertion Failed error on the last line.
Debug tells me m_richEdit is of class CMyRichEditCtrl, with a handle hWnd=0x00000df4.  m_hWndOwner is 0x00000000

go to the crichedit class you just created and put the following function in there from the claswizard:

take off the VERIFY, and out a breakpoint in the PreSubClassWindow() function
see if its got there.
make sure the control is built using the Create function, see if you see it by using
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

SixStringAuthor Commented:
I added the PreSubclassWindow function and put a breakpoint in it.  Yes, it is getting there.  The Debug assertion happens after that, but before the dialog box appears.

I did not understand your last comment, about using SW_SHOW.

By the way, does it matter where all this RichEdit code is in the OnInitDialog function; that is, before or after the CDialog::OnInitDialog() call?

suppose to be after OnInitDialog()
the SW_SHOW is a flag to show a window, check if the edit is get shown, in other words, does your edit control created at all.

are u using Dialog/CForm ?
if so, why u are using Create, and u do not place them on the Dialog itself ?
any reasin ?
SixStringAuthor Commented:
I know what WM_SHOW does, I just didn't understand your request.  Let me recap:

The dialog and RichEdit control were working fine before I added the subclassing; I just wanted to trap commands.

Now that I'm subclassing, I get an Assertion failure BEFORE the dialog is displayed, but sometime AFTER the call to the PreSubclassWindow function you had me add.  If I Ignore the Debug Assertion error, the programs continues fine; the dialog and controls are created and the button commands are trapped, just like I need.

I just need to get rid of the Assertion failure.

I don't know what Dialog/CForm is.  I am using Create because I got the code here on Ex-exchange from someone else who had a similar question:  how to create a RichEditCtrl when you're using VC++ 4, which has only a standard EditCtrl button in Developer Studio.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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