Click here to Skip to main content
15,880,972 members
Articles / Desktop Programming / MFC
Article

The MFC CDHtmlDialog class

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

Sample Image - DHTMLDialog.gif

Introduction

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:

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

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='http://www.codeproject.com'>here</a>"));
        }
        // For an inactive link, replace the outerHTML with grey text
        else
        {
            pLinkElement->put_outerHTML(_bstr_t("<font ID=LinkCP color='#COCOCO'>here</font>"));
        }

        pLinkElement->Release();  // Thanks Heath
    }

    return S_OK;
}
</a>

License

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


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions

 
GeneralRe: nice rating Pin
Chris Meech4-May-01 10:44
Chris Meech4-May-01 10:44 
GeneralRe: nice rating Pin
Chris Maunder4-May-01 13:53
cofounderChris Maunder4-May-01 13:53 
GeneralRe: nice rating Pin
7-May-01 22:22
suss7-May-01 22:22 
Generalpoor article Pin
4-May-01 9:45
suss4-May-01 9:45 
GeneralRe: poor article Pin
Chris Maunder4-May-01 13:55
cofounderChris Maunder4-May-01 13:55 
GeneralRe: Haha! Pin
Masaaki Onishi4-May-01 16:41
Masaaki Onishi4-May-01 16:41 
GeneralRe: Haha! Pin
4-May-01 18:12
suss4-May-01 18:12 
GeneralRe: Haha! Pin
5-May-01 3:26
suss5-May-01 3:26 
GeneralRe: Thank you, Chris, but... Pin
Masaaki Onishi5-May-01 4:24
Masaaki Onishi5-May-01 4:24 
GeneralRe: poor article Pin
Cathy12-Sep-01 12:49
Cathy12-Sep-01 12:49 
GeneralRe: poor article Pin
Selvam R3-Feb-04 7:04
professionalSelvam R3-Feb-04 7:04 
GeneralJust what I need, but... Pin
4-May-01 3:49
suss4-May-01 3:49 
GeneralRe: Just what I need, but... Pin
Pete Bassett4-May-01 4:39
Pete Bassett4-May-01 4:39 
GeneralRe: afxdhtml.h wanted. Pin
Masaaki Onishi4-May-01 5:36
Masaaki Onishi4-May-01 5:36 
GeneralRe: afxdhtml.h wanted. Pin
Miguel Lopes1-Apr-02 0:35
Miguel Lopes1-Apr-02 0:35 
GeneralRe: Just what I need, but... Pin
Alexander Fedorov8-May-01 13:01
Alexander Fedorov8-May-01 13:01 
GeneralCan't execute! Pin
Masaaki Onishi4-May-01 3:45
Masaaki Onishi4-May-01 3:45 
GeneralRe: Can't execute! Pin
Walter Sullivan4-May-01 9:06
Walter Sullivan4-May-01 9:06 

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.