Click here to Skip to main content
12,360,321 members (56,683 online)
Click here to Skip to main content
Add your own
alternative version

Stats

283.4K views
2.9K downloads
60 bookmarked
Posted

Using the new HTML Editing classes in MFC7

, 11 Sep 2001 CPOL
Rate this:
Please Sign up or sign in to vote.
This article provides a bare-bones samples that demonstrates the use of the new HTML editing and browsing classes in MFC.
<!-- Download Links --> <!-- Article image -->

Sample Image - MFCHTMLEdit.gif

<!-- Main HTML starts here -->

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)

Share

About the Author

Chris Maunder
Founder CodeProject
Canada Canada
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.

You may also be interested in...

Comments and Discussions

 
QuestionWhy can't I get hyperlinks to work in my derived CHtmlEditView class? Pin
twoee19-Apr-12 0:03
membertwoee19-Apr-12 0:03 
GeneralOverriding the default Enter key behavior in CHtmlEditView <p></p> Pin
Member 288400826-Aug-09 9:01
memberMember 288400826-Aug-09 9:01 
GeneralRe: Overriding the default Enter key behavior in CHtmlEditView Pin
Ekedorff5-Oct-11 7:14
memberEkedorff5-Oct-11 7:14 
Generaldisplay a animated gif in the view area [modified] Pin
pk200724-Apr-07 0:43
memberpk200724-Apr-07 0:43 
QuestionHow to lock blocks? [modified] Pin
richard sancenot20-Nov-06 22:42
memberrichard sancenot20-Nov-06 22:42 
GeneralUsing this example for converting MFc to HTML. Pin
moshiko_abc20-Jun-05 23:10
membermoshiko_abc20-Jun-05 23:10 
Generalafter the file has been changed Pin
Tcpip200516-Apr-05 20:39
memberTcpip200516-Apr-05 20:39 
Generalspell checker Pin
cowboymoore5-Aug-04 8:40
membercowboymoore5-Aug-04 8:40 
GeneralCan't show source Pin
Alick Xiong29-Jun-04 4:16
memberAlick Xiong29-Jun-04 4:16 
GeneralRe: Can't show source Pin
Alick Xiong1-Jul-04 4:56
memberAlick Xiong1-Jul-04 4:56 
GeneralMSHTML Custom Print Pin
senthil_kumar_project17-Jun-04 4:05
membersenthil_kumar_project17-Jun-04 4:05 
GeneralProblem on handling mht files Pin
Alick Xiong15-Jun-04 6:18
memberAlick Xiong15-Jun-04 6:18 
GeneralCHtmlEditCtrl instead of view Pin
cowboymoore4-Jun-04 17:21
membercowboymoore4-Jun-04 17:21 
GeneralRe: CHtmlEditCtrl instead of view Pin
frixx15-Jun-04 4:32
memberfrixx15-Jun-04 4:32 
GeneralRe: CHtmlEditCtrl instead of view Pin
Anonymous11-Sep-04 4:21
sussAnonymous11-Sep-04 4:21 
GeneralRe: CHtmlEditCtrl instead of view Pin
M Tarik20-Feb-05 11:26
memberM Tarik20-Feb-05 11:26 
GeneralRe: CHtmlEditCtrl instead of view Pin
testasss31-Mar-06 3:23
membertestasss31-Mar-06 3:23 
GeneralRe: CHtmlEditCtrl instead of view Pin
ibraheempindi12-Nov-08 1:50
memberibraheempindi12-Nov-08 1:50 
AnswerFor those who are trying to do so... Pin
richard sancenot25-Jan-07 5:11
memberrichard sancenot25-Jan-07 5:11 
Generalyour code is a copy of MSDN samples Pin
haoshenghan23-Apr-04 1:20
memberhaoshenghan23-Apr-04 1:20 
GeneralRe: your code is a copy of MSDN samples Pin
Chris Maunder23-Apr-04 10:03
adminChris Maunder23-Apr-04 10:03 
QuestionHow to make MSDN htmlEdit Sample available in VC6 Pin
okboy25-Dec-03 18:26
memberokboy25-Dec-03 18:26 
AnswerRe: How to make MSDN htmlEdit Sample available in VC6 Pin
Irek Zielinski5-Jun-04 10:17
memberIrek Zielinski5-Jun-04 10:17 
QuestionHow to Modify Right Button Menu? Pin
FERREO-ROCHE13-Aug-03 21:58
sussFERREO-ROCHE13-Aug-03 21:58 
QuestionHow to use CHtmlEditCtrl in a Dialog Box Pin
raulCM4-Aug-03 6:13
memberraulCM4-Aug-03 6:13 
AnswerRe: How to use CHtmlEditCtrl in a Dialog Box Pin
MarkDoubson2-Mar-06 17:02
memberMarkDoubson2-Mar-06 17:02 
GeneralSelection in CHtmlEditView Pin
AleemSheikh4-Jul-03 3:22
memberAleemSheikh4-Jul-03 3:22 
QuestionHow to disable toolbar in "Browse" mode? Pin
conan52015-Apr-03 16:27
memberconan52015-Apr-03 16:27 
QuestionHow can i use this in VC6 ?????? Pin
mz200028-Nov-02 2:58
sussmz200028-Nov-02 2:58 
AnswerMFC6 version now available Pin
Jean-Michel LE FOL5-May-03 6:38
memberJean-Michel LE FOL5-May-03 6:38 
GeneralRe: MFC6 version now available Pin
jmmolina16-Jul-03 21:25
memberjmmolina16-Jul-03 21:25 
AnswerRe: How can i use this in VC6 ?????? Pin
Irek Zielinski5-Jun-04 10:17
memberIrek Zielinski5-Jun-04 10:17 
Questionhow to disable element edit? Pin
cool_stone13-Nov-02 17:58
membercool_stone13-Nov-02 17:58 
Generalsomeone PLEASE post a sample using VC 6.0 Pin
Anonymous19-Oct-02 20:28
sussAnonymous19-Oct-02 20:28 
GeneralRe: someone PLEASE post a sample using VC 6.0 Pin
Irek Zielinski5-Jun-04 10:16
memberIrek Zielinski5-Jun-04 10:16 
GeneralTables and Frames Pin
Ji Gua6-Aug-02 11:38
sussJi Gua6-Aug-02 11:38 
QuestionHow to get text from the Htmlview? Pin
Anonymous15-Jul-02 16:44
sussAnonymous15-Jul-02 16:44 
AnswerRe: How to get text from the Htmlview? Pin
mobian12-Aug-02 18:56
membermobian12-Aug-02 18:56 
GeneralRe: How to get text from the Htmlview? Pin
AleemSheikh19-May-03 4:35
memberAleemSheikh19-May-03 4:35 
QuestionHow to handle events in HTML document? Pin
Mircea Berghia10-Jul-02 21:08
sussMircea Berghia10-Jul-02 21:08 
Questionhow to browse view? Pin
fhqiplj4-Jul-02 16:14
memberfhqiplj4-Jul-02 16:14 
QuestionHow to insert an ActiveX ctrl into an HTML document ? Pin
Lucian Bumbuc1-Jul-02 8:27
memberLucian Bumbuc1-Jul-02 8:27 
AnswerRe: How to insert an ActiveX ctrl into an HTML document ? Pin
Lucian Bumbuc8-Jul-02 5:22
memberLucian Bumbuc8-Jul-02 5:22 
GeneralRe: How to insert an ActiveX ctrl into an HTML document ? Pin
Ji Gua6-Aug-02 11:36
sussJi Gua6-Aug-02 11:36 
QuestionInsert tables? Pin
ZnakeEye8-Feb-02 10:51
memberZnakeEye8-Feb-02 10:51 
AnswerRe: Insert tables? Pin
Art Cote3-Nov-02 16:10
sussArt Cote3-Nov-02 16:10 
GeneralRe: Insert tables? Pin
chamana28-Feb-03 0:20
memberchamana28-Feb-03 0:20 
AnswerRe: Insert tables? other method! Pin
conan52016-Apr-03 16:50
memberconan52016-Apr-03 16:50 
QuestionCan this be done in MFC6? Pin
JamesPrevallet4-Oct-01 9:48
memberJamesPrevallet4-Oct-01 9:48 
AnswerSure! Pin
Vitali Brusentsev8-Oct-01 17:23
memberVitali Brusentsev8-Oct-01 17:23 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 12 Sep 2001
Article Copyright 2001 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid