Click here to Skip to main content
Licence CPOL
First Posted 11 Sep 2001
Views 212,841
Downloads 1,822
Bookmarked 57 times

Using the new HTML Editing classes in MFC7

By Chris Maunder | 11 Sep 2001
This article provides a bare-bones samples that demonstrates the use of the new HTML editing and browsing classes in MFC.
2 votes, 15.4%
1

2

3
2 votes, 15.4%
4
9 votes, 69.2%
5
4.81/5 - 22 votes
2 removed
μ 4.55, σa 2.59 [?]

Sample Image - MFCHTMLEdit.gif

Introduction

This article provides a bare-bones samples that demonstrates the use of the new HTML editing and browsing classes in MFC.

MFC7 includes several new classes that encapsulate the Internet Explorer MSHTML editing control. This control is an ActiveX control that provides WYSIWYG editing and browsing, and the MFC and ATL wrappers for this class make it's use very straight forward.

The sample application is a simple AppWizard generated application with the view class derived from CHtmlEditView (the last step in the MFC application wizard). In specifying CHtmlEditView as your edit class' base class, you automatically take on CHtmlEditDoc as your Document's base class as well.

These two classes work together to provide HTML file loading, saving, editing, browsing and printing. Context menus are provided and it is very simple to hook your toolbar button or menu items to the various wrapper methods in the MFC HTML editing classes.

The sample contains a rebar with two toolbars - one containing the standard load, save and cut/copy/paste buttons, and one containing formatting options such as bold, italic, indenting, hyperlink and editing mode.

In the CHtmlEditView derived class is a set of macro calls similar to the standard BEGIN_MESSAGE_MAP macros familiar to MFC programmers. In this case, though, the macros are of the form

BEGIN_DHTMLEDITING_CMDMAP(CHTMLEditView)
DHTMLEDITING_CMD_ENTRY(ID_EDIT_COPY, IDM_COPY)
DHTMLEDITING_CMD_ENTRY(ID_EDIT_CUT, IDM_CUT)
DHTMLEDITING_CMD_ENTRY(ID_EDIT_PASTE,IDM_PASTE)
DHTMLEDITING_CMD_ENTRY(ID_EDIT_UNDO, IDM_UNDO)
...
END_DHTMLEDITING_CMDMAP()

What these macros do is allow you to associate an MSHTML Command Identifier with a command ID. When the view receives the given command ID (say, ID_EDIT_COPY) it will execute the associated MSHTML command (IDM_COPY). This will in turn execute the
appropriate method of the MSHTML ActiveX control.

To associate a toolbar button with ID value ID_UNDERLINE with the MSHTML command for underlining the current selection, simply add the line

DHTMLEDITING_CMD_ENTRY(ID_UNDERLINE, IDM_UNDERLINE)

Some of the MSHTML identifiers are shown below. For a full list, consult the topic 'MSHTML Command Identifiers' in the online documentation.

IDM_BACKCOLOR Sets or retrieves the background color of the current selection.
IDM_BOLD Toggles the current selection between bold and nonbold.
IDM_BOOKMARK Creates a bookmark anchor or retrieves the name of a bookmark anchor for the current
IDM_COPY Copies the current selection to the clipboard.
IDM_CUT Copies the current selection to the clipboard and then deletes it.
IDM_DELETE Deletes the current selection.
IDM_FONT Opens a font dialog box to enable the user to change the text color, font, and font size of the current selection.
IDM_FONTNAME Sets or retrieves the font for the current selection.
IDM_FONTSIZE Sets or retrieves the font size for the current selection.
IDM_FORECOLOR Sets or retrieves the foreground (text) color of the current selection.
IDM_HYPERLINK Inserts a hyperlink on the current selection, or displays a dialog box enabling the user to specify a URL to insert as a hyperlink on the current selection.
IDM_INDENT Increases the indent of the selected text by one indentation increment.
IDM_ITALIC Toggles the current selection between italic and nonitalic.
IDM_JUSTIFYCENTER Centers the format block in which the current selection is located.
IDM_JUSTIFYLEFT Left-justifies the format block in which the current selection is located.
IDM_JUSTIFYRIGHT Right-justifies the format block in which the current selection is located.
IDM_ORDERLIST Toggles the current selection between an ordered list and a normal format block.
IDM_OUTDENT Decreases by one increment the indentation of the format block in which the current selection is located.
IDM_OVERWRITE Toggles the text-entry mode between insert and overwrite.
IDM_PASTE Overwrites the contents of the clipboard on the current selection.
IDM_PRINT Prints the current document using either the default print template or a custom print template.
IDM_PRINTPREVIEW Opens the Print Preview window for the current document using either the default print preview template or a custom template.
IDM_UNDERLINE Toggles the current selection between underlined and not underlined.
IDM_UNORDERLIST Toggles the current selection between an ordered list and a normal format block.

If you wish to call any one of the MSHTML commands directly, then the MFC wrapper classes provide you with a number of helper functions

HRESULT ExecHelperNN(UINT nCmdID);
HRESULT ExecHelperSetVal(UINT nCmdID, LPCTSTR szID=NULL) const
HRESULT ExecHelperSetVal(UINT nCmdID, bool bValue) const
HRESULT ExecHelperSetVal(UINT nCmdID, short nNewVal) const
HRESULT ExecHelperGetVal(UINT nCmdID, bool &bValue) const
HRESULT ExecHelperGetVal(UINT nCmdID, short &nValue) const

These functions take a MSHTML Command identifier, plus an optional value (or reference for a return value) and execute the associated command.

An example is in changing the editing more to Browse:

ExecHelperNN(IDM_BROWSEMODE);

More specific wrappers are provided, such as functions to set the font face (SetFontFace(LPCTSTR szFace)) or functions to set and get the HTML (GetDocumentHTML/SetDocumentHTML).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Chris Maunder

Founder
The Code Project
Canada Canada

Member

Follow on Twitter Follow on Twitter
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.
 
His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.
 
He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.
 
Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralOverriding the default Enter key behavior in CHtmlEditView PinmemberMember 288400810:01 26 Aug '09  
GeneralRe: Overriding the default Enter key behavior in CHtmlEditView [modified] PinmemberEkedorff8:14 5 Oct '11  
Generaldisplay a animated gif in the view area [modified] Pinmemberpk20071:43 24 Apr '07  
QuestionHow to lock blocks? [modified] Pinmemberrichard sancenot23:42 20 Nov '06  
GeneralUsing this example for converting MFc to HTML. Pinmembermoshiko_abc0:10 21 Jun '05  
Generalafter the file has been changed PinmemberTcpip200521:39 16 Apr '05  
Generalspell checker Pinmembercowboymoore9:40 5 Aug '04  
GeneralCan't show source PinmemberAlick Xiong5:16 29 Jun '04  
GeneralRe: Can't show source PinmemberAlick Xiong5:56 1 Jul '04  
GeneralMSHTML Custom Print Pinmembersenthil_kumar_project5:05 17 Jun '04  
GeneralProblem on handling mht files PinmemberAlick Xiong7:18 15 Jun '04  
GeneralCHtmlEditCtrl instead of view Pinmembercowboymoore18:21 4 Jun '04  
GeneralRe: CHtmlEditCtrl instead of view Pinmemberfrixx5:32 15 Jun '04  
GeneralRe: CHtmlEditCtrl instead of view PinsussAnonymous5:21 11 Sep '04  
GeneralRe: CHtmlEditCtrl instead of view PinmemberM Tarik12:26 20 Feb '05  
GeneralRe: CHtmlEditCtrl instead of view Pinmembertestasss4:23 31 Mar '06  
GeneralRe: CHtmlEditCtrl instead of view Pinmemberibraheempindi2:50 12 Nov '08  
AnswerFor those who are trying to do so... Pinmemberrichard sancenot6:11 25 Jan '07  
Generalyour code is a copy of MSDN samples Pinmemberhaoshenghan2:20 23 Apr '04  
GeneralRe: your code is a copy of MSDN samples PinadminChris Maunder11:03 23 Apr '04  
QuestionHow to make MSDN htmlEdit Sample available in VC6 Pinmemberokboy19:26 25 Dec '03  
AnswerRe: How to make MSDN htmlEdit Sample available in VC6 PinmemberIrek Zielinski11:17 5 Jun '04  
QuestionHow to Modify Right Button Menu? PinsussFERREO-ROCHE22:58 13 Aug '03  
QuestionHow to use CHtmlEditCtrl in a Dialog Box PinmemberraulCM7:13 4 Aug '03  
AnswerRe: How to use CHtmlEditCtrl in a Dialog Box PinmemberMarkDoubson18:02 2 Mar '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120210.1 | Last Updated 12 Sep 2001
Article Copyright 2001 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid