zoukankan      html  css  js  c++  java
  • C++(MFC)中WebBrowser去除3D边框的方法(实现IDocHostUIHandler接口)

    先说实在的:最终解决办法是实现IDocHostUIHandler接口,在GetHostInfo方法里解决,但“实现接口”意味着QueryInterface、AddRef、Release三个方法必须实现,所以相关代码是:
    class CMyWebBrowser : public IDocHostUIHandler
    {
    public:
        CMyWebBrowser() { m_cRef = 0; }
        virtual ~CMyWebBrowser() {}

        LONG                m_cRef;

    private:
        HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject)
        {
            HRESULT result = S_OK;

            if (IsBadWritePtr(ppvObject, sizeof(LPVOID))) 
                result = E_INVALIDARG;  

            if (result == S_OK)
            {
                *ppvObject = NULL; 

                if ( IsEqualIID( riid, IID_IUnknown ) )
                    *ppvObject = this;
                //             else if ( IsEqualIID( riid, IID_IOleClientSite ) )
                //                 *ppvObject = (IOleClientSite *) this;
                else if ( IsEqualIID( riid, IID_IDocHostUIHandler ) )
                    *ppvObject = (IDocHostUIHandler *) this;
                else
                    result = E_NOINTERFACE;
            }

            if (result == S_OK)
                this->AddRef(); 

            return result; 
        }

        ULONG STDMETHODCALLTYPE AddRef(){
            InterlockedIncrement(&m_cRef); 
            return m_cRef;
        }

        ULONG STDMETHODCALLTYPE Release() {
            ULONG ulRefCount = InterlockedDecrement(&m_cRef);

            return ulRefCount; 
        }

        HRESULT STDMETHODCALLTYPE EnableModeless(BOOL fEnable) { return S_OK; }

        HRESULT STDMETHODCALLTYPE FilterDataObject(IDataObject *pDO,IDataObject **ppDORet){return S_OK;}

        HRESULT STDMETHODCALLTYPE GetDropTarget(IDropTarget *pDropTarget, IDropTarget **ppDropTarget){return S_OK;}

        HRESULT STDMETHODCALLTYPE GetExternal(IDispatch **ppDispatch){return S_OK;}

        HRESULT STDMETHODCALLTYPE GetOptionKeyPath(LPOLESTR *pchKey,DWORD dw){return S_OK;}

        STDMETHOD(GetHostInfo)(DOCHOSTUIINFO *pInfo) {
            pInfo->cbSize = sizeof(DOCHOSTUIINFO);
            pInfo->dwFlags |= DOCHOSTUIFLAG_NO3DBORDER;
            pInfo->dwDoubleClick = DOCHOSTUIDBLCLK_DEFAULT;
            pInfo->pchHostCss = 0;
            pInfo->pchHostNS = 0;

            return S_OK;
        }

        HRESULT STDMETHODCALLTYPE HideUI(VOID){return S_OK;}

        HRESULT STDMETHODCALLTYPE OnDocWindowActivate(BOOL fActivate){return S_OK;}

        HRESULT STDMETHODCALLTYPE OnFrameWindowActivate(BOOL fActivate){return S_OK;}

        HRESULT STDMETHODCALLTYPE ResizeBorder(LPCRECT prcBorder,IOleInPlaceUIWindow *pUIWindow,BOOL fFrameWindow){return S_OK;}

        HRESULT STDMETHODCALLTYPE ShowContextMenu(DWORD dwID, POINT *ppt, IUnknown *pcmdtReserved,IDispatch *pdispReserved){return S_OK;}

        HRESULT STDMETHODCALLTYPE ShowUI(DWORD dwID,
            IOleInPlaceActiveObject *pActiveObject,
            IOleCommandTarget *pCommandTarget,
            IOleInPlaceFrame *pFrame,
            IOleInPlaceUIWindow *pDoc
            ){return S_OK;}

        HRESULT STDMETHODCALLTYPE TranslateAccelerator(LPMSG lpMsg,
            const GUID *pguidCmdGroup,
            DWORD nCmdID
            )
        {return S_OK;}

        HRESULT STDMETHODCALLTYPE TranslateUrl(DWORD dwTranslate,
            OLECHAR *pchURLIn,
            OLECHAR **ppchURLOut
            )
        {return S_OK;}

        HRESULT STDMETHODCALLTYPE UpdateUI(VOID) {return S_OK;}
    };

    1.要写的更通用一点,就是上述类就这么些内容,然后WEBBrowser从这个类继承。

    2.除了实现这个接口外,别忘了SetUIHandler,这个不难查到,这里就不写了。

    牢骚一下:项目中用到WEBBrowser,由于项目组中只有我一个人做过WEB,所以这玩意一直是我维护,最近由于我给里面的Html加了DocType,导致WEBBrowser出现了3D边框,不管是css的border:0还是body的leftmargin和topmargin,都不管用,查了很多资料(主要是google外国人的),都说实现IDocHostUIHandler接口,在GetHostInfo方法里能干掉3D边框,但我按照网上做的,结果要么报错,要么GetHostInfo不走,郁闷了我,折腾了两天,最后几乎不懂COM的我还是觉得QueryInterface等3个方法要实现,结果去查,果然如此,实现之后,搞定~~~

  • 相关阅读:
    一个屌丝程序猿的人生(一百零二)
    一个屌丝程序猿的人生(一百零一)
    一个屌丝程序猿的人生(一百)
    一个屌丝程序猿的人生(九十九)
    JavaEE开发之SpringMVC中的静态资源映射及服务器推送技术
    JavaEE开发之SpringMVC中的自定义拦截器及异常处理
    JavaEE开发之SpringMVC中的路由配置及参数传递详解
    JavaEE开发使用Maven管理的SpringMVC工程
    JavaEE开发之Spring中的条件注解、组合注解与元注解
    JavaEE开发之Spring中的多线程编程以及任务定时器详解
  • 原文地址:https://www.cnblogs.com/lidabo/p/3608118.html
Copyright © 2011-2022 走看看