Click here to Skip to main content
Click here to Skip to main content

Handling HTML Element Events in CHtmlView and Reusing CDHtmlDialog Serial Class

, 9 Jun 2005
Rate this:
Please Sign up or sign in to vote.
The enhanced CHtmlView class handles the HTML element event and exchanges data in the view.

Foreword

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

Share

About the Author

luo31
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? PinmemberPRMARJORAM15-May-09 6:00 
GeneralPlease help for CDHtmlDialog PinmemberATS++2-Jun-08 8:27 
Answercode of getting links Pinmemberxinguomindang8-Apr-08 20:29 
Generalget a version for vc6.0 Pinmemberyuguichong2-Sep-07 22:39 
GeneralVery good , thanks Pinmemberevgesha14-Aug-07 8:52 
Questionhow to get input text ? Pinmemberfor3h11-May-07 2:05 
AnswerRe: how to get input text ? PinmemberBilliamR15-Oct-07 7:55 
Generala problem about the use of CDHtmlViewSpec class Pinmemberchoulery10-Dec-06 22:00 
GeneralApplet in WebBrowser Pinmembercolin-12313-Sep-06 1:26 
QuestionNeed Help on Http Header to add my own User-Agent Pinmemberrex_chen13-May-06 7:59 
I use CDHTMLDialog to program a simple web browser, and I had a trobule on Http Header modify.
I want add my own "User-Agent" when client use my application to browse web.
 
//------------------------------------------------------------------------------------------------
#define WM_NVTO            (WM_USER+1000)
 
class NvToParam
{
public:
      CString URL;
      DWORD Flags;
      CString TargetFrameName;
      CByteArray PostedData;
      CString Headers;
      DWORD dwPostDataLen;
};
//------------------------------------------------------------------------------------------------
LRESULT Ccdhtml_t1Dlg::OnNvTo(WPARAM wParam, LPARAM lParam)
{
      NvToParam* pNvTo = (NvToParam*)wParam;
     
      CDHtmlDialog::Navigate((LPCTSTR)pNvTo->URL, pNvTo->Flags, (LPCTSTR)pNvTo->TargetFrameName, (LPCTSTR)pNvTo->Headers, pNvTo->PostedData.GetData());
 
      delete pNvTo;
      return 1;
}
 
//------------------------------------------------------------------------------------------------
void Ccdhtml_t1Dlg::OnBeforeNavigate2(LPDISPATCH pDisp, VARIANT FAR* URL, VARIANT FAR* Flags, VARIANT FAR* TargetFrameName, VARIANT FAR* PostData, VARIANT FAR* Headers, BOOL FAR* Cancel)
{
            CString strHeaders(V_BSTR(Headers));
    
     // Check User-Agent           
     if(strHeaders.Find("User-Agent:LHPBrowser 1.0") < 0)
            {
                  *Cancel = TRUE; // Cancel this request
 
                  if(!strHeaders.IsEmpty())
                        strHeaders += "\r\n";
           // Add Customize Http Header
                  strHeaders += "User-Agent:LHPBrowser 1.0";
 
                  NvToParam* pNvTo = new NvToParam;
                  ASSERT(V_VT(URL) == VT_BSTR);
                  ASSERT(V_VT(TargetFrameName) == VT_BSTR);
                  ASSERT(V_VT(PostData) == (VT_VARIANT | VT_BYREF));
                  ASSERT(V_VT(Headers) == VT_BSTR);
                  ASSERT(Cancel != NULL);
 
                  USES_CONVERSION;
 
                  VARIANT* vtPostedData = V_VARIANTREF(PostData);
                  CByteArray array;
                  if (V_VT(vtPostedData) & VT_ARRAY)
                  {
                        // must be a vector of bytes
                        ASSERT(vtPostedData->parray->cDims == 1 && vtPostedData->parray->cbElements == 1);
 
                        vtPostedData->vt |= VT_UI1;
                        COleSafeArray safe(vtPostedData);
 
                        DWORD dwSize = safe.GetOneDimSize();
 
                        LPVOID pVoid;
                        safe.AccessData(&pVoid);
 
                        array.SetSize(dwSize);
                        LPBYTE lpByte = array.GetData();
 
                        memcpy(lpByte, pVoid, dwSize);
 
                        safe.UnaccessData();
                  }
                  // make real parameters out of the notification
 
                  CString strTargetFrameName(V_BSTR(TargetFrameName));
                  CString strURL(V_BSTR(URL));
                  DWORD nFlags = V_I4(Flags);
 
                  //=====================================================
                  pNvTo->URL = strURL;
                  pNvTo->Flags = nFlags;
                  pNvTo->TargetFrameName = strTargetFrameName;
                  pNvTo->PostedData.Copy(array);
                  pNvTo->Headers = strHeaders;
                  //=====================================================
                 
                  // Post msg
                  PostMessage(WM_NVTO,(WPARAM)pNvTo);
 
                  return;
            }
      CDHtmlDialog::_OnBeforeNavigate2( pDisp, URL, Flags, TargetFrameName, PostData, Headers, Cancel );                       
}
 
----------------------------------------------------------
After the testing, I found it will work fine on "http reqest", "http form get" and "http form post".
But if my http form is "multipart/form-data" (upload file form), some of the post datas will lost.
 
How should I fix it? Does any one can help me? Or any better method to add http header when navigate a url?
GeneralVery good article,nice job PinsussAnonymous30-Sep-05 7:10 
GeneralNeed it to work for VC 6 Pinmemberandrew.truckle@atkinsglobal.com21-Sep-05 2:01 
Generalsink frameset events Pinmemberhecchan23-Jun-05 0:40 
GeneralGood article PinmemberPerry Zh19-May-05 19:11 
Generalthe demo is damaged. Pinmemberprog coney16-May-05 16:19 
GeneralRe: the demo is damaged. Pinmemberluo3116-May-05 19:16 
GeneralWow PinsussAnonymous16-May-05 6:24 
GeneralRe: Wow Pinmemberluo3116-May-05 15:36 
GeneralRe: Wow PinstaffSmitha Vijayan18-May-05 19:09 

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
Web01 | 2.8.141223.1 | Last Updated 9 Jun 2005
Article Copyright 2005 by luo31
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid