我如何使用IProtectFocus保持CDHtmlDialog从editBox的绘图焦点导航?

时间:2014-01-26 08:31:05

标签: c++ com focus activex

我知道IProtectFocus接口AllowFocusChange函数可以在CDHtmlDialog执行navigate()函数时解决“焦点窃取问题”,但我不太清楚如何在我的代码中实现IProtectFocus函数。有人可以帮帮我吗?任何代码示例都非常感谢。

更多细节,我可以实现IInternetSecurityManager接口来修复javascript ActiveX权限问题。但我尝试以同样的方式实现IProtectFocus,不起作用。 我只需要C ++代码,谢谢!

我的代码示例:(CCustomControlSite是自定义的IWebBrowser控件,CInstantAnswerDlg是CDHtmlDialog。)

MFC界面部分:

class CCustomControlSite: public COleControlSite    
{

public:
CCustomControlSite(COleControlContainer *pCnt):COleControlSite(pCnt){}

protected:
DECLARE_INTERFACE_MAP();
    // Implement IServiceProvider
    BEGIN_INTERFACE_PART(ServiceProvider, IServiceProvider)
        STDMETHOD(QueryService)(REFGUID,REFIID,void**);
    END_INTERFACE_PART(ServiceProvider)

    // Implement IServiceProvider
    BEGIN_INTERFACE_PART(ProtectFocus, IProtectFocus)
        STDMETHOD(AllowFocusChange)(BOOL *);
    END_INTERFACE_PART(ProtectFocus)
};

实施部分:

ULONG FAR EXPORT CCustomControlSite::XServiceProvider::AddRef()
{
    METHOD_PROLOGUE(CCustomControlSite, ServiceProvider)
        return pThis->ExternalAddRef();
}

 ULONG FAR EXPORT CCustomControlSite::XServiceProvider::Release()
 {                            
    METHOD_PROLOGUE(CCustomControlSite, ServiceProvider)
    return pThis->ExternalRelease();
 }

HRESULT FAR EXPORT CCustomControlSite::XServiceProvider::QueryInterface(REFIID riid,void** ppvObj)
 {
    METHOD_PROLOGUE(CCustomControlSite, ServiceProvider)
    HRESULT hr = (HRESULT)pThis->ExternalQueryInterface(&riid, ppvObj);
    return hr;
 }

STDMETHODIMP CCustomControlSite::XServiceProvider::QueryService(REFGUID guidService,REFIID riid,void** ppvObject)
{
    if (guidService == SID_SInternetSecurityManager && 
    riid == IID_IInternetSecurityManager)
    {
        METHOD_PROLOGUE(CCustomControlSite, ServiceProvider)
        HRESULT hr = (HRESULT)pThis->ExternalQueryInterface(&riid, ppvObject);
    return hr;
} 
else 
{
    *ppvObject = NULL;
}
return E_NOINTERFACE;
}

// ProtectFocus Methods
ULONG FAR EXPORT CCustomControlSite::XProtectFocus::AddRef()
{
    METHOD_PROLOGUE(CCustomControlSite, ProtectFocus)
        return pThis->ExternalAddRef();
 }

ULONG FAR EXPORT CCustomControlSite::XProtectFocus::Release()
{                            
    METHOD_PROLOGUE(CCustomControlSite, ProtectFocus)
        return pThis->ExternalRelease();
}

 HRESULT FAR EXPORT CCustomControlSite::XProtectFocus::QueryInterface(REFIID riid,void** ppvObj)
{
    METHOD_PROLOGUE(CCustomControlSite, ProtectFocus)
        HRESULT hr = (HRESULT)pThis->ExternalQueryInterface(&riid, ppvObj);
     return hr;
}

 STDMETHODIMP CCustomControlSite::XProtectFocus::AllowFocusChange(BOOL *pfAllow)
 {
     *pfAllow = FALSE;
      return S_OK;
 }

将控件添加到CDHtmlDialog:

BOOL CInstantAnswerDlg::CreateControlSite(COleControlContainer* pContainer,COleControlSite** ppSite,UINT /* nID */,REFCLSID /* clsid */ )
{
    if(ppSite == NULL)
    {
        ASSERT(FALSE);
        return FALSE;
    }
    m_pCustSite = m_insAnswerManager->CreateSite(pContainer);
    if (!m_pCustSite)
        return FALSE;
    *ppSite = m_pCustSite;
    return TRUE;    
}

问题是XProtectFocus :: QueryInterface和XProtectFocus :: AllowFocusChange函数都没有被调用,而XServiceProvider :: QueryInterface可能被调用。我哪里错了?

1 个答案:

答案 0 :(得分:0)

您放置IProtectFocus和IInternetSecurityManager实现的位置取决于您的QueryService实现。由于您在实现中调用CCustomControlSite :: ExternalQueryInterface,因此需要在CCustomControlSite类上添加interface map macros