最近在SDK下使用WebBrowser遇到了个问题
最近在SDK下使用WebBrowser遇到了個問題,就是VK_TAB加速健,經(jīng)后來分析,其實Ctrl+C等操作也是不行的,只不過已經(jīng)事先在框架中執(zhí)行了ExecWeb操作。
一開始,我還以為是MFC的CHtmlView對WebBrowser的包裝過程中也象我處理Ctrl+C處理好了。因此,從早上到今晚的12點,我都試圖使用這種方法來解決。就是在DocumetComplete后得到所有的Input link和textarea以及frame,然后實現(xiàn)SetFocusToNext和SetFocusToPrev.等函數(shù)。
//
BOOL CHtmlView::SetFocusToNext(LPDISPATCH pHtmlDocument)
{
BOOL bFocus=FALSE;
BOOL bFassCurFocus=FALSE;
IHTMLDocument2 *pDoc=NULL;
pHtmlDocument->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc);
if(pDoc)
{
?? IHTMLElementCollection* pColl = NULL;
?? //得到所有元素
?? if(SUCCEEDED(pDoc->get_all(&pColl)))
?? {
??? long?? nNumElements;??
??? if(SUCCEEDED(pColl->get_length(&nNumElements)))
??? {
???? if(nNumElements)
???? {
????? IHTMLElement* pActiveElement=NULL;
????? if(SUCCEEDED(pDoc->get_activeElement(&pActiveElement)))
????? {
?????? IHTMLFrameBase2* pFrame=NULL;
?????? if(SUCCEEDED(pActiveElement->QueryInterface(IID_IHTMLFrameBase2,(void**)&pFrame)))
?????? {
??????? if(pFrame)
??????? {
???????? IHTMLWindow2 *pWin;
???????? if(SUCCEEDED(pFrame->get_contentWindow(&pWin)))
???????? {
????????? if(pWin)
????????? {
?????????? IHTMLDocument2 *pDoc2=NULL;
?????????? if(SUCCEEDED(pWin->get_document(&pDoc2)))
?????????? {
??????????? if(pDoc2)
??????????? {
???????????? SetFocusToFirst(pDoc2);
???????????? pDoc2->Release();
??????????? }
?????????? }
?????????? pWin->Release();
????????? }
???????? }
???????? pFrame->Release();
??????? }
?????? }
?????? //這里已獲得activeElement http://www.wzszf.com/
?????? long lActSrcIndex;
?????? pActiveElement->get_sourceIndex(&lActSrcIndex);
?????? for(long i = 0;i< nNumElements;i++)??
?????? {
??????? VARIANT varName;
??????? varName.vt=VT_UINT;
??????? varName.lVal?? =?? i;
??????? VARIANT varIndex;
??????? LPDISPATCH lpElementDisp=NULL;
??????? pColl->item(varName,varIndex,&lpElementDisp);
??????? {
???????? if(bFassCurFocus)//過了焦點,出現(xiàn)可焦則焦
???????? {
????????? IHTMLElement2* pElement2=NULL;???
????????? if(SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLElement2,(void**)&pElement2)))
????????? {
?????????? IHTMLInputElement2*pInput=NULL;
?????????? IHTMLAnchorElement2* pAnchor=NULL;
?????????? IHTMLTextAreaElement* pTextArea=NULL;
?????????? IHTMLFrameBase2* pFrame=NULL;
?????????? if(SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLInputElement2,(void**)&pInput))
??????????? ||SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLAnchorElement2,(void**)&pAnchor))
??????????? ||SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLTextAreaElement,(void**)&pTextArea))
??????????? ||SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLFrameBase2,(void**)&pFrame)))
?????????? {
??????????? if(pInput||pAnchor||pTextArea||pFrame)
??????????? {
???????????? if(SUCCEEDED(pElement2->focus()))
????????????? bFocus=TRUE;
???????????? if(pInput)
????????????? pInput->Release();
???????????? if(pAnchor)
????????????? pAnchor->Release();
???????????? if(pTextArea)
????????????? pTextArea->Release();
???????????? if(pFrame&&bFocus)
????????????? pFrame->Release();
???????????? if(bFocus)
???????????? {
????????????? pElement2->Release();
????????????? break;
???????????? }
??????????? }
?????????? }
?????????? pElement2->Release();
????????? }
???????? }
???????? else
???????? {
????????? IHTMLElement* pElement=NULL;???
????????? if(SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLElement,(void**)&pElement)))
????????? {
?????????? long lCurSrcIndex;
?????????? pElement->get_sourceIndex(&lCurSrcIndex);
?????????? if(lCurSrcIndex==lActSrcIndex)//找到當前活動元素
?????????? {
??????????? bFassCurFocus=TRUE;
?????????? }
?????????? pElement->Release();
????????? }
???????? }
??????? }
?????? }//for
?????? pActiveElement->Release();
????? }
???? }
??? }
??? pColl->Release();
?? }
?? pDoc->Release();
}
return bFocus;
}
//
BOOL CHtmlView::SetFocusToPrev()
{
BOOL bFocus=FALSE;
BOOL bFassCurFocus=FALSE;
LPDISPATCH lpDisp=NULL;
m_pHost->m_pWebBrowser->get_Document(&lpDisp);
IHTMLDocument2 *pDoc=NULL;
lpDisp->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc);
if(pDoc)
{
?? IHTMLElementCollection* pColl = NULL;
?? //得到所有元素
?? if(SUCCEEDED(pDoc->get_all(&pColl)))
?? {
??? long?? nNumElements;??
??? if(SUCCEEDED(pColl->get_length(&nNumElements)))
??? {
???? if(nNumElements)
???? {
????? IHTMLElement* pActiveElement=NULL;
????? if(SUCCEEDED(pDoc->get_activeElement(&pActiveElement)))
????? {
?????? //這里已獲得activeElement
?????? long lActSrcIndex;
?????? pActiveElement->get_sourceIndex(&lActSrcIndex);
?????? for(long i=nNumElements-1;i>=0;i--)??
?????? {
??????? VARIANT varName;
??????? varName.vt=VT_UINT;
??????? varName.lVal?? =?? i;
??????? VARIANT varIndex;
??????? LPDISPATCH lpElementDisp=NULL;
??????? pColl->item(varName,varIndex,&lpElementDisp);
??????? {
???????? if(bFassCurFocus)//過了焦點,出現(xiàn)可焦則焦
???????? {
????????? IHTMLElement2* pElement2=NULL;???
????????? if(SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLElement2,(void**)&pElement2)))
????????? {
?????????? IHTMLInputElement2*pInput=NULL;
?????????? IHTMLAnchorElement2* pAnchor=NULL;
?????????? IHTMLTextAreaElement* pTextArea=NULL;
?????????? if(SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLInputElement2,(void**)&pInput))
??????????? ||SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLAnchorElement2,(void**)&pAnchor))
??????????? ||SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLTextAreaElement,(void**)&pTextArea)))
?????????? {
??????????? if(pInput||pAnchor||pTextArea)
??????????? {
???????????? if(SUCCEEDED(pElement2->focus()))
???????????? {
????????????? pElement2->Release();
????????????? bFocus=TRUE;
????????????? break;
???????????? }
???????????? if(pInput)
????????????? pInput->Release();
???????????? if(pAnchor)
????????????? pAnchor->Release();
???????????? if(pTextArea)
????????????? pTextArea->Release();
??????????? }
?????????? }
?????????? pElement2->Release();
????????? }
???????? }
???????? else
???????? {
????????? IHTMLElement* pElement=NULL;???
????????? if(SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLElement,(void**)&pElement)))
????????? {
?????????? long lCurSrcIndex;
?????????? pElement->get_sourceIndex(&lCurSrcIndex);
?????????? if(lCurSrcIndex==lActSrcIndex)//找到當前活動元素
?????????? {
??????????? bFassCurFocus=TRUE;
?????????? }
?????????? pElement->Release();
????????? }
???????? }
??????? }
?????? }//for
?????? pActiveElement->Release();
????? }
???? }
??? }
??? pColl->Release();
?? }
?? else
??? Alert("er");
?? pDoc->Release();
}
lpDisp->Release();
return bFocus;
}
void CHtmlView::SetFocusToFirst(LPDISPATCH pHtmlDocument)
{
IHTMLDocument2 *pDoc=NULL;
pHtmlDocument->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc);
if(pDoc)
{
?? IHTMLElementCollection* pColl = NULL;
?? //得到所有元素
?? if(SUCCEEDED(pDoc->get_all(&pColl)))
?? {
??? long?? nNumElements;??
??? if(SUCCEEDED(pColl->get_length(&nNumElements)))
??? {
???? if(nNumElements)
???? {
????? for(long i = 0;i< nNumElements;i++)??
????? {
?????? VARIANT varName;
?????? varName.vt=VT_UINT;
?????? varName.lVal?? =?? i;
?????? VARIANT varIndex;
?????? LPDISPATCH lpElementDisp=NULL;
?????? pColl->item(varName,varIndex,&lpElementDisp);
?????? {
??????? IHTMLElement2* pElement2=NULL;???
??????? if(SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLElement2,(void**)&pElement2)))
??????? {
???????? IHTMLInputElement2*pInput=NULL;
???????? IHTMLAnchorElement2* pAnchor=NULL;
???????? IHTMLTextAreaElement* pTextArea=NULL;
???????? if(SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLInputElement2,(void**)&pInput))
????????? ||SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLAnchorElement2,(void**)&pAnchor))
????????? ||SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLTextAreaElement,(void**)&pTextArea)))
???????? {
????????? if(pInput||pAnchor||pTextArea)
????????? {
?????????? if(SUCCEEDED(pElement2->focus()))
?????????? {
??????????? pElement2->Release();
??????????? break;
?????????? }
?????????? if(pInput)
??????????? pInput->Release();
?????????? if(pAnchor)
??????????? pAnchor->Release();
?????????? if(pTextArea)
??????????? pTextArea->Release();
????????? }
???????? }
???????? pElement2->Release();
??????? }
?????? }
????? }
???? }
??? }
??? pColl->Release();
?? }
?? pDoc->Release();
}
}
// http://www.wzszf.com/
void CHtmlView::SetFocusToLast()
{
?? LPDISPATCH lpDisp=NULL;
m_pHost->m_pWebBrowser->get_Document(&lpDisp);
IHTMLDocument2 *pDoc=NULL;
lpDisp->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc);
if(pDoc)
{
?? IHTMLElementCollection* pColl = NULL;
?? //得到所有元素
?? if(SUCCEEDED(pDoc->get_all(&pColl)))
?? {
??? long?? nNumElements;??
??? if(SUCCEEDED(pColl->get_length(&nNumElements)))
??? {
???? if(nNumElements)
???? {
????? for(long i = nNumElements-1;i>=0;i--)??
????? {
?????? VARIANT varName;
?????? varName.vt=VT_UINT;
?????? varName.lVal?? =?? i;
?????? VARIANT varIndex;
?????? LPDISPATCH lpElementDisp=NULL;
?????? pColl->item(varName,varIndex,&lpElementDisp);
?????? {
??????? IHTMLElement2* pElement2=NULL;???
??????? if(SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLElement2,(void**)&pElement2)))
??????? {
???????? IHTMLInputElement2*pInput=NULL;
???????? IHTMLAnchorElement2* pAnchor=NULL;
???????? IHTMLTextAreaElement* pTextArea=NULL;
???????? if(SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLInputElement2,(void**)&pInput))
????????? ||SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLAnchorElement2,(void**)&pAnchor))
????????? ||SUCCEEDED(lpElementDisp->QueryInterface(IID_IHTMLTextAreaElement,(void**)&pTextArea)))
???????? {
????????? if(pInput||pAnchor||pTextArea)
????????? {
?????????? if(SUCCEEDED(pElement2->focus()))
?????????? {
??????????? pElement2->Release();
??????????? break;
?????????? }
?????????? if(pInput)
??????????? pInput->Release();
?????????? if(pAnchor)
??????????? pAnchor->Release();
?????????? if(pTextArea)
??????????? pTextArea->Release();
????????? }
???????? }
???????? pElement2->Release();
??????? }
?????? }
????? }
???? }
??? }
??? pColl->Release();
?? }
?? pDoc->Release();
}
lpDisp->Release();
}
理論上這個方法可以解決,事實也接近解決,當然在遇到Frame時候我碰到了極大挑戰(zhàn)。結果,我開始重新思考解決方案。
總結
以上是生活随笔為你收集整理的最近在SDK下使用WebBrowser遇到了个问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VTK修炼之道17:图像基本操作_图像信
- 下一篇: loadlibrary failed w