Click here to Skip to main content
15,393,377 members
Articles / Desktop Programming / MFC
Posted 3 May 2001


77 bookmarked

The MFC CDHtmlDialog class

Rate me:
Please Sign up or sign in to vote.
4.25/5 (13 votes)
11 Sep 2001CPOL2 min read
This sample demonstrates using the MFC CDHtmlDialog class in MFC7

Sample Image - DHTMLDialog.gif


This sample demonstrates using the new MFC7 CDHtmlDialog class. A dialog with a simple HTML page is created and displayed, and events from objects within that page are handled, and the HTML within the page modified dynamically to respond to these events.

Please note that you need the new MFC libraries to compile this application. I have statically linked the demo application so you can at least see the new class in action.

Creating the Application

The sample application is based on the MFC AppWizard. To create the project, use the wizard to create a standard MFC dialog and in the Application Type property page ensure that you choose dialog based, and check the Use HTML dialog. An application will be created with the main dialog derived from CDHtmlDialog. A resource containing a HTML page will also be created, and it's this page that will be displayed in the dialog at execution.

The HTML designer in Visual Studio allows you to edit the HTML. Each element on the HTML should be given an ID so that it can be accessed from within your CDHtmlDialog derived class.

HTML Element events

To catch events fired by HTML elements (such as mouse clicks on buttons) you must add an entry to the dialog's DHTML event map. This is analogous to adding message handlers for normal windows controls:

    DHTML_EVENT_ONCLICK(_T("ButtonOK"), OnButtonOK)
    DHTML_EVENT_ONCLICK(_T("ButtonCancel"), OnButtonCancel)
    DHTML_EVENT_ONCLICK(_T("CheckLink"), OnCheckClick)

Our HTML page contains 2 buttons (OK, with ID ButtonOK and Cancel, with ID ButtonCancel) and a checkbox (ID CheckLink). We will use the checkbox to enable/disable a hyperlink element (ID LinkCP) on the same page.

The DHTML event map shown above associates click events for the buttons and check boxes with member functions of the dialog. The OK and Cancel button handlers simply call base class members that close the dialog. The OnCheckClick handler is called when the checkbox in the HTML page is clicked.

To make the check box determine the state of the hyperlink we catch the click event for the checkbox and replace the outer HTML of the hyperlink (the outer HTML is the HTML within the hyperlink plus the open and closing tags). For a non-active state we replace the out HTML with plain text, and for an active state we insert the <a href=...> tag.

Our click event handler function looks like the following. We use a member variable m_LinkActive to keep track of the state of the link.

<a>HRESULT CDHTMLDialogDlg::OnCheckClick(IHTMLElement* pElement)
    // toggle the link's active state
    m_LinkActive = !m_LinkActive;

    // we need to get an interface pointer to the link element
    IHTMLElement* pLinkElement = NULL;

    if (GetElement(_T("LinkCP"), &pLinkElement) == S_OK && 
          pLinkElement != NULL)
        // For an active link, set the outerHTML as the appropriate <a ...> tag
        if (m_LinkActive)
            pLinkElement->put_outerHTML(_bstr_t("<a ID=LinkCP target=_blank href=''>here</a>"));
        // For an inactive link, replace the outerHTML with grey text
            pLinkElement->put_outerHTML(_bstr_t("<font ID=LinkCP color='#COCOCO'>here</font>"));

        pLinkElement->Release();  // Thanks Heath

    return S_OK;


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 CodeProject
Canada Canada
Current passion project: CodeProject SenseAI. Please join in!

Chris is the Co-founder of the popular code-sharing site, the digital advertising agency and the content marketing agency ContentLab.IO.

He's been programming way too long and has been, in various guides, an astrophysicist, mechanic, mathematician, physicist, breeder of carnivorous plants, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a serial entrepreneur.

Chris has programmed everything from FORTRAN on a CRAY to Python on a Pi, but generally leans on TypeScript, C#, and SQL for the front, middle and back bits of his applications. His current focus is on ensuring developers know enough about Artificial Intelligence to be dangerous.

Comments and Discussions

QuestionGetting file path using <input name="File" id="File" type="file" accept=".gif, .jpg, .jpeg, .png"> Pin
jung-kreidler9-Sep-19 23:51
Memberjung-kreidler9-Sep-19 23:51 
QuestionHow can I make the dialog fit the size of different webpages? Pin
namilantu30-May-16 22:44
Membernamilantu30-May-16 22:44 
QuestionHow to add resources (HTML and images) from dll in CDhtmlDialog Pin
devesh.tyagi29-Jul-14 19:48
Memberdevesh.tyagi29-Jul-14 19:48 
QuestionHow to run an HTML page dynamically Pin
Javed Akhtar Ansari19-Apr-10 19:51
MemberJaved Akhtar Ansari19-Apr-10 19:51 
Just wanted to add one more tip here, which I needed for my project. If you want to show an HTML page at run time instead of what was included in the HTML resource use the Navigate() member function of CDHTMLDialog may be in the OnInitDialog().
Like Navigate(_T("c:\\Test\\myHtml.html"));
or Navigate(_T(""));
Javed A Ansari
Software Developer
Hyderabad, India

General不错 Pin
ylwnet118-Jul-08 16:18
Memberylwnet118-Jul-08 16:18 
QuestionHow did you add the member function "HRESULT CDHTMLDialogDlg::OnCheckClick(IHTMLElement* pElement)"? Pin
grin_t10-Jul-08 2:08
Membergrin_t10-Jul-08 2:08 
GeneralProblem when the page containing javascript Pin
Shailk13-Mar-08 23:11
MemberShailk13-Mar-08 23:11 
GeneralRe: Problem when the page containing javascript Pin
dimsik27-Aug-08 2:31
Memberdimsik27-Aug-08 2:31 
GeneralRe: Problem when the page containing javascript Pin
Member 453916524-Feb-10 1:32
MemberMember 453916524-Feb-10 1:32 
GeneralRe: Problem when the page containing javascript Pin
wetwilly10121-Mar-12 6:11
Memberwetwilly10121-Mar-12 6:11 
QuestionImage fail to load in Vista Pin
John Tan Jin Kiat28-Nov-07 20:58
MemberJohn Tan Jin Kiat28-Nov-07 20:58 
AnswerRe: Image fail to load in Vista Pin
John Tan Jin Kiat29-Nov-07 14:55
MemberJohn Tan Jin Kiat29-Nov-07 14:55 
GeneralRe: Image fail to load in Vista Pin
Sinekethan2-Jul-14 15:38
MemberSinekethan2-Jul-14 15:38 
GeneralHowto stop context menu and F5 Pin
jung-kreidler2-May-07 2:37
Memberjung-kreidler2-May-07 2:37 
GeneralRe: Howto stop context menu and F5 Pin
Cristian Amarie31-May-08 8:09
MemberCristian Amarie31-May-08 8:09 
Generalclick() method Pin
Sirgothivan1-Dec-05 6:00
MemberSirgothivan1-Dec-05 6:00 
GeneralRe: click() method Pin
valsorbod22-Jan-06 10:48
Membervalsorbod22-Jan-06 10:48 
GeneralAdd methods obtainable by a script Pin
jmoses15-Jun-05 1:03
Memberjmoses15-Jun-05 1:03 
GeneralRe: Add methods obtainable by a script Pin
Amit220326-Nov-06 4:28
MemberAmit220326-Nov-06 4:28 
QuestionOnClick for Hyperlinks? Pin
figmo27-Apr-05 0:20
Memberfigmo27-Apr-05 0:20 
GeneralCDhtmlDialog With CStatusBar Pin
-R-J-9-Mar-05 4:10
Member-R-J-9-Mar-05 4:10 
Generalremove ie shortcut features Pin
pluto6625-Oct-04 5:54
Memberpluto6625-Oct-04 5:54 
GeneralNeed help!! Is there any way to find result code after CDHtmlDialog's Navigate method? ThanX Pin
dimak19-Apr-04 2:47
Memberdimak19-Apr-04 2:47 
QuestionHow to remove all browser features not suited for a dialog? Pin
Member 49676331-Jul-03 13:03
MemberMember 49676331-Jul-03 13:03 
AnswerRe: How to remove all browser features not suited for a dialog? Pin
Ianier Munoz6-Oct-04 3:29
MemberIanier Munoz6-Oct-04 3:29 

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.