• Duilib中Webbrowser事件完善使其支持判断页面加载完毕


    在多iframe的页面中,需要结合DISPID_DOCUMENTCOMPLETE和DISPID_NAVIGATECOMPLETE2两个事件判断页面是否加载完毕,而duilib中没有提供对DISPID_DOCUMENTCOMPLETE的支持。

    要支持DISPID_DOCUMENTCOMPLETE,需要修改UIWebBrowser.cpp、UIWebBrowser.h和WebBrowserEventHandler.h三个文件

    1.WebBrowserEventHandler.h文件

    添加virtual void DocumentComplete();

      1 #pragma once
      2 #include <ExDisp.h>
      3 #include <ExDispid.h>
      4 #include <mshtmhst.h>
      5 
      6 namespace DuiLib
      7 {
      8     class CWebBrowserEventHandler
      9     {
     10     public:
     11         CWebBrowserEventHandler() {}
     12         ~CWebBrowserEventHandler() {}
     13 
     14         virtual void BeforeNavigate2( IDispatch *pDisp,VARIANT *&url,VARIANT *&Flags,VARIANT *&TargetFrameName,VARIANT *&PostData,VARIANT *&Headers,VARIANT_BOOL *&Cancel ) {}
     15         virtual void NavigateError(IDispatch *pDisp,VARIANT * &url,VARIANT *&TargetFrameName,VARIANT *&StatusCode,VARIANT_BOOL *&Cancel) {}
     16         virtual void NavigateComplete2(IDispatch *pDisp,VARIANT *&url){}
     17         virtual void DocumentComplete(){} //2016.10.19
     18         virtual void ProgressChange(LONG nProgress, LONG nProgressMax){}
     19         virtual void NewWindow3(IDispatch **pDisp, VARIANT_BOOL *&Cancel, DWORD dwFlags, BSTR bstrUrlContext, BSTR bstrUrl){}
     20         virtual void CommandStateChange(long Command,VARIANT_BOOL Enable){};
     21 
     22         // interface IDocHostUIHandler
     23         virtual HRESULT STDMETHODCALLTYPE ShowContextMenu(
     24             /* [in] */ DWORD dwID,
     25             /* [in] */ POINT __RPC_FAR *ppt,
     26             /* [in] */ IUnknown __RPC_FAR *pcmdtReserved,
     27             /* [in] */ IDispatch __RPC_FAR *pdispReserved)
     28         {
     29             //return E_NOTIMPL;
     30             //返回 E_NOTIMPL 正常弹出系统右键菜单
     31             return S_OK;
     32             //返回S_OK 则可屏蔽系统右键菜单
     33         }
     34 
     35         virtual HRESULT STDMETHODCALLTYPE GetHostInfo(
     36             /* [out][in] */ DOCHOSTUIINFO __RPC_FAR *pInfo)
     37         {
     38             //         if (pInfo != NULL)
     39             //         {
     40             //             pInfo->dwFlags |= DOCHOSTUIFLAG_NO3DBORDER;
     41             //         }
     42             return S_OK;
     43         }
     44 
     45         virtual HRESULT STDMETHODCALLTYPE ShowUI(
     46             /* [in] */ DWORD dwID,
     47             /* [in] */ IOleInPlaceActiveObject __RPC_FAR *pActiveObject,
     48             /* [in] */ IOleCommandTarget __RPC_FAR *pCommandTarget,
     49             /* [in] */ IOleInPlaceFrame __RPC_FAR *pFrame,
     50             /* [in] */ IOleInPlaceUIWindow __RPC_FAR *pDoc)
     51         {
     52             return S_FALSE;
     53         }
     54 
     55         virtual HRESULT STDMETHODCALLTYPE HideUI( void)
     56         {
     57             return S_OK;
     58         }
     59 
     60         virtual HRESULT STDMETHODCALLTYPE UpdateUI( void)
     61         {
     62             return S_OK;
     63         }
     64 
     65         virtual HRESULT STDMETHODCALLTYPE EnableModeless(
     66             /* [in] */ BOOL fEnable)
     67         {
     68             return S_OK;
     69         }
     70 
     71         virtual HRESULT STDMETHODCALLTYPE OnDocWindowActivate(
     72             /* [in] */ BOOL fActivate)
     73         {
     74             return S_OK;
     75         }
     76 
     77         virtual HRESULT STDMETHODCALLTYPE OnFrameWindowActivate(
     78             /* [in] */ BOOL fActivate)
     79         {
     80             return S_OK;
     81         }
     82 
     83         virtual HRESULT STDMETHODCALLTYPE ResizeBorder(
     84             /* [in] */ LPCRECT prcBorder,
     85             /* [in] */ IOleInPlaceUIWindow __RPC_FAR *pUIWindow,
     86             /* [in] */ BOOL fRameWindow)
     87         {
     88             return S_OK;
     89         }
     90 
     91         virtual HRESULT STDMETHODCALLTYPE TranslateAccelerator(
     92             /* [in] */ LPMSG lpMsg,
     93             /* [in] */ const GUID __RPC_FAR *pguidCmdGroup,
     94             /* [in] */ DWORD nCmdID)
     95         {
     96             return S_FALSE;
     97         }
     98 
     99         virtual HRESULT STDMETHODCALLTYPE GetOptionKeyPath(
    100             /* [out] */ LPOLESTR __RPC_FAR *pchKey,
    101             /* [in] */ DWORD dw)
    102         {
    103             return S_OK;
    104         }
    105 
    106         virtual HRESULT STDMETHODCALLTYPE GetDropTarget(
    107             /* [in] */ IDropTarget __RPC_FAR *pDropTarget,
    108             /* [out] */ IDropTarget __RPC_FAR *__RPC_FAR *ppDropTarget)
    109         {
    110             return E_NOTIMPL;
    111         }
    112 
    113         virtual HRESULT STDMETHODCALLTYPE GetExternal(
    114             /* [out] */ IDispatch __RPC_FAR *__RPC_FAR *ppDispatch)
    115         {
    116             return E_NOTIMPL;
    117         }
    118 
    119         virtual HRESULT STDMETHODCALLTYPE TranslateUrl(
    120             /* [in] */ DWORD dwTranslate,
    121             /* [in] */ OLECHAR __RPC_FAR *pchURLIn,
    122             /* [out] */ OLECHAR __RPC_FAR *__RPC_FAR *ppchURLOut)
    123         {
    124             return S_OK;
    125         }
    126 
    127         virtual HRESULT STDMETHODCALLTYPE FilterDataObject(
    128             /* [in] */ IDataObject __RPC_FAR *pDO,
    129             /* [out] */ IDataObject __RPC_FAR *__RPC_FAR *ppDORet)
    130         {
    131             return S_OK;
    132         }
    133 
    134         //     virtual HRESULT STDMETHODCALLTYPE GetOverrideKeyPath( 
    135         //         /* [annotation][out] */ 
    136         //         __deref_out  LPOLESTR *pchKey,
    137         //         /* [in] */ DWORD dw)
    138         //     {
    139         //         return E_NOTIMPL;
    140         //     }
    141 
    142         // IDownloadManager
    143         virtual HRESULT STDMETHODCALLTYPE Download( 
    144             /* [in] */ IMoniker *pmk,
    145             /* [in] */ IBindCtx *pbc,
    146             /* [in] */ DWORD dwBindVerb,
    147             /* [in] */ LONG grfBINDF,
    148             /* [in] */ BINDINFO *pBindInfo,
    149             /* [in] */ LPCOLESTR pszHeaders,
    150             /* [in] */ LPCOLESTR pszRedir,
    151             /* [in] */ UINT uiCP)
    152         {
    153             return S_OK;
    154         }
    155     };
    156 }

    2.在UIWebBrowser.h添加

    void DocumentComplete();
      1 #ifndef __UIWEBBROWSER_H__
      2 #define __UIWEBBROWSER_H__
      3 
      4 #pragma once
      5 
      6 #include "../Utils/WebBrowserEventHandler.h"
      7 #include <ExDisp.h>
      8 
      9 namespace DuiLib
     10 {
     11     class UILIB_API CWebBrowserUI
     12         : public CActiveXUI
     13         , public IDocHostUIHandler
     14         , public IServiceProvider
     15         , public IOleCommandTarget
     16         , public IDispatch
     17         , public ITranslateAccelerator
     18     {
     19     public:
     20         /// 构造函数
     21         CWebBrowserUI();
     22         virtual ~CWebBrowserUI();
     23 
     24         void SetHomePage(LPCTSTR lpszUrl);
     25         LPCTSTR GetHomePage();
     26 
     27         void SetAutoNavigation(bool bAuto = TRUE);
     28         bool IsAutoNavigation();
     29 
     30         void SetWebBrowserEventHandler(CWebBrowserEventHandler* pEventHandler);
     31         void Navigate2(LPCTSTR lpszUrl);
     32         void Refresh();
     33         void Refresh2(int Level);
     34         void GoBack();
     35         void GoForward();
     36         void NavigateHomePage();
     37         void NavigateUrl(LPCTSTR lpszUrl);
     38         virtual bool DoCreateControl();
     39         IWebBrowser2* GetWebBrowser2(void);
     40         IDispatch*           GetHtmlWindow();
     41         static DISPID FindId(IDispatch *pObj, LPOLESTR pName);
     42         static HRESULT InvokeMethod(IDispatch *pObj, LPOLESTR pMehtod, VARIANT *pVarResult, VARIANT *ps, int cArgs);
     43         static HRESULT GetProperty(IDispatch *pObj, LPOLESTR pName, VARIANT *pValue);
     44         static HRESULT SetProperty(IDispatch *pObj, LPOLESTR pName, VARIANT *pValue);
     45 
     46     protected:
     47         IWebBrowser2*            m_pWebBrowser2; //浏览器指针
     48         IHTMLWindow2*        _pHtmlWnd2;
     49         LONG m_dwRef;
     50         DWORD m_dwCookie;
     51         virtual void ReleaseControl();
     52         HRESULT RegisterEventHandler(BOOL inAdvise);
     53         virtual void SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue);
     54         CDuiString m_sHomePage;    // 默认页面
     55         bool m_bAutoNavi;    // 是否启动时打开默认页面
     56         CWebBrowserEventHandler* m_pWebBrowserEventHandler;    //浏览器事件处理
     57 
     58         // DWebBrowserEvents2
     59         void BeforeNavigate2( IDispatch *pDisp,VARIANT *&url,VARIANT *&Flags,VARIANT *&TargetFrameName,VARIANT *&PostData,VARIANT *&Headers,VARIANT_BOOL *&Cancel );
     60         void NavigateError(IDispatch *pDisp,VARIANT * &url,VARIANT *&TargetFrameName,VARIANT *&StatusCode,VARIANT_BOOL *&Cancel);
     61         void NavigateComplete2(IDispatch *pDisp,VARIANT *&url);
     62         void ProgressChange(LONG nProgress, LONG nProgressMax);
     63         void NewWindow3(IDispatch **pDisp, VARIANT_BOOL *&Cancel, DWORD dwFlags, BSTR bstrUrlContext, BSTR bstrUrl);
     64         void CommandStateChange(long Command,VARIANT_BOOL Enable);
     65         void DocumentComplete();//201610.19
     66     public:
     67         virtual LPCTSTR GetClass() const;
     68         virtual LPVOID GetInterface( LPCTSTR pstrName );
     69 
     70         // IUnknown
     71         STDMETHOD_(ULONG,AddRef)();
     72         STDMETHOD_(ULONG,Release)();
     73         STDMETHOD(QueryInterface)(REFIID riid, LPVOID *ppvObject);
     74 
     75         // IDispatch
     76         virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount( __RPC__out UINT *pctinfo );
     77         virtual HRESULT STDMETHODCALLTYPE GetTypeInfo( UINT iTInfo, LCID lcid, __RPC__deref_out_opt ITypeInfo **ppTInfo );
     78         virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames( __RPC__in REFIID riid, __RPC__in_ecount_full(cNames ) LPOLESTR *rgszNames, UINT cNames, LCID lcid, __RPC__out_ecount_full(cNames) DISPID *rgDispId);
     79         virtual HRESULT STDMETHODCALLTYPE Invoke( DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr );
     80 
     81         // IDocHostUIHandler
     82         STDMETHOD(ShowContextMenu)(DWORD dwID, POINT* pptPosition, IUnknown* pCommandTarget, IDispatch* pDispatchObjectHit);
     83         STDMETHOD(GetHostInfo)(DOCHOSTUIINFO* pInfo);
     84         STDMETHOD(ShowUI)(DWORD dwID, IOleInPlaceActiveObject* pActiveObject, IOleCommandTarget* pCommandTarget, IOleInPlaceFrame* pFrame, IOleInPlaceUIWindow* pDoc);
     85         STDMETHOD(HideUI)();
     86         STDMETHOD(UpdateUI)();
     87         STDMETHOD(EnableModeless)(BOOL fEnable);
     88         STDMETHOD(OnDocWindowActivate)(BOOL fActivate);
     89         STDMETHOD(OnFrameWindowActivate)(BOOL fActivate);
     90         STDMETHOD(ResizeBorder)(LPCRECT prcBorder, IOleInPlaceUIWindow* pUIWindow, BOOL fFrameWindow);
     91         STDMETHOD(TranslateAccelerator)(LPMSG lpMsg, const GUID* pguidCmdGroup, DWORD nCmdID);    //浏览器消息过滤
     92         STDMETHOD(GetOptionKeyPath)(LPOLESTR* pchKey, DWORD dwReserved);
     93         STDMETHOD(GetDropTarget)(IDropTarget* pDropTarget, IDropTarget** ppDropTarget);
     94         STDMETHOD(GetExternal)(IDispatch** ppDispatch);
     95         STDMETHOD(TranslateUrl)(DWORD dwTranslate, OLECHAR* pchURLIn, OLECHAR** ppchURLOut);
     96         STDMETHOD(FilterDataObject)(IDataObject* pDO, IDataObject** ppDORet);
     97 
     98         // IServiceProvider
     99         STDMETHOD(QueryService)(REFGUID guidService, REFIID riid, void** ppvObject);
    100 
    101         // IOleCommandTarget
    102         virtual HRESULT STDMETHODCALLTYPE QueryStatus( __RPC__in_opt const GUID *pguidCmdGroup, ULONG cCmds, __RPC__inout_ecount_full(cCmds ) OLECMD prgCmds[ ], __RPC__inout_opt OLECMDTEXT *pCmdText);
    103         virtual HRESULT STDMETHODCALLTYPE Exec( __RPC__in_opt const GUID *pguidCmdGroup, DWORD nCmdID, DWORD nCmdexecopt, __RPC__in_opt VARIANT *pvaIn, __RPC__inout_opt VARIANT *pvaOut );
    104 
    105         // IDownloadManager
    106         STDMETHOD(Download)( 
    107             /* [in] */ IMoniker *pmk,
    108             /* [in] */ IBindCtx *pbc,
    109             /* [in] */ DWORD dwBindVerb,
    110             /* [in] */ LONG grfBINDF,
    111             /* [in] */ BINDINFO *pBindInfo,
    112             /* [in] */ LPCOLESTR pszHeaders,
    113             /* [in] */ LPCOLESTR pszRedir,
    114             /* [in] */ UINT uiCP);
    115 
    116         // ITranslateAccelerator
    117         // Duilib消息分发给WebBrowser
    118         virtual LRESULT TranslateAccelerator( MSG *pMsg );
    119     };
    120 } // namespace DuiLib
    121 #endif // __UIWEBBROWSER_H__

    3.UIWebBrowser.cpp添加实现

      1 #include "StdAfx.h"
      2 #include "UIWebBrowser.h"
      3 #include <atlconv.h>
      4 #include <atlcomcli.h>
      5 #include "../Utils/downloadmgr.h"
      6 #include <mshtml.h>
      7 
      8 DuiLib::CWebBrowserUI::CWebBrowserUI()
      9 : m_pWebBrowser2(NULL)
     10 , _pHtmlWnd2(NULL)
     11 , m_pWebBrowserEventHandler(NULL)
     12 , m_bAutoNavi(false)
     13 , m_dwRef(0)
     14 , m_dwCookie(0)
     15 {
     16     m_clsid=CLSID_WebBrowser;
     17     m_sHomePage.Empty();
     18 }
     19 
     20 bool DuiLib::CWebBrowserUI::DoCreateControl()
     21 {
     22     if (!CActiveXUI::DoCreateControl())
     23         return false;
     24     GetManager()->AddTranslateAccelerator(this);
     25     GetControl(IID_IWebBrowser2,(LPVOID*)&m_pWebBrowser2);
     26     if ( m_bAutoNavi && !m_sHomePage.IsEmpty())
     27     {
     28         this->Navigate2(m_sHomePage);
     29     }
     30     RegisterEventHandler(TRUE);
     31     return true;
     32 }
     33 
     34 void DuiLib::CWebBrowserUI::ReleaseControl()
     35 {
     36     m_bCreated=false;
     37     GetManager()->RemoveTranslateAccelerator(this);
     38     RegisterEventHandler(FALSE);
     39 }
     40 
     41 DuiLib::CWebBrowserUI::~CWebBrowserUI()
     42 {
     43     ReleaseControl();
     44 }
     45 
     46 STDMETHODIMP DuiLib::CWebBrowserUI::GetTypeInfoCount( UINT *iTInfo )
     47 {
     48     *iTInfo = 0;
     49     return E_NOTIMPL;
     50 }
     51 
     52 STDMETHODIMP DuiLib::CWebBrowserUI::GetTypeInfo( UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo )
     53 {
     54     return E_NOTIMPL;
     55 }
     56 
     57 STDMETHODIMP DuiLib::CWebBrowserUI::GetIDsOfNames( REFIID riid, OLECHAR **rgszNames, UINT cNames, LCID lcid,DISPID *rgDispId )
     58 {
     59     return E_NOTIMPL;
     60 }
     61 
     62 STDMETHODIMP DuiLib::CWebBrowserUI::Invoke( DISPID dispIdMember, REFIID riid, LCID lcid,WORD wFlags, DISPPARAMS* pDispParams,VARIANT* pVarResult, EXCEPINFO* pExcepInfo,UINT* puArgErr )
     63 {
     64     if ((riid != IID_NULL))
     65         return E_INVALIDARG;
     66 
     67     switch(dispIdMember)
     68     {
     69     case DISPID_BEFORENAVIGATE2:
     70         BeforeNavigate2(
     71             pDispParams->rgvarg[6].pdispVal,
     72             pDispParams->rgvarg[5].pvarVal,
     73             pDispParams->rgvarg[4].pvarVal,
     74             pDispParams->rgvarg[3].pvarVal,
     75             pDispParams->rgvarg[2].pvarVal,
     76             pDispParams->rgvarg[1].pvarVal,
     77             pDispParams->rgvarg[0].pboolVal);
     78         break;
     79     case DISPID_COMMANDSTATECHANGE:
     80         CommandStateChange(
     81             pDispParams->rgvarg[1].lVal,
     82             pDispParams->rgvarg[0].boolVal);
     83         break;
     84     case DISPID_NAVIGATECOMPLETE2:
     85         NavigateComplete2(
     86             pDispParams->rgvarg[1].pdispVal,
     87             pDispParams->rgvarg[0].pvarVal);
     88         break;
     89     case DISPID_NAVIGATEERROR:
     90         NavigateError(
     91             pDispParams->rgvarg[4].pdispVal,
     92             pDispParams->rgvarg[3].pvarVal,
     93             pDispParams->rgvarg[2].pvarVal,
     94             pDispParams->rgvarg[1].pvarVal,
     95             pDispParams->rgvarg[0].pboolVal);
     96         break;
     97     case DISPID_STATUSTEXTCHANGE:
     98         break;
     99         //      case DISPID_NEWWINDOW2:
    100         //          break;
    101     case DISPID_NEWWINDOW3:
    102         NewWindow3(
    103             pDispParams->rgvarg[4].ppdispVal,
    104             pDispParams->rgvarg[3].pboolVal,
    105             pDispParams->rgvarg[2].uintVal,
    106             pDispParams->rgvarg[1].bstrVal,
    107             pDispParams->rgvarg[0].bstrVal);
    108         break;
    109 //     case DISPID_PROPERTYCHANGE:
    110 //         if (pDispParams->cArgs>0 && pDispParams->rgvarg[0].vt == VT_BSTR) {
    111 //             TRACE(_T("PropertyChange(%s)
    "), pDispParams->rgvarg[0].bstrVal);
    112 //         }
    113 //         break;
    114 
    115     case DISPID_DOCUMENTCOMPLETE:
    116         DocumentComplete();//2016.10.19
    117         break;
    118     default:
    119         return DISP_E_MEMBERNOTFOUND;
    120     }
    121     return S_OK;
    122 }
    123 
    124 STDMETHODIMP DuiLib::CWebBrowserUI::QueryInterface( REFIID riid, LPVOID *ppvObject )
    125 {
    126     *ppvObject = NULL;
    127 
    128     if( riid == IID_IDocHostUIHandler)
    129         *ppvObject = static_cast<IDocHostUIHandler*>(this);
    130     else if( riid == IID_IDispatch)
    131         *ppvObject = static_cast<IDispatch*>(this);
    132     else if( riid == IID_IServiceProvider)
    133         *ppvObject = static_cast<IServiceProvider*>(this);
    134     else if (riid == IID_IOleCommandTarget)
    135         *ppvObject = static_cast<IOleCommandTarget*>(this);
    136 
    137     if( *ppvObject != NULL )
    138         AddRef();
    139     return *ppvObject == NULL ? E_NOINTERFACE : S_OK;
    140 }
    141 
    142 STDMETHODIMP_(ULONG) DuiLib::CWebBrowserUI::AddRef()
    143 {
    144     InterlockedIncrement(&m_dwRef); 
    145     return m_dwRef;
    146 }
    147 
    148 STDMETHODIMP_(ULONG) DuiLib::CWebBrowserUI::Release()
    149 {
    150     ULONG ulRefCount = InterlockedDecrement(&m_dwRef);
    151     return ulRefCount; 
    152 }
    153 
    154 void DuiLib::CWebBrowserUI::Navigate2( LPCTSTR lpszUrl )
    155 {
    156     if (lpszUrl == NULL)
    157         return;
    158 
    159     if (m_pWebBrowser2)
    160     {
    161         CVariant url;
    162         url.vt=VT_BSTR;
    163         url.bstrVal=T2BSTR(lpszUrl);
    164         HRESULT hr = m_pWebBrowser2->Navigate2(&url, NULL, NULL, NULL, NULL);
    165     }
    166 }
    167 
    168 void DuiLib::CWebBrowserUI::Refresh()
    169 {
    170     if (m_pWebBrowser2)
    171     {
    172         m_pWebBrowser2->Refresh();
    173     }
    174 }
    175 void DuiLib::CWebBrowserUI::GoBack()
    176 {
    177     if (m_pWebBrowser2)
    178     {
    179         m_pWebBrowser2->GoBack();
    180     }
    181 }
    182 void DuiLib::CWebBrowserUI::GoForward()
    183 {
    184     if (m_pWebBrowser2)
    185     {
    186         m_pWebBrowser2->GoForward();
    187     }
    188 }
    189 /// DWebBrowserEvents2
    190 void DuiLib::CWebBrowserUI::BeforeNavigate2( IDispatch *pDisp,VARIANT *&url,VARIANT *&Flags,VARIANT *&TargetFrameName,VARIANT *&PostData,VARIANT *&Headers,VARIANT_BOOL *&Cancel )
    191 {
    192     if (m_pWebBrowserEventHandler)
    193     {
    194         m_pWebBrowserEventHandler->BeforeNavigate2(pDisp,url,Flags,TargetFrameName,PostData,Headers,Cancel);
    195     }
    196 }
    197 
    198 void DuiLib::CWebBrowserUI::NavigateError( IDispatch *pDisp,VARIANT * &url,VARIANT *&TargetFrameName,VARIANT *&StatusCode,VARIANT_BOOL *&Cancel )
    199 {
    200     if (m_pWebBrowserEventHandler)
    201     {
    202         m_pWebBrowserEventHandler->NavigateError(pDisp,url,TargetFrameName,StatusCode,Cancel);
    203     }
    204 }
    205 
    206 void DuiLib::CWebBrowserUI::NavigateComplete2( IDispatch *pDisp,VARIANT *&url )
    207 {
    208     CComPtr<IDispatch> spDoc;   
    209     m_pWebBrowser2->get_Document(&spDoc);   
    210 
    211     if (spDoc)
    212     {   
    213         CComQIPtr<ICustomDoc, &IID_ICustomDoc> spCustomDoc(spDoc);   
    214         if (spCustomDoc)   
    215             spCustomDoc->SetUIHandler(this);   
    216     }
    217 
    218     if (m_pWebBrowserEventHandler)
    219     {
    220         m_pWebBrowserEventHandler->NavigateComplete2(pDisp,url);
    221     }
    222 }
    223 
    224 void DuiLib::CWebBrowserUI::ProgressChange( LONG nProgress, LONG nProgressMax )
    225 {
    226     if (m_pWebBrowserEventHandler)
    227     {
    228         m_pWebBrowserEventHandler->ProgressChange(nProgress,nProgressMax);
    229     }
    230 }
    231 
    232 void DuiLib::CWebBrowserUI::NewWindow3( IDispatch **pDisp, VARIANT_BOOL *&Cancel, DWORD dwFlags, BSTR bstrUrlContext, BSTR bstrUrl )
    233 {
    234     if (m_pWebBrowserEventHandler)
    235     {
    236         m_pWebBrowserEventHandler->NewWindow3(pDisp,Cancel,dwFlags,bstrUrlContext,bstrUrl);
    237     }
    238 }
    239 void DuiLib::CWebBrowserUI::CommandStateChange(long Command,VARIANT_BOOL Enable)
    240 {
    241     if (m_pWebBrowserEventHandler)
    242     {
    243         m_pWebBrowserEventHandler->CommandStateChange(Command,Enable);
    244     }
    245 }
    246 void DuiLib::CWebBrowserUI::DocumentComplete()//2016.10.19
    247 {
    248     if (m_pWebBrowserEventHandler)
    249     {
    250         m_pWebBrowserEventHandler->DocumentComplete();
    251     }
    252 }
    253 
    254 // IDownloadManager
    255 STDMETHODIMP DuiLib::CWebBrowserUI::Download( /* [in] */ IMoniker *pmk, /* [in] */ IBindCtx *pbc, /* [in] */ DWORD dwBindVerb, /* [in] */ LONG grfBINDF, /* [in] */ BINDINFO *pBindInfo, /* [in] */ LPCOLESTR pszHeaders, /* [in] */ LPCOLESTR pszRedir, /* [in] */ UINT uiCP )
    256 {
    257     if (m_pWebBrowserEventHandler)
    258     {
    259         return m_pWebBrowserEventHandler->Download(pmk,pbc,dwBindVerb,grfBINDF,pBindInfo,pszHeaders,pszRedir,uiCP);
    260     }
    261     return S_OK;
    262 }
    263 
    264 // IDocHostUIHandler
    265 STDMETHODIMP DuiLib::CWebBrowserUI::ShowContextMenu( DWORD dwID, POINT* pptPosition, IUnknown* pCommandTarget, IDispatch* pDispatchObjectHit )
    266 {
    267     if (m_pWebBrowserEventHandler)
    268     {
    269         return m_pWebBrowserEventHandler->ShowContextMenu(dwID,pptPosition,pCommandTarget,pDispatchObjectHit);
    270     }
    271     return S_FALSE;
    272 }
    273 
    274 STDMETHODIMP DuiLib::CWebBrowserUI::GetHostInfo( DOCHOSTUIINFO* pInfo )
    275 {
    276     if (m_pWebBrowserEventHandler)
    277     {
    278         return m_pWebBrowserEventHandler->GetHostInfo(pInfo);
    279     }
    280     return E_NOTIMPL;
    281 }
    282 
    283 STDMETHODIMP DuiLib::CWebBrowserUI::ShowUI( DWORD dwID, IOleInPlaceActiveObject* pActiveObject, IOleCommandTarget* pCommandTarget, IOleInPlaceFrame* pFrame, IOleInPlaceUIWindow* pDoc )
    284 {
    285     if (m_pWebBrowserEventHandler)
    286     {
    287         return m_pWebBrowserEventHandler->ShowUI(dwID,pActiveObject,pCommandTarget,pFrame,pDoc);
    288     }
    289     return S_OK;
    290 }
    291 
    292 STDMETHODIMP DuiLib::CWebBrowserUI::HideUI()
    293 {
    294     if (m_pWebBrowserEventHandler)
    295     {
    296         return m_pWebBrowserEventHandler->HideUI();
    297     }
    298     return S_OK;
    299 }
    300 
    301 STDMETHODIMP DuiLib::CWebBrowserUI::UpdateUI()
    302 {
    303     if (m_pWebBrowserEventHandler)
    304     {
    305         return m_pWebBrowserEventHandler->UpdateUI();
    306     }
    307     return S_OK;
    308 }
    309 
    310 STDMETHODIMP DuiLib::CWebBrowserUI::EnableModeless( BOOL fEnable )
    311 {
    312     if (m_pWebBrowserEventHandler)
    313     {
    314         return m_pWebBrowserEventHandler->EnableModeless(fEnable);
    315     }
    316     return E_NOTIMPL;
    317 }
    318 
    319 STDMETHODIMP DuiLib::CWebBrowserUI::OnDocWindowActivate( BOOL fActivate )
    320 {
    321     if (m_pWebBrowserEventHandler)
    322     {
    323         return m_pWebBrowserEventHandler->OnDocWindowActivate(fActivate);
    324     }
    325     return E_NOTIMPL;
    326 }
    327 
    328 STDMETHODIMP DuiLib::CWebBrowserUI::OnFrameWindowActivate( BOOL fActivate )
    329 {
    330     if (m_pWebBrowserEventHandler)
    331     {
    332         return m_pWebBrowserEventHandler->OnFrameWindowActivate(fActivate);
    333     }
    334     return E_NOTIMPL;
    335 }
    336 
    337 STDMETHODIMP DuiLib::CWebBrowserUI::ResizeBorder( LPCRECT prcBorder, IOleInPlaceUIWindow* pUIWindow, BOOL fFrameWindow )
    338 {
    339     if (m_pWebBrowserEventHandler)
    340     {
    341         return m_pWebBrowserEventHandler->ResizeBorder(prcBorder,pUIWindow,fFrameWindow);
    342     }
    343     return E_NOTIMPL;
    344 }
    345 
    346 STDMETHODIMP DuiLib::CWebBrowserUI::TranslateAccelerator( LPMSG lpMsg, const GUID* pguidCmdGroup, DWORD nCmdID )
    347 {
    348     if (m_pWebBrowserEventHandler)
    349     {
    350         return m_pWebBrowserEventHandler->TranslateAccelerator(lpMsg,pguidCmdGroup,nCmdID);
    351     }
    352     return S_FALSE;
    353 }
    354 
    355 LRESULT DuiLib::CWebBrowserUI::TranslateAccelerator( MSG *pMsg )
    356 {
    357     if(pMsg->message < WM_KEYFIRST || pMsg->message > WM_KEYLAST)
    358         return S_FALSE;
    359 
    360     if( m_pWebBrowser2 == NULL )
    361         return E_NOTIMPL;
    362 
    363     // 当前Web窗口不是焦点,不处理加速键
    364     BOOL bIsChild = FALSE;
    365     HWND hTempWnd = NULL;
    366     HWND hWndFocus = ::GetFocus();
    367 
    368     hTempWnd = hWndFocus;
    369     while(hTempWnd != NULL)
    370     {
    371         if(hTempWnd == m_hwndHost)
    372         {
    373             bIsChild = TRUE;
    374             break;
    375         }
    376         hTempWnd = ::GetParent(hTempWnd);
    377     }
    378     if(!bIsChild)
    379         return S_FALSE;
    380 
    381     IOleInPlaceActiveObject *pObj;
    382     if (FAILED(m_pWebBrowser2->QueryInterface(IID_IOleInPlaceActiveObject, (LPVOID *)&pObj)))
    383         return S_FALSE;
    384 
    385     HRESULT hResult = pObj->TranslateAccelerator(pMsg);
    386     pObj->Release();
    387     return hResult;
    388 }
    389 
    390 STDMETHODIMP DuiLib::CWebBrowserUI::GetOptionKeyPath( LPOLESTR* pchKey, DWORD dwReserved )
    391 {
    392     if (m_pWebBrowserEventHandler)
    393     {
    394         return m_pWebBrowserEventHandler->GetOptionKeyPath(pchKey,dwReserved);
    395     }
    396     return E_NOTIMPL;
    397 }
    398 
    399 STDMETHODIMP DuiLib::CWebBrowserUI::GetDropTarget( IDropTarget* pDropTarget, IDropTarget** ppDropTarget )
    400 {
    401     if (m_pWebBrowserEventHandler)
    402     {
    403         return m_pWebBrowserEventHandler->GetDropTarget(pDropTarget,ppDropTarget);
    404     }
    405     return S_FALSE;    // 使用系统拖拽
    406 }
    407 
    408 STDMETHODIMP DuiLib::CWebBrowserUI::GetExternal( IDispatch** ppDispatch )
    409 {
    410     if (m_pWebBrowserEventHandler)
    411     {
    412         return m_pWebBrowserEventHandler->GetExternal(ppDispatch);
    413     }
    414     return S_FALSE;
    415 }
    416 
    417 STDMETHODIMP DuiLib::CWebBrowserUI::TranslateUrl( DWORD dwTranslate, OLECHAR* pchURLIn, OLECHAR** ppchURLOut )
    418 {
    419     if (m_pWebBrowserEventHandler)
    420     {
    421         return m_pWebBrowserEventHandler->TranslateUrl(dwTranslate,pchURLIn,ppchURLOut);
    422     }
    423     else
    424     {
    425         *ppchURLOut = 0;
    426         return E_NOTIMPL;
    427     }
    428 }
    429 
    430 STDMETHODIMP DuiLib::CWebBrowserUI::FilterDataObject( IDataObject* pDO, IDataObject** ppDORet )
    431 {
    432     if (m_pWebBrowserEventHandler)
    433     {
    434         return m_pWebBrowserEventHandler->FilterDataObject(pDO,ppDORet);
    435     }
    436     else
    437     {
    438         *ppDORet = 0;
    439         return E_NOTIMPL;
    440     }
    441 }
    442 
    443 void DuiLib::CWebBrowserUI::SetWebBrowserEventHandler( CWebBrowserEventHandler* pEventHandler )
    444 {
    445     if ( pEventHandler!=NULL && m_pWebBrowserEventHandler!=pEventHandler)
    446         m_pWebBrowserEventHandler=pEventHandler;
    447 }
    448 
    449 void DuiLib::CWebBrowserUI::Refresh2( int Level )
    450 {
    451     CVariant vLevel;
    452     vLevel.vt=VT_I4;
    453     vLevel.intVal=Level;
    454     m_pWebBrowser2->Refresh2(&vLevel);
    455 }
    456 
    457 void DuiLib::CWebBrowserUI::SetAttribute( LPCTSTR pstrName, LPCTSTR pstrValue )
    458 {
    459     if (_tcscmp(pstrName, _T("homepage")) == 0)
    460     {
    461         m_sHomePage = pstrValue;
    462     }
    463     else if (_tcscmp(pstrName, _T("autonavi"))==0)
    464     {
    465         m_bAutoNavi = (_tcscmp(pstrValue, _T("true")) == 0);
    466     }
    467     else
    468         CActiveXUI::SetAttribute(pstrName, pstrValue);
    469 }
    470 
    471 void DuiLib::CWebBrowserUI::NavigateHomePage()
    472 {
    473     if (!m_sHomePage.IsEmpty())
    474         this->NavigateUrl(m_sHomePage);
    475 }
    476 
    477 void DuiLib::CWebBrowserUI::NavigateUrl( LPCTSTR lpszUrl )
    478 {
    479     if (m_pWebBrowser2 && lpszUrl)
    480     {
    481         m_pWebBrowser2->Navigate((BSTR)SysAllocString(T2BSTR(lpszUrl)),NULL,NULL,NULL,NULL);
    482     }
    483 }
    484 
    485 LPCTSTR DuiLib::CWebBrowserUI::GetClass() const
    486 {
    487     return _T("WebBrowserUI");
    488 }
    489 
    490 LPVOID DuiLib::CWebBrowserUI::GetInterface( LPCTSTR pstrName )
    491 {
    492     if( _tcscmp(pstrName, DUI_CTR_WEBBROWSER) == 0 ) return static_cast<CWebBrowserUI*>(this);
    493     return CActiveXUI::GetInterface(pstrName);
    494 }
    495 
    496 void DuiLib::CWebBrowserUI::SetHomePage( LPCTSTR lpszUrl )
    497 {
    498     m_sHomePage.Format(_T("%s"),lpszUrl);
    499 }
    500 
    501 LPCTSTR DuiLib::CWebBrowserUI::GetHomePage()
    502 {
    503     return m_sHomePage;
    504 }
    505 
    506 void DuiLib::CWebBrowserUI::SetAutoNavigation( bool bAuto /*= TRUE*/ )
    507 {
    508     if (m_bAutoNavi==bAuto)    return;
    509 
    510     m_bAutoNavi=bAuto;
    511 }
    512 
    513 bool DuiLib::CWebBrowserUI::IsAutoNavigation()
    514 {
    515     return m_bAutoNavi;
    516 }
    517 
    518 STDMETHODIMP DuiLib::CWebBrowserUI::QueryService( REFGUID guidService, REFIID riid, void** ppvObject )
    519 {
    520     HRESULT hr = E_NOINTERFACE;
    521     *ppvObject = NULL;
    522 
    523     if ( guidService == SID_SDownloadManager && riid == IID_IDownloadManager)
    524     {
    525         *ppvObject = this;
    526         return S_OK;
    527     }
    528 
    529     return hr;
    530 }
    531 
    532 HRESULT DuiLib::CWebBrowserUI::RegisterEventHandler( BOOL inAdvise )
    533 {
    534     CComPtr<IWebBrowser2> pWebBrowser;
    535     CComPtr<IConnectionPointContainer>  pCPC;
    536     CComPtr<IConnectionPoint> pCP;
    537     HRESULT hr = GetControl(IID_IWebBrowser2, (void**)&pWebBrowser);
    538     if (FAILED(hr))
    539         return hr;
    540     hr=pWebBrowser->QueryInterface(IID_IConnectionPointContainer,(void **)&pCPC);
    541     if (FAILED(hr))
    542         return hr;
    543     hr=pCPC->FindConnectionPoint(DIID_DWebBrowserEvents2,&pCP);
    544     if (FAILED(hr))
    545         return hr;
    546 
    547     if (inAdvise)
    548     {
    549         hr = pCP->Advise((IDispatch*)this, &m_dwCookie);
    550     }
    551     else
    552     {
    553         hr = pCP->Unadvise(m_dwCookie);
    554     }
    555     return hr; 
    556 }
    557 
    558 DISPID DuiLib::CWebBrowserUI::FindId( IDispatch *pObj, LPOLESTR pName )
    559 {
    560     DISPID id = 0;
    561     if(FAILED(pObj->GetIDsOfNames(IID_NULL,&pName,1,LOCALE_SYSTEM_DEFAULT,&id))) id = -1;
    562     return id;
    563 }
    564 
    565 HRESULT DuiLib::CWebBrowserUI::InvokeMethod( IDispatch *pObj, LPOLESTR pMehtod, VARIANT *pVarResult, VARIANT *ps, int cArgs )
    566 {
    567     DISPID dispid = FindId(pObj, pMehtod);
    568     if(dispid == -1) return E_FAIL;
    569 
    570     DISPPARAMS dispparams;
    571     dispparams.cArgs = cArgs;
    572     dispparams.rgvarg = ps;
    573     dispparams.cNamedArgs = 0;
    574     dispparams.rgdispidNamedArgs = NULL;
    575 
    576     return pObj->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dispparams, pVarResult, NULL, NULL);
    577 }
    578 
    579 HRESULT DuiLib::CWebBrowserUI::GetProperty( IDispatch *pObj, LPOLESTR pName, VARIANT *pValue )
    580 {
    581     DISPID dispid = FindId(pObj, pName);
    582     if(dispid == -1) return E_FAIL;
    583 
    584     DISPPARAMS ps;
    585     ps.cArgs = 0;
    586     ps.rgvarg = NULL;
    587     ps.cNamedArgs = 0;
    588     ps.rgdispidNamedArgs = NULL;
    589 
    590     return pObj->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &ps, pValue, NULL, NULL);
    591 }
    592 
    593 HRESULT DuiLib::CWebBrowserUI::SetProperty( IDispatch *pObj, LPOLESTR pName, VARIANT *pValue )
    594 {
    595     DISPID dispid = FindId(pObj, pName);
    596     if(dispid == -1) return E_FAIL;
    597 
    598     DISPPARAMS ps;
    599     ps.cArgs = 1;
    600     ps.rgvarg = pValue;
    601     ps.cNamedArgs = 0;
    602     ps.rgdispidNamedArgs = NULL;
    603 
    604     return pObj->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYPUT, &ps, NULL, NULL, NULL);
    605 }
    606 
    607 IDispatch* DuiLib::CWebBrowserUI::GetHtmlWindow()
    608 {
    609     IDispatch* pDp =  NULL;
    610     HRESULT hr;
    611     if (m_pWebBrowser2)
    612         hr=m_pWebBrowser2->get_Document(&pDp);
    613 
    614     if (FAILED(hr))
    615         return NULL;
    616 
    617     CComQIPtr<IHTMLDocument2> pHtmlDoc2 = pDp;
    618 
    619     if (pHtmlDoc2 == NULL)
    620         return NULL;
    621 
    622     hr=pHtmlDoc2->get_parentWindow(&_pHtmlWnd2);
    623 
    624     if (FAILED(hr))
    625         return NULL;
    626 
    627     IDispatch *pHtmlWindown = NULL;
    628     hr=_pHtmlWnd2->QueryInterface(IID_IDispatch, (void**)&pHtmlWindown);
    629     if (FAILED(hr))
    630         return NULL;
    631 
    632     return pHtmlWindown;
    633 }
    634 
    635 IWebBrowser2* DuiLib::CWebBrowserUI::GetWebBrowser2( void )
    636 {
    637     return m_pWebBrowser2;
    638 }
    639 
    640 HRESULT STDMETHODCALLTYPE DuiLib::CWebBrowserUI::QueryStatus( __RPC__in_opt const GUID *pguidCmdGroup, ULONG cCmds, __RPC__inout_ecount_full(cCmds ) OLECMD prgCmds[ ], __RPC__inout_opt OLECMDTEXT *pCmdText )
    641 {
    642     HRESULT hr = OLECMDERR_E_NOTSUPPORTED;
    643     return hr;
    644 }
    645 
    646 HRESULT STDMETHODCALLTYPE DuiLib::CWebBrowserUI::Exec( __RPC__in_opt const GUID *pguidCmdGroup, DWORD nCmdID, DWORD nCmdexecopt, __RPC__in_opt VARIANT *pvaIn, __RPC__inout_opt VARIANT *pvaOut )
    647 {
    648     HRESULT hr = S_OK;
    649 
    650     if (pguidCmdGroup && IsEqualGUID(*pguidCmdGroup, CGID_DocHostCommandHandler))
    651     {
    652 
    653         switch (nCmdID) 
    654         {
    655 
    656         case OLECMDID_SHOWSCRIPTERROR:
    657             {
    658                 IHTMLDocument2*             pDoc = NULL;
    659                 IHTMLWindow2*               pWindow = NULL;
    660                 IHTMLEventObj*              pEventObj = NULL;
    661                 BSTR                        rgwszNames[5] = 
    662                 { 
    663                     SysAllocString(L"errorLine"),
    664                     SysAllocString(L"errorCharacter"),
    665                     SysAllocString(L"errorCode"),
    666                     SysAllocString(L"errorMessage"),
    667                     SysAllocString(L"errorUrl")
    668                 };
    669                 DISPID                      rgDispIDs[5];
    670                 VARIANT                     rgvaEventInfo[5];
    671                 DISPPARAMS                  params;
    672                 BOOL                        fContinueRunningScripts = true;
    673                 int                         i;
    674 
    675                 params.cArgs = 0;
    676                 params.cNamedArgs = 0;
    677 
    678                 // Get the document that is currently being viewed.
    679                 hr = pvaIn->punkVal->QueryInterface(IID_IHTMLDocument2, (void **) &pDoc);    
    680                 // Get document.parentWindow.
    681                 hr = pDoc->get_parentWindow(&pWindow);
    682                 pDoc->Release();
    683                 // Get the window.event object.
    684                 hr = pWindow->get_event(&pEventObj);
    685                 // Get the error info from the window.event object.
    686                 for (i = 0; i < 5; i++) 
    687                 {  
    688                     // Get the property's dispID.
    689                     hr = pEventObj->GetIDsOfNames(IID_NULL, &rgwszNames[i], 1, 
    690                         LOCALE_SYSTEM_DEFAULT, &rgDispIDs[i]);
    691                     // Get the value of the property.
    692                     hr = pEventObj->Invoke(rgDispIDs[i], IID_NULL,
    693                         LOCALE_SYSTEM_DEFAULT,
    694                         DISPATCH_PROPERTYGET, &params, &rgvaEventInfo[i],
    695                         NULL, NULL);
    696                     SysFreeString(rgwszNames[i]);
    697                 }
    698 
    699                 // At this point, you would normally alert the user with 
    700                 // the information about the error, which is now contained
    701                 // in rgvaEventInfo[]. Or, you could just exit silently.
    702 
    703                 (*pvaOut).vt = VT_BOOL;
    704                 if (fContinueRunningScripts)
    705                 {
    706                     // Continue running scripts on the page.
    707                     (*pvaOut).boolVal = VARIANT_TRUE;
    708                 }
    709                 else
    710                 {
    711                     // Stop running scripts on the page.
    712                     (*pvaOut).boolVal = VARIANT_FALSE;   
    713                 } 
    714                 break;
    715             }
    716         default:
    717             hr = OLECMDERR_E_NOTSUPPORTED;
    718             break;
    719         }
    720     }
    721     else
    722     {
    723         hr = OLECMDERR_E_UNKNOWNGROUP;
    724     }
    725     return (hr);
    726 }

    带参数的:

    源码

  • 相关阅读:
    目前比较全的CSS重设(reset)方法总结
    UrlRewritingNet.UrlRewrite 中文文档
    CSS Hack汇总快查
    CSS8款CSS制作数据报表技巧
    CSS属性与JavaScript 编码方法对照表
    需要掌握的八个CSS布局技巧
    了解网页设计标准尺寸
    JavaScript是否为创造游戏准备好了
    JavaScript(2)
    CSS兼容
  • 原文地址:https://www.cnblogs.com/chechen/p/5976462.html
Copyright © 2020-2023  润新知