Click here to Skip to main content
4.06 / 5, 28 votes
1 vote, 12.5%
1
1 vote, 12.5%
2
1 vote, 12.5%
3
4 votes, 50.0%
4
1 vote, 12.5%
5

The MFC CDHtmlDialog class

By Chris Maunder | 11 Sep 2001
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.

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;
}

License

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 for this article
Add a reason or comment to your vote:

Comments and Discussions

You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 26 to 50 of 63 (Total in Forum: 63) (Refresh)FirstPrevNext
GeneralRe: get_innerText method PinmemberM. Kale23:29 9 Oct '03  
GeneralRe: get_innerText method Pinmemberquoi3511:53 13 Dec '03  
GeneralRe: get_innerText method PinsussAnonymous18:56 14 Nov '04  
GeneralRe: get_innerText method Pinmemberachims.@gmx.net13:29 24 Jul '08  
GeneralASP.NET PinmemberEsteves9:30 1 Jan '03  
GeneralEvent identifier Pinsussjoblemarq5:27 20 Dec '02  
GeneralReleasing Interfaces PinmemberHeath Stewart10:57 20 Nov '02  
GeneralInternet Explorer 5.5 or higer PinmemberAnonymous18:28 7 Jul '02  
GeneralHow to disable manual F5 refresh? Pinmemberelectricninja16:31 22 May '02  
GeneralRe: How to disable manual F5 refresh? PinmemberBill Brooks11:25 8 Jul '02  
GeneralRe: How to disable manual F5 refresh? PinsussAnonymous4:41 3 Sep '02  
GeneralEmbedding the image PinmemberPhilG5:19 19 Feb '02  
GeneralRe: Embedding the image PinsussAnonymous3:49 3 Sep '02  
This worked for me, and you don't need to know the exe name.

<IMG alt="" src="res:\#2/#1">

The ID of the Image I inserted was 1.
GeneralSelecting Text with CDHtmlDialog PinmemberJR Furman11:18 18 Dec '01  
GeneralUpdated eh? PinmemberErik Thompson16:36 14 Sep '01  
GeneralGreat article PinmemberZurren10:30 4 Sep '01  
GeneralNow, EXE works. PinmemberMasaaki Onishi17:33 4 May '01  
GeneralRe: Now, EXE works. PinmemberChristian Graus19:07 4 May '01  
GeneralUpdated download PinmemberChris Maunder15:08 4 May '01  
Generalnice rating PinmemberAnonymous10:49 4 May '01  
GeneralRe: nice rating PinmemberChris Meech11:44 4 May '01  
GeneralRe: nice rating PinmemberChris Maunder14:53 4 May '01  
GeneralRe: nice rating PinmemberAnonymous23:22 7 May '01  
Generalpoor article PinmemberAnonymous10:45 4 May '01  
GeneralRe: poor article PinmemberChris Maunder14:55 4 May '01  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 11 Sep 2001

Copyright 2001 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project