Click here to Skip to main content
13,297,266 members (71,072 online)
Click here to Skip to main content
Add your own
alternative version


110 bookmarked
Posted 24 May 2004

HTML editor for VC++ 6.0

, 27 Dec 2004
Rate this:
Please Sign up or sign in to vote.
HTML editing control for VC++ 6.0 equivalent to MFC7 CHtmlEditCtrlBase class.

Sample Image - HtmlEdit.gif


Today, applications often need a rich user interface. One of them is the ability to compose or edit HTML documents in a easy, effective and reliable way. This article and source code attached gives you a nice control to perform this task. And now, the best part: this control works fine with Microsoft VC++ 6.0!


I'm the author of Tray Helper application. For a long time, I wanted to add a feature to send a colorful HTML mail. After some research, I found that Microsoft did a nice thing implementing IHTMLDocument2 interface (and a few others related to it) - but working with raw COM interfaces is not very easy, effective or programmer-friendly. I think someone from Microsoft also realized that, and in MFC 7.0, there is a very nice wrapper for COM: CHtmlEditCtrlBase class. After examining the code of this class, I decided to port it to MFC 4.0 and VC++ 6.0.

Using the code

The CHtmlEditCtrl2 class I want to introduce there is not only ported from CHtmlEditCtrlBase - I did some other improvements and changes to the original code - so it not only compiles under VC++ 6.0 but also has some new features and is even easier to use in existing projects. The main change was that CHtmlEditCtrl2 class derives from CWebBrowser2. It means that if your projects already use a standard web browser control to display HTML documents - adding editing capabilities would be trivial (read below for a full instruction).

Adding HTML editor to your projects:

Step 1: Adding an ordinary web browser control to your project.

If you already use a web browser control in your project - skip this paragraph and start reading from "Step 2".

Let's assume that you want to add a HTML edit control to your dialog based application. First, you need to add a web browser control that will contain your HTML documents.

  • In order to do it, go to dialog template editor of Visual Studio and click on "Project / Add to project / Components and controls". From pop-up dialog, select "Registered ActiveX controls", and finally select "Web Browser control" (the name of the control depends on the language of your PC - for example, on my Polish version of Windows, it is "Przegladarka Przeglądarka sieci Web firmy Microsoft"). To be sure that you're selecting the right control, check the description below the file list - it should be "WebBrowser control".
  • After selecting a control, a dialog box with CWebBrowser2 class to be created should appear. Click on "OK" without any modifications.
  • Note that new position should be added to tool-bar with list of controls available to insert into dialog. So, select it and just add a web browser!
  • Finally, use a class wizard (Ctrl+W) to add a member variable of type CWebBrowser2 to just add item.
  • Next, you can add a OnInitdialog method to your dialog and request a browser to load a HTML document using Navigate method of CWebBrowser2 class.

Step 2: Adding edit capabilities to web browser.

It's very easy. Just follow these few steps:

  • Copy HtmlEditCtrl2.H and HtmlEditCtrl2.cpp directory with your source files and add them to your project.
  • Modify header file where you have a member variable of CWebBrowser2 declared and change it to CHtmlEditCtrl2.
  • Change #include "WebBrowser2.H" to #include "HtmlEditCtrl2.H".
  • After loading a HTML document (using, for example, Navigate method), call SetDesignMode(TRUE) method to enable edit mode.
  • If after edit is complete, you want to get a source of HTML document, call GetDocumentHTML function.

Highlight of the most important methods of the control (most likely you will need to use them):

  • void Navigate(LPCTSTR URL, VARIANT* Flags, VARIANT* TargetFrameName, VARIANT* PostData, VARIANT* Headers)

    Use this function to load a HTML document. Pass as URL, a string with a path to the file to open. It could be a remote site or a local file. Rest of the parameters could be null.


    Navigate("<A href=""></A>", NULL, NULL, NULL, NULL);
    Navigate("C:\\my_file.html", NULL, NULL, NULL, NULL);
  • BOOL SetDesignMode(BOOL bMode);

    Enables or disables edit mode. In versions of Internet Explorer prior to 6.0, document must be loaded in the first place to enter a design mode.

  • HRESULT GetDocumentHTML(CString& szHTML, BOOL a_bClearDirtyFlag = FALSE);

    Gets the content of HTML document.


    Sets new content for HTML document. Please note that you can't call this function on an empty control. You need to load a document first (for example, by calling the Navigate method).

Complete list of public member functions of CHtmlEditCtrl2 class:

These member functions are taken directly from CHtmlEditCtrlBase class. Full description at MSDN pages (click here):

BOOL SetDesignMode(BOOL bMode);
HRESULT ExecCommand(const GUID *pGuid, long cmdID, 
  long cmdExecOpt, VARIANT* pInVar=NULL, VARIANT* pOutVar=NULL);
HRESULT ExecCommand(long cmdID, long cmdExecOpt, 
long QueryStatus(long cmdID);
HRESULT GetEvent(IHTMLEventObj **ppEventObj);
HRESULT GetEventSrcElement(IHTMLElement **ppSrcElement);
HRESULT GetDocument(IHTMLDocument2** ppDoc);
HRESULT NewDocument();
HRESULT GetDocumentHTML(CString& szHTML, 
  BOOL a_bClearDirtyFlag = FALSE);
HRESULT GetIsDirty();
HRESULT GetDocumentTitle(CString& szTitle);
HRESULT GetBlockFormatNames(CStringArray &sa);
HRESULT SetForeColor(LPCTSTR szColor);
HRESULT SetForeColor(int nColor);
HRESULT GetForeColor(int &nColor);
HRESULT GetBackColor(int& nColor);
HRESULT SetBackColor(LPCTSTR szColor);
HRESULT SetBackColor(int nColor);

HRESULT SetDefaultComposeSettings(LPCSTR szFontName=NULL,
                  unsigned short nFontSize=3,
                  COLORREF crFontColor=0xFF000000,
                  COLORREF crFontBgColor=0xFF000000,
                  bool bBold = false,
                  bool bItalic = false,
                  bool bUnderline = false);

HRESULT GetBlockFormat(CString& strFormat);
HRESULT SetBlockFormat(LPCTSTR szFormat);
HRESULT GetFontFace(CString& strFace);
HRESULT SetFontFace(LPCTSTR szFace);
HRESULT GetBookMark(CString& strAnchor);
HRESULT SetBookMark(LPCTSTR szAnchorName);
HRESULT SetOverwriteMode(bool bMode);
HRESULT Is1DElement(bool& bValue);
HRESULT Is2DElement(bool& bValue);
HRESULT GetFontSize(short& nSize);
HRESULT SetFontSize(unsigned short size);
HRESULT GetFrameZone(short& nZone);
HRESULT SetCSSEditingLevel(short nLevel);

HRESULT AddToGlyphTable(LPCTSTR szTag,
            LPCTSTR szImgUrl,
            unsigned short nTagType,
            unsigned short nAlignment,
            unsigned short nPosInfo,
            unsigned short nDirection,
            unsigned int nImgWidth,
            unsigned int nImgHeight);

HRESULT EmptyGlyphTable();
HRESULT HorizontalLine(LPCTSTR szId = NULL);
HRESULT InsInputButton(LPCTSTR szId = NULL);
HRESULT InsInputHidden(LPCTSTR szId = NULL);
HRESULT InsInputPassword(LPCTSTR szId = NULL);
HRESULT InsInputSubmit(LPCTSTR szId = NULL);
HRESULT InsInputUpload(LPCTSTR szId = NULL);
HRESULT GetAbsolutePosition(bool &bCurValue);
HRESULT SetAbsolutePosition(bool bNewValue);
HRESULT Set2DPosition(bool bNewValue);
HRESULT SetAtomicSelection(bool bNewValue);
HRESULT SetAutoURLDetectMode(bool bNewValue);
HRESULT SetDisableEditFocusUI(bool bNewValue);
HRESULT SetIE5PasteMode(bool bNewValue);
HRESULT SetLiveResize(bool bNewValue);
HRESULT SetMultiSelect(bool bNewValue);
HRESULT SetOverrideCursor(bool bNewValue);
HRESULT SetRespectVisInDesign(bool bNewValue);
HRESULT GetShowAlignedSiteTags(bool &bCurValue);
HRESULT SetShowAlignedSiteTags(bool bNewValue);
HRESULT GetShowAllTags(bool &bCurValue);
HRESULT SetShowAllTags(bool bNewValue);
HRESULT GetShowAreaTags(bool &bCurValue);
HRESULT SetShowAreaTags(bool bNewValue);
HRESULT GetShowCommentTags(bool &bCurValue);
HRESULT SetShowCommentTags(bool bNewValue);
HRESULT GetShowMiscTags(bool &bCurValue);
HRESULT SetShowMiscTags(bool bNewValue);
HRESULT GetShowScriptTags(bool &bCurValue);
HRESULT SetShowScriptTags(bool bNewValue);
HRESULT GetShowStyleTags(bool &bCurValue);
HRESULT SetShowStyleTags(bool bNewValue);
HRESULT GetShowUnknownTags(bool &bCurValue);
HRESULT SetShowUnknownTags(bool bNewValue);
HRESULT GetShowBRTags(bool &bCurValue);
HRESULT SetShowBRTags(bool bNewValue);
HRESULT PrintDocument();
HRESULT PrintDocument(LPCTSTR szPrintTemplate);
HRESULT PrintDocument(bool bShowPrintDialog);
HRESULT PrintPreview();
HRESULT PrintPreview(LPCTSTR szPrintTemplate);
HRESULT Delete();
HRESULT Indent();
HRESULT Italic();
HRESULT JustifyCenter();
HRESULT JustifyLeft();
HRESULT JustifyRight();
HRESULT Outdent();
HRESULT Paste();
HRESULT RemoveFormat();
HRESULT SelectAll();
HRESULT Underline();
HRESULT Unlink();
HRESULT ClearSelection();
HRESULT RefreshDocument();
HRESULT UnBookmark();

These member functions were added by the author of this article (Ireneusz Zieliński) just to make the usage of this class easier, and are not a part of the original ChtmlEditCtrlBase class:

  • BOOL IsBold();

    Checks if currently selected text has “bold” attribute.

  • BOOL IsUnderline();

    Checks if currently selected text has “underline” attribute.

  • BOOL IsStrikeOut();

    Checks if currently selected text has “strike-out” attribute.

  • BOOL IsItalic();

    Checks if currently selected text has “italic” attribute.

  • BOOL CanPaste();

    Checks if paste operation is available on HTML editor right now.

  • HRESULT LineBreakNormal();

    Inserts a single line break (HTML <BR> tag) at current editing position.

  • BOOL IsDesignMode();

    Checks if editor is in “designer” mode (in other words, TRUE means that you can edit content).

  • HRESULT GetURLsOfAllImages(CStringArray& a_arrImages);

    Returns a collection of all images present in currently loaded HTML document.

  • HRESULT ReplaceImageURL(const CString& a_sUrlToReplace, const CString& a_sUrlToReplaceWith);

    Replaces an image with a different one.

  • HRESULT GetDocumentBody(CString& a_sBody, BOOL a_bTextInsteadHTML);

    Gets the body of the message. Body is a part of HTML document that is limited by <BODY> and </BODY> HTML tags. You can pass TRUE as a second parameter to get a plain text representation of the content of the document.

  • HRESULT Undo();

    Performs an undo operation on editor.

  • HRESULT Redo();

    Performs a redo operation on editor.

  • HRESULT Find();

    Displays a Find Text dialog.

  • HRESULT StrikeOut();

    Sets a “strike-out” attribute on currently selected text.

  • HRESULT SubScriptSelectedText();

    Sets a “sub-script” attribute on currently selected text.

  • HRESULT SuperScriptSelectedText();

    Sets a “super-script” attribute on currently selected text.

  • HRESULT SetDocumentCharset(const CString& a_sCharsetEncoding);

    Sets a new charset encoding for currently loaded HTML document.

  • HRESULT GetDocumentCharset(CString& a_sCharsetEncoding);

    Returns a string representing the current character encoding of the document.

  • HRESULT ShowSource();

    Opens Notepad with the HTML source of the document.

  • HRESULT ShowIEOptionsDialog();

    Displays a dialog with Internet Explorer options.

  • HRESULT GetBodyBackgroundImage(CString& a_sImage);

    Gets a URL to the background image of the document.

  • HRESULT SetBodyBackgroundImage(const CString& a_sImage);

    Sets a new background image for the document.

  • HRESULT GetBodyProperties(CString& a_sTag);

    Gets proprieties of <BODY> tag.

  • HRESULT GetBodyBackgroundColor(CString& a_sColor);

    Gets background color of document’s body.

  • HRESULT GetBodyTextColor(CString& a_sColor);

    Gets text color of document’s body.

  • HRESULT PasteHTMLAtCurrentSelection(const CString& a_sHTMLText, BOOL a_bSetCursorAtBeginingOfInsertedText);

    Pastes HTML code at the current cursor location. Moves back cursor after inserting, if required.

The meaning and usage of all of these functions are very easy to guess / understand. Most of these functions are simple wrappers over COM calls - so, in case of any problems - check what COM function is being called and read about it in MSDN. The ultimate MSDN resource is a description of CHtmlEditCtrlBase class. Since my class is only an 80% port of CHtmlEditCtrlBase, I recommend to read this page first (in case of any questions).

Here are some tips that could save you some time:

  1. Call SetDesignerMode(TRUE) function when document is loaded into control. Doing it just after calling Navigate(url...) could be too early (function will fail if Internet Explorer prior to version 6.0 is installed on the machine).
  2. If you create an empty HTML document and start editing it - you quickly realize that pressing an Enter key inserts a paragraph, not a line break. This default behavior is pretty irritating and could be solved in two ways: first, you could press SHIFT + ENTER instead, or load into HTML control such an HTML document:
    <DIV> </DIV>
  3. In case of compilation errors, make sure that you have on your machine the latest Platform SDK installed (the most important are Core and Internet Development SDKs).

At this point of the article, I would like to wish you good luck creating killer applications ;-D. Sorry for my English - it's not my native language.

Points of Interest

History of changes

  • 27th of December 2004: Now, control should work perfect with UNICODE defined. Added fail-safe CopyEx method (full description in code (CPP file)).
  • 8th of June 2004: Updated demo application.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Irek Zielinski
Web Developer
United Kingdom United Kingdom
Irek works as a C++ senior software developer.
He runs also his own small shareware bussines (He is author of few quite popular applications like: Tray Helper or Time Adjuster).

Occasionaly he posts articles to Codeguru or Codeproject. Besides C++ he likes motorcycles and paragliding.
Check out his software at:

You may also be interested in...

Comments and Discussions

QuestionSet ZOOM function? Pin
bosfan21-Oct-16 5:51
memberbosfan21-Oct-16 5:51 
QuestionThe best example of using WindowsWebBrowser control, not the one of !!! Pin
margaritaville27-Oct-13 20:51
membermargaritaville27-Oct-13 20:51 
Questioni have get the information of the html,how to show the html information to dialog? Pin
uukko26-Oct-11 0:52
memberuukko26-Oct-11 0:52 
First thanks your code , it's very useful for me. thank you very much
when i got the html information, i want to show it on the other dialog,but it doesn't work

just like

i can't show the strNewString information(html information)
can u show me how to use the SetDocumentHTML or how to show it on the other dialog?
thank you very much
GeneralMy vote of 4 Pin
jyl_hrb17-Apr-11 22:38
memberjyl_hrb17-Apr-11 22:38 
GeneralBug in GetHTMLDocument using Ansi Pin
schmurgulum28-Jan-11 6:31
memberschmurgulum28-Jan-11 6:31 
GeneralAdding a "Find and Replace" dialog Pin
Member 288400822-Sep-10 9:28
memberMember 288400822-Sep-10 9:28 
GeneralTool Tips Pin
AggMeena7-Dec-09 0:51
memberAggMeena7-Dec-09 0:51 
shrikanth 20093-Jun-09 5:50
membershrikanth 20093-Jun-09 5:50 
Irek Zielinski7-Dec-09 1:05
memberIrek Zielinski7-Dec-09 1:05 
GeneralSetAutoURLDetectMode not work Pin
bunpkin20-May-09 1:23
memberbunpkin20-May-09 1:23 
GeneralRe: SetAutoURLDetectMode not work Pin
bunpkin20-May-09 17:19
memberbunpkin20-May-09 17:19 
QuestionHow to format <h1>,<h2>,<h3>...?</h3></h2></h1> Pin
y_yy200824-Jan-09 3:10
membery_yy200824-Jan-09 3:10 
QuestionGetHTMLDocument Pin
Natacha268-Oct-08 5:39
memberNatacha268-Oct-08 5:39 
Answeri found it and correct it as follows: Pin
y_yy200824-Jan-09 3:01
membery_yy200824-Jan-09 3:01 
AnswerRe: i found it and correct it as follows: Pin
Irek Zielinski26-Jan-09 2:23
memberIrek Zielinski26-Jan-09 2:23 
GeneralThanks, your modificatoin is a bug free! Pin
y_yy200826-Jan-09 18:27
membery_yy200826-Jan-09 18:27 
QuestionCan we make a DLL of a program which are using ActiveX control Pin
nitin_pro27-Feb-08 20:38
membernitin_pro27-Feb-08 20:38 
Questioncall this application from IE Button Pin
JD8120-Nov-07 1:22
memberJD8120-Nov-07 1:22 
Questionerror unresolved external symbol _WinMain@16 Pin
JD8118-Nov-07 14:39
memberJD8118-Nov-07 14:39 
AnswerRe: error unresolved external symbol _WinMain@16 Pin
JD8118-Nov-07 14:57
memberJD8118-Nov-07 14:57 
Questionhow to find whether the focus is on the browser Pin
rupesh_gangi6-Nov-07 2:20
memberrupesh_gangi6-Nov-07 2:20 
Questioncuhow to send focus to the browser? Pin
rupesh_gangi5-Nov-07 23:20
memberrupesh_gangi5-Nov-07 23:20 
GeneralID value erros solution Pin
rupesh_gangi5-Nov-07 4:21
memberrupesh_gangi5-Nov-07 4:21 
General20 'error C2065' Errors Pin
kunal.tawde24-Oct-07 2:33
memberkunal.tawde24-Oct-07 2:33 
GeneralRe: 20 'error C2065' Errors Pin
yaron shani30-Oct-07 10:13
memberyaron shani30-Oct-07 10:13 
GeneralRe: 20 'error C2065' Errors Pin
shjw73041-Sep-08 23:55
membershjw73041-Sep-08 23:55 
QuestionVery nice but speller? Pin
cowboymoore15-Oct-07 11:38
membercowboymoore15-Oct-07 11:38 
QuestionHow save html without popup dialog Box means save not saveas Pin
Yogesh Kumar Dhiman7-Oct-07 21:25
memberYogesh Kumar Dhiman7-Oct-07 21:25 
AnswerRe: How save html without popup dialog Box means save not saveas Pin
Irek Zielinski14-Oct-07 8:30
memberIrek Zielinski14-Oct-07 8:30 
AnswerRe: How save html without popup dialog Box means save not saveas Pin
ismael6-May-08 5:09
memberismael6-May-08 5:09 
Questionhow to mute the explorer window ? Pin
Paul the Great30-Sep-07 5:48
memberPaul the Great30-Sep-07 5:48 
QuestionHow to navigate in the same window when i open a new URL? Pin
lincolnfz7-Jan-07 4:23
memberlincolnfz7-Jan-07 4:23 
GeneralThanks Pin
cqwally26-Nov-06 16:46
membercqwally26-Nov-06 16:46 
GeneralShow dotted Table border Pin
Staati21-Apr-06 5:43
memberStaati21-Apr-06 5:43 
QuestionHow to save the HTML Pin
nubz29-Mar-06 12:46
membernubz29-Mar-06 12:46 
AnswerRe: How to save the HTML Pin
Staati21-Apr-06 5:21
memberStaati21-Apr-06 5:21 
GeneralPossible solution to compile Pin
nubz29-Mar-06 12:45
membernubz29-Mar-06 12:45 
General(Slightly offtopic - Can you help with some MSHTML issues) Pin
mcahriman10-Mar-06 11:34
membermcahriman10-Mar-06 11:34 
Generala practical question Pin
scvarz3-Feb-06 11:05
memberscvarz3-Feb-06 11:05 
QuestionHow to handle &quot;Properties&quot;? Pin
Anonymous18-Aug-05 3:01
sussAnonymous18-Aug-05 3:01 
GeneralSetDesignMode erases content Pin
manju_soham28-Jul-05 1:57
membermanju_soham28-Jul-05 1:57 
GeneralRe: SetDesignMode erases content Pin
manju_soham28-Jul-05 2:02
membermanju_soham28-Jul-05 2:02 
GeneralRe: SetDesignMode erases content Pin
David Fleming2-Aug-05 13:33
memberDavid Fleming2-Aug-05 13:33 
GeneralRe: SetDesignMode erases content Pin
manju_soham2-Aug-05 18:53
membermanju_soham2-Aug-05 18:53 
GeneralRe: SetDesignMode erases content Pin
David Fleming3-Aug-05 19:27
memberDavid Fleming3-Aug-05 19:27 
GeneralQuestion about downloading the SDK Pin
David Fleming13-Jul-05 5:50
memberDavid Fleming13-Jul-05 5:50 
GeneralCStreamOnCString Pin
Tcpip200525-Mar-05 0:37
memberTcpip200525-Mar-05 0:37 
QuestionRe: CStreamOnCString Pin
Alex_kurumpelil21-Nov-06 5:51
memberAlex_kurumpelil21-Nov-06 5:51 
GeneralI Have found a way to get rid of it's border,and how to change it's back color Pin
wpxs030313-Feb-05 3:36
memberwpxs030313-Feb-05 3:36 
GeneralRe: I Have found a way to get rid of it's border,and how to change it's back color Pin
EnderJSC4-Nov-05 19:05
memberEnderJSC4-Nov-05 19:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 28 Dec 2004
Article Copyright 2004 by Irek Zielinski
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid