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

How do I trap HTML events from an embedded web control in edit mode?

In my application I have an embedded web control and some buttons that allow the user to enter edit mode and change properties in the html code like font color, bold, italic, etc.
I want to trap the event of the user selecting a piece of text and set these buttons to their appropriate states like in an editor (ie. the "bold" button should be pressed/checked if the selection is bold).
I have already implemented the following relevant classes (aside from the obvious CWebbrowser class):

class CImpIDispatch : public IDispatch
class CCustomControlSite : public COleControlSite
class CCustomOccManager : public COccManager

I want to be able to capture events like
when in edit mode.
How do I do that?

Thanks in advance.
1 Solution

It's much easier if the thingie is embedded in a dialog or form, but even if you use it as view, the following might help you generate the necessary code

(I suggest try this in a helper project first, for playing around, etc....)

Create a dialog resource, right-click, choose "Insert ActiveX control", choose "Microsoft Webbrowser"

in class wizard, member variables, add a member variable for the IDC_EXPLORER1.

You will be asked to import the Webbrowser defiunitions, agree. (Note: you can do this in another project any time using clazz wizard, "Add Class / From type library..")

now, in class view, right click on the dialog, choose "Add Windows Message Handler". Select the doialog resource, add a handler for some other event you might (or might not) be interested in.

Now we can examine the dlg, to see what we would need to take over:

member variable:
CWebBrowser2 m_browser;   // or whatever

// when you want to do this in a view, declare CWebBrowser2 as member of the view class, and call m_browser.Create to create it. See note below)

in .cpp file:
BEGIN_EVENTSINK_MAP(class, baseclass)

you can now add additional handlers manually. First, check the parameter list of the event. Mostly, it's variants, bools, and a pointer to one of this (as return value)

- declare a message handler function (outside the //{AFX_...}); make sure you use the correct argument list

- add an entry to the event sink map

ON_EVENT( <class where the handler function is in>,
          <ID of the browser control>,
          <event dispatch id>,
          <handler function>,
          <parameter type list>)
note that the parameter type list is a sequence of VTS_.. (like VTS_VARIANT, VTS_BOOL, VTS_PBOOL) WITH NO SEPARATORS!

- add an implementation of the handler


Note on views:
Unhappily, MFC allows catching COM events only in the parent of the window hosting the actual Control. That's why you need to create the browser control as child of your view. I have some more info on this at

Good luck!

Roshan DavisCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: peterchen

Please leave any comments here within the next seven days.


Roshan Davis
EE Cleanup Volunteer
DGeomelAuthor Commented:
This is about an older project and I apologize for letting it linger for
so long. Although I remember Peter's answer had not been very helpful, I have no choice but to award him the points now.
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

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