Click here to Skip to main content
11,704,509 members (58,663 online)
Click here to Skip to main content

The MFC CDHtmlDialog class

, 11 Sep 2001 CPOL 367.7K 7.6K 72
Rate this:
Please Sign up or sign in to vote.
This sample demonstrates using the MFC CDHtmlDialog class in MFC7
<!-- Download Links --> <!-- Article image -->

Sample Image - DHTMLDialog.gif

<!-- Main HTML starts here -->

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

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

 
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 
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 
Add in h:

STDMETHOD(ShowContextMenu)(DWORD dwID, POINT *ppt, IUnknown *pcmdtReserved, IDispatch *pdispReserved);
STDMETHOD(TranslateAccelerator)(LPMSG lpMsg, const GUID * pguidCmdGroup, DWORD nCmdID);

Add in cpp:

STDMETHODIMP ChtmldialogDlg::ShowContextMenu(DWORD dwID, POINT *ppt, IUnknown *pcmdtReserved, IDispatch *pdispReserved)
{
//return CDHtmlDialog::ShowContextMenu(dwID, ppt, pcmdtReserved, pdispReserved);
return S_OK;
}

STDMETHODIMP ChtmldialogDlg::TranslateAccelerator(LPMSG lpMsg, const GUID * pguidCmdGroup, DWORD nCmdID)
{
if (lpMsg && lpMsg->message == WM_KEYDOWN && lpMsg->wParam == VK_F5) {
return S_OK;
}
return CDHtmlDialog::TranslateAccelerator(lpMsg, pguidCmdGroup, nCmdID);
}

Greetings,
jung-kreidler
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
Dobroslav Kolev22-Jan-06 10:48
memberDobroslav Kolev22-Jan-06 10:48 
GeneralAdd methods obtainable by a script Pin
JMoses15-Jun-05 1:03
sussJMoses15-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
sussfigmo27-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
Magnus Enarsson31-Jul-03 13:03
memberMagnus Enarsson31-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 
GeneralRe: How to remove all browser features not suited for a dialog? Pin
Ianier Munoz6-Oct-04 3:43
memberIanier Munoz6-Oct-04 3:43 
GeneralRe: How to remove all browser features not suited for a dialog? Pin
MMs_xH4-Apr-06 22:38
memberMMs_xH4-Apr-06 22:38 
AnswerI make it. Pin
iTreeSoft28-Oct-04 16:11
memberiTreeSoft28-Oct-04 16:11 
GeneralPrinting the dialog contents Pin
Ed K23-Jun-03 4:39
memberEd K23-Jun-03 4:39 
Generalget_innerText method Pin
Majid Shahabfar10-Jun-03 20:07
memberMajid Shahabfar10-Jun-03 20:07 
GeneralRe: get_innerText method Pin
M. Kale9-Oct-03 22:29
memberM. Kale9-Oct-03 22:29 
GeneralRe: get_innerText method Pin
quoi3513-Dec-03 10:53
memberquoi3513-Dec-03 10:53 
GeneralRe: get_innerText method Pin
Anonymous14-Nov-04 17:56
sussAnonymous14-Nov-04 17:56 
GeneralRe: get_innerText method Pin
achims.@gmx.net24-Jul-08 12:29
memberachims.@gmx.net24-Jul-08 12:29 
GeneralASP.NET Pin
Esteves1-Jan-03 8:30
memberEsteves1-Jan-03 8:30 
GeneralEvent identifier Pin
joblemarq20-Dec-02 4:27
sussjoblemarq20-Dec-02 4:27 
GeneralReleasing Interfaces Pin
Heath Stewart20-Nov-02 9:57
memberHeath Stewart20-Nov-02 9:57 
GeneralInternet Explorer 5.5 or higer Pin
Anonymous7-Jul-02 17:28
memberAnonymous7-Jul-02 17:28 
QuestionHow to disable manual F5 refresh? Pin
electricninja22-May-02 15:31
memberelectricninja22-May-02 15:31 
AnswerRe: How to disable manual F5 refresh? Pin
Bill Brooks8-Jul-02 10:25
memberBill Brooks8-Jul-02 10:25 
GeneralRe: How to disable manual F5 refresh? Pin
Anonymous3-Sep-02 3:41
sussAnonymous3-Sep-02 3:41 
GeneralEmbedding the image Pin
PhilG19-Feb-02 4:19
memberPhilG19-Feb-02 4:19 
GeneralRe: Embedding the image Pin
Anonymous3-Sep-02 2:49
sussAnonymous3-Sep-02 2:49 
GeneralSelecting Text with CDHtmlDialog Pin
JR Furman18-Dec-01 10:18
memberJR Furman18-Dec-01 10:18 
QuestionUpdated eh? Pin
Erik Thompson14-Sep-01 15:36
memberErik Thompson14-Sep-01 15:36 
GeneralGreat article Pin
Zurren4-Sep-01 9:30
memberZurren4-Sep-01 9:30 
GeneralNow, EXE works. Pin
Masaaki Onishi4-May-01 16:33
memberMasaaki Onishi4-May-01 16:33 
GeneralRe: Now, EXE works. Pin
Christian Graus4-May-01 18:07
memberChristian Graus4-May-01 18:07 
GeneralUpdated download Pin
Chris Maunder4-May-01 14:08
memberChris Maunder4-May-01 14:08 
Generalnice rating Pin
Anonymous4-May-01 9:49
memberAnonymous4-May-01 9:49 
GeneralRe: nice rating Pin
Chris Meech4-May-01 10:44
memberChris Meech4-May-01 10:44 
GeneralRe: nice rating Pin
Chris Maunder4-May-01 13:53
memberChris Maunder4-May-01 13:53 

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.

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