Click here to Skip to main content
15,867,568 members
Articles / Web Development / HTML
Article

Handling HTML Element Events in CHtmlView and Reusing CDHtmlDialog Serial Class

Rate me:
Please Sign up or sign in to vote.
4.75/5 (18 votes)
9 Jun 2005 539.1K   5.8K   58   19
The enhanced CHtmlView class handles the HTML element event and exchanges data in the view.

Foreword

Image 1

CDHtmlSpecEventSink and CDHtmlViewSpec are very cool, they give you the macro that deals with the document event. Please have a look at the MSDN article: How to sink HTML document events for WebBrowser host.

Structure class CDHtmlViewSpec

CDHtmlDialog class inherits directly from CDHtmlEventSink class, CDHtmlViewSpec class does the same. The content of CDHtmlSpecEventSink class is given below:

class CDHtmlSpecEventSink: public CDHtmlEventSink
{
    public:
    
    //implement abstrace method
    virtual const DHtmlEventMapEntry* GetDHtmlEventMap();
    virtual BOOL DHtmlEventHook(HRESULT *phr, DISPID dispIdMember, 
            DISPPARAMS *pDispParams, VARIANT *pVarResult, 
            EXCEPINFO *pExcepInfo, UINT *puArgErr);
    virtual HRESULT GetDHtmlDocument(IHTMLDocument2 **pphtmlDoc);
    
    //struct
    CDHtmlSpecEventSink();
    //give the interface to IHTMLDocument2,and 
    //I can't find reference in CDHtmlDialog 
    void InitEventSink(IHTMLDocument2* pDoc);
    
    protected:
    IHTMLDocument2* m_pDocHtml;
    CHtmlView* m_pView;
};
CDHtmlViewSpec:
class CDHtmlViewSpec : public CHtmlView,
                       public CDHtmlSpecEventSink
{
    //add releated function and copy 
    //the code from CDHtmlDialog
};

Copy your function from CDHtmlDialog when you want to modify.

Use class CDHtmlViewSpec

Using this class is very simple. Inherit a class directly from CHtmlView, then change CHtmlView class to CDHtmlViewSpec. Add the events and DDx. Please go through CDHtmlDialog's help. If you don't want to look at the help, refer to the code given below:

Add events support

Add DECLARE_DHTML_EVENT_MAP() into your HtmlView header file.
      //Add the code into your htmlView Implement file:
        BEGIN_DHTML_EVENT_MAP(ChtmlExView)
           DHTML_EVENT_ONCLICK(_T("btnEnd"), OnButtonEnd)
        END_DHTML_EVENT_MAP()
      //and then add OnButtonEnd function.

Add data exchange support

Add the code into the DoDataExchange function:

void ChtmlExView::DoDataExchange(CDataExchange* pDX)
     {
         CDHtmlViewSpec::DoDataExchange(pDX);
         //support radio
         DDX_DHtml_Radio(pDX,_T("btnControl"),m_iControl);
     }

About the author and the code

If you have any questions or suggestions, please email me at (luomh AT haitai.Com.Cn). You are free to use and revise this class. If your Chinese is good, please visit my website. Thanks!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
If your chinese is good,pls visit my website:http://blog.mvpcn.net/luo31/.

Comments and Discussions

 
QuestionWhere is the tutorial? Pin
PRMARJORAM15-May-09 5:00
PRMARJORAM15-May-09 5:00 
GeneralPlease help for CDHtmlDialog Pin
ATS++2-Jun-08 7:27
ATS++2-Jun-08 7:27 
Answercode of getting links Pin
xinguomindang8-Apr-08 19:29
xinguomindang8-Apr-08 19:29 
Generalget a version for vc6.0 Pin
yuguichong2-Sep-07 21:39
yuguichong2-Sep-07 21:39 
GeneralVery good , thanks Pin
evgesha14-Aug-07 7:52
evgesha14-Aug-07 7:52 
Questionhow to get input text ? Pin
for3h11-May-07 1:05
for3h11-May-07 1:05 
AnswerRe: how to get input text ? Pin
BilliamR15-Oct-07 6:55
BilliamR15-Oct-07 6:55 
Generala problem about the use of CDHtmlViewSpec class Pin
choulery10-Dec-06 21:00
choulery10-Dec-06 21:00 
I used this class to implement my application. but, I encounter a problem, how can I dynamicl add the options from the mfc code to the dropdown(like the combobox) control of the html? On the other words, I must get the data from database and initial to show to the html interface. Such as,I would add "option1"/"option2" and "option3" to the list of the control, What to do?
GeneralApplet in WebBrowser Pin
colin-12313-Sep-06 0:26
colin-12313-Sep-06 0:26 
QuestionNeed Help on Http Header to add my own User-Agent Pin
rex_chen13-May-06 6:59
rex_chen13-May-06 6:59 
GeneralVery good article,nice job Pin
Anonymous30-Sep-05 6:10
Anonymous30-Sep-05 6:10 
GeneralNeed it to work for VC 6 Pin
andrewtruckle21-Sep-05 1:01
andrewtruckle21-Sep-05 1:01 
Generalsink frameset events Pin
hecchan22-Jun-05 23:40
hecchan22-Jun-05 23:40 
GeneralGood article Pin
Mingliang Zhu19-May-05 18:11
Mingliang Zhu19-May-05 18:11 
Generalthe demo is damaged. Pin
Member 148694216-May-05 15:19
Member 148694216-May-05 15:19 
GeneralRe: the demo is damaged. Pin
luo3116-May-05 18:16
luo3116-May-05 18:16 
GeneralWow Pin
Anonymous16-May-05 5:24
Anonymous16-May-05 5:24 
GeneralRe: Wow Pin
luo3116-May-05 14:36
luo3116-May-05 14:36 
GeneralRe: Wow Pin
Smitha Nishant18-May-05 18:09
protectorSmitha Nishant18-May-05 18:09 

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.