CSDN博客

img skyremember

如何控制WebBrowser,并使其提交其中的网页或者操纵其中的文本框等?

发表于2008/10/3 11:15:00  2098人阅读

http://www.80diy.com/home/20050718/23/4152927.html

如何控制WebBrowser,并使其提交其中的网页或者操纵其中的文本框等?

楼主tedyxuan(小飞侠)2005-07-18 23:43:55 在 VC/MFC / ATL/ActiveX/COM 提问

这个控件已经下载了一个网页,这个网页中有一个表格。我想通过VC调用来提交这个表格。  
  最好能告诉我两种方法,一个是往这个webbrowser控件发回车换行事件,另外一个我希望能遍历其中的文本框,表格,然后控制他们。谢谢  
   
  我现在的问题是:  
  我用加了一个Webbrowser控件,可是我调用GetHwnd()方法出错,得不到句柄,所以没法发消息。  
  我用GetUnkown()得到IHTMLInterface,但是总是得不到。很奇怪  
   
  希望指点一下,谢谢 问题点数:30、回复次数:2Top

<script style="display: none;" type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

1 楼FatSpiders(蜘蛛仔)回复于 2005-07-19 12:10:53 得分 0

先指定类型然后再试一下。  
  或者你改用POST/GET试一下Top

2 楼masterz(www.fruitfruit.com)回复于 2005-07-19 12:28:45 得分 30

看看http://www.fruitfruit.com/vc/ie/iehtml.cppTop



 ///////////////////////////////////////////////////////////////////////////////////////
//click submit button of IE window
//If it works, it is written by masterz,otherwise I don't
//know who writes it^_^
///////////////////////////////////////////////////////////////////////////////////////
void CGetIESrcDlg::NavigateToUrl()
{
// Import the following files in your stdafx.h
// #import <mshtml.tlb> // Internet Explorer 5
// #import <shdocvw.dll>
//  Refer to "Connect to Internet Explorer Instances, From your own Process. " in www.codeguru.com
SHDocVw::IShellWindowsPtr m_spSHWinds;
CoInitialize(NULL);
if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) == S_OK)
{
IDispatchPtr spDisp;
long nCount = m_spSHWinds->GetCount();
for (long i = 0; i < nCount; i++)
{
_variant_t va(i, VT_I4);
spDisp = m_spSHWinds->Item(va);
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
if (spBrowser != NULL)
{
IDispatchPtr spDisp;
if(spBrowser->get_Document(&spDisp) == S_OK && spDisp!= 0 )
{
MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp);
MSHTML::IHTMLElementPtr spHtmlElement;
if(spHtmlDocument==NULL)
continue;
spHtmlDocument->get_body(&spHtmlElement);
if(spHtmlDocument==NULL)
continue;
HRESULT hr;
MSHTML::IHTMLElementCollection* pColl=NULL;
hr=spHtmlDocument->get_all(&pColl);
if(pColl!=NULL&&SUCCEEDED(hr))
{
MSHTML::IHTMLElement* pElem=NULL;
_variant_t index;
index.vt=VT_I4;
index.intVal=0;
_variant_t name("Submit");
IDispatchPtr disp;
disp=pColl->item(name,index);
if(disp==NULL)
hr=E_FAIL;
else
{
hr=disp->QueryInterface(&pElem);
}
if (SUCCEEDED(hr)&& pElem != NULL)
{
//
BSTR bstrhtml;
pElem->get_outerHTML(&bstrhtml);
CString str(bstrhtml);
AfxMessageBox(str);
pElem->click();
pElem->Release();
}
pColl->Release();
}
}

}
}

}
else {
AfxMessageBox("Shell Windows interface is not avilable");
}
CoUninitialize();
}
///////////////////////////////////////////////////////////////////////////////////////
//GetRunningIESrc
///////////////////////////////////////////////////////////////////////////////////////
void CGetIESrcDlg::GetRunningIESrc()
{
// Import the following files in your stdafx.h
// #import <mshtml.tlb> // Internet Explorer 5
// #import <shdocvw.dll>
//  Refer to "Connect to Internet Explorer Instances, From your own Process. " in www.codeguru.com
//  enumeration all shell windows, only URL and source code of the first IE window is shown, you have to
//modify according your own need.
SHDocVw::IShellWindowsPtr m_spSHWinds;
CoInitialize(NULL);
if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) == S_OK)
{
IDispatchPtr spDisp;
long nCount = m_spSHWinds->GetCount();
for (long i = 0; i < nCount; i++)
{
_variant_t va(i, VT_I4);
spDisp = m_spSHWinds->Item(va);
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
if (spBrowser != NULL)
{
IDispatchPtr spDisp;
if(spBrowser->get_Document(&spDisp) == S_OK && spDisp!= 0 )
{
MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp);
MSHTML::IHTMLElementPtr spHtmlElement;
if(spHtmlDocument==NULL)
continue;
spHtmlDocument->get_body(&spHtmlElement);
if(spHtmlDocument==NULL)
continue;
_bstr_t str ;
str=spBrowser->GetLocationURL();//URL of IE window();
AfxMessageBox(str);
MSHTML::IHTMLDocument3* pHTMLDoc3;
HRESULT hr = spHtmlDocument->QueryInterface(__uuidof(MSHTML::IHTMLDocument3),(LPVOID*)&pHTMLDoc3);
ASSERT(SUCCEEDED(hr));
MSHTML::IHTMLElement* pDocElem;
hr = pHTMLDoc3->get_documentElement(&pDocElem);
pHTMLDoc3->Release();
ASSERT(SUCCEEDED(hr));
BSTR bstrHTML;
pDocElem->get_outerHTML(&bstrHTML);
pDocElem->Release();
AfxMessageBox(CString(bstrHTML));
SysFreeString(bstrHTML);
}
break;
}
}

}
else {
AfxMessageBox("Shell Windows interface is not avilable");
}
CoUninitialize();
}

///////////////////////////////////////////////////////////////////////////////////////
//change_anchor_href
//If it works, it is written by masterz,otherwise I don't
//know who writes it^_^
///////////////////////////////////////////////////////////////////////////////////////
void change_anchor_href()
{
//CComQIPtr<IWebBrowser2> m_spBrowser;
HRESULT hr;
if (m_spBrowser != NULL)
{
IDispatchPtr spDisp=NULL;
hr=m_spBrowser->get_Document(&spDisp);
if(SUCCEEDED(hr)&& spDisp!= NULL )
{
IHTMLDocument2Ptr spHtmlDocument(spDisp);
IHTMLElementPtr spHtmlElement;
spHtmlDocument->get_body(&spHtmlElement);
IHTMLElementCollection* pColl=NULL;
hr=spHtmlDocument->get_all(&pColl);
if(pColl!=NULL&&SUCCEEDED(hr))
{
IHTMLElement* pElem=NULL;
_variant_t index;
index.vt=VT_I4;
IDispatchPtr disp;
long num;
hr=pColl->get_length(&num);
if(SUCCEEDED(hr))
{
for(long i=0;i<num;i++)
{
index.intVal=i;
hr=pColl->item(index,index,&disp);
IHTMLAnchorElementPtr pAnchorElement;
if(SUCCEEDED(hr)&&disp!=NULL)
{
hr=disp->QueryInterface(&pAnchorElement);
}
if (SUCCEEDED(hr)&& pAnchorElement != NULL)
{
pAnchorElement->put_href(_bstr_t("c://tmp//link.htm"));
}

}
}
pColl->Release();
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////
//scrolltobottom Scroll to HTML view bottom
//If it works, it is written by masterz,otherwise I don't
//know who writes it^_^
///////////////////////////////////////////////////////////////////////////////////////
void CHtmlradioselView::scrolltobottom()
{
HRESULT hr;
LPDISPATCH pDispatch = GetHtmlDocument();
if( pDispatch != NULL )
{
IHTMLDocument2* pHtmlDoc=NULL;
hr = pDispatch->QueryInterface( __uuidof( IHTMLDocument2 ),(void**)&pHtmlDoc );
IHTMLWindow2*  pWindow;
if(pHtmlDoc!=NULL)
{
hr = pHtmlDoc->get_parentWindow(&pWindow);
pHtmlDoc->Release();
}

ASSERT( SUCCEEDED( hr ) );
long docheight=0;
IHTMLElement* pe=NULL;
pHtmlDoc->get_body(&pe);
IHTMLTextContainer* pe2=NULL;
if(pe!=NULL)
{
pe->QueryInterface(&pe2);
pe->Release();
}
if(pe2!=NULL)
{
pe2->get_scrollHeight(&docheight);
pe2->Release();
}
if(pWindow!=NULL&&docheight>0)
{
pWindow->scrollTo(0,docheight);
pWindow->Release();
}
pDispatch->Release();
}
}
////////////////////////////////////////////////////////
//IE internet options
///////////////////////////////////////////////////////
//method1
IShellDispatch* disp;
HRESULT sc;
sc = ::CoInitialize(NULL);
if (FAILED (sc))
{
CString str;
str.Format(_T("Failed to Initialize."));
TRACE( str) ;
return;
}

sc = ::CoCreateInstance( CLSID_Shell, NULL, CLSCTX_SERVER,
IID_IDispatch, (void**)&disp ) ;
if (FAILED (sc))
{
CString str;
str.Format(_T("Failed to create Instance :-( "));
TRACE( str) ;
return;
}
disp->ControlPanelItem(CString("inetcpl.cpl").AllocSysString());
//method2
//ShellExecute(0, "open", "control.exe", "inetcpl.cpl", ".", SW_SHOW);
//method3
//WinExec("control.exe inetcpl.cpl",SW_SHOW);


////////////////////////////////////////////////////////
//Fill input field in IE
//If it works, it is written by masterz,otherwise I don't
//know who writes it^_^
///////////////////////////////////////////////////////
#import <mshtml.tlb> // Internet Explorer 5
#import <shdocvw.dll>
#include "Shlwapi.h"
#pragma comment(lib,"Shlwapi.lib")
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
SHDocVw::IShellWindowsPtr m_spSHWinds;
if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) == S_OK)
{
IDispatchPtr spDisp;
long nCount = m_spSHWinds->GetCount();
for (long i = 0; i < nCount; i++)
{
_variant_t va(i, VT_I4);
spDisp = m_spSHWinds->Item(va);
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
if (spBrowser != NULL)
{
IDispatchPtr spDisp;
if(spBrowser->get_Document(&spDisp) == S_OK && spDisp!= 0 )
{
MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp);
MSHTML::IHTMLElementPtr spHtmlElement;
if(spHtmlDocument==NULL)
continue;
spHtmlDocument->get_body(&spHtmlElement);
if(spHtmlDocument==NULL)
continue;
HRESULT hr;
MSHTML::IHTMLElementCollection* pColl=NULL;
hr=spHtmlDocument->get_all(&pColl);
if(pColl!=NULL&&SUCCEEDED(hr))
{
long lcount = 0;
pColl->get_length(&lcount);
for(int i=0;i<lcount;i++)
{
_variant_t index;
index.vt=VT_I4;
index.intVal=i;
IDispatchPtr disp;
disp=pColl->item(index,index);
if(disp==NULL)
hr=E_FAIL;
else
{
MSHTML::IHTMLInputElementPtr pInput(disp);
if(pInput)
{
BSTR bstrtype;
pInput->get_type(&bstrtype);
printf(_bstr_t(bstrtype));
if(StrCmpW(bstrtype,L"text")==0)
{
pInput->put_value(_bstr_t("fill it"));
printf("fill a field/n");
}
SysFreeString(bstrtype);
}
}

}
pColl->Release();
}
}

}
}

}
else 
{
printf("Shell Windows interface is not avilable/n");
}
CoUninitialize();
return 0;
}


//Get IHTMLDocument2 interface from HWND
IHTMLDocument2* GetDocInterface(HWND hWnd) 
{
// 我们需要显示地装载OLEACC.DLL,这样我们才知道有没有安装MSAA
HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
IHTMLDocument2* pDoc2=NULL;
if ( hInst != NULL ){
if ( hWnd != NULL ){
CComPtr<IHTMLDocument> spDoc=NULL;
LRESULT lRes;

UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
::SendMessageTimeout( hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );

LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
if ( pfObjectFromLresult != NULL ){
HRESULT hr;
hr=pfObjectFromLresult(lRes,IID_IHTMLDocument,0,(void**)&spDoc);
if ( SUCCEEDED(hr) ){
CComPtr<IDispatch> spDisp;
CComQIPtr<IHTMLWindow2> spWin;
spDoc->get_Script( &spDisp );
spWin = spDisp;
spWin->get_document( &pDoc2 );
}
}
}
::FreeLibrary(hInst);
else{//如果没有安装MSAA
AfxMessageBox(_T("请您安装Microsoft Active Accessibility"));
}
return pDoc2;
HOWTO: Get IHTMLDocument2 from a HWND(根据HWND取得IHTMLDocument2接口)(http://support.microsoft.com/default.aspx?scid=kb;EN-US;q249232
/*函数名:GetDocInterfaceByMSAA
  参数:hwnd,WebBrowser控件的窗口句柄
  功能:取得hwnd对应的Webbrowser控件的IHTMLDocument2*接口.
*/
IHTMLDocument2* GetDocInterfaceByMSAA(HWND hwnd)
{
HRESULT hr;
HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );

IHTMLDocument2* pDoc2=NULL;
if ( hInst != NULL ){
if ( hwnd != NULL ){
//取得AccessibleObjectFromWindow函数
LPFNACCESSIBLEOBJECTFROMWINDOW pfAccessibleObjectFromWindow =
(LPFNACCESSIBLEOBJECTFROMWINDOW)::GetProcAddress(hInst,_T("AccessibleObjectFromWindow"));
if(pfAccessibleObjectFromWindow != NULL){
CComPtr spAccess;
hr=pfAccessibleObjectFromWindow(hwnd,0,
IID_IAccessible,(void**) &spAccess);//取得Webbrowser控件的IAccessible接口
if ( SUCCEEDED(hr) ){
CComPtr spServiceProv;
hr=spAccess->QueryInterface(IID_IServiceProvider,(void**)&spServiceProv);
if(hr==S_OK){
CComPtr spWin;
hr=spServiceProv->QueryService(IID_IHTMLWindow2,IID_IHTMLWindow2,
(void**)&spWin);
/*
注意:并不是每次都能取得IHTMLWindow2接口,如果调用失败,可以尝试取得IHTMLElement接口:
CComPtr spElement;
hr=spServiceProv->QueryService(IID_IHTMLElement,IID_IHTMLElement,(void**)&spElement);
*/
if(hr==S_OK)
spWin->get_document(&pDoc2);
}
}
}
}
::FreeLibrary(hInst);
}
else{
AfxMessageBox(_T("请您安装Microsoft Active Accessibility"));
}
return pDoc2;
}

void SetPassword(IHTMLDocument2 *pDoc2,POINT pt,CString strName)
{
if(pDoc2==NULL)return "";
CComPtr<IHTMLElement> pElement;
HRESULT hr=pDoc2->elementFromPoint(pt.x,pt.y,&pElement);
if(SUCCEEDED(hr)){
CComPtr<IHTMLInputTextElement> pPwdElement;
hr=pElement->QueryInterface(IID_IHTMLInputTextElement,
(void**)&pPwdElement);
if(SUCCEEDED(hr)){
CComBSTR type;
hr=pPwdElement->get_type(&type);
if(SUCCEEDED(hr)){
if(type==_T("password")){
CComBSTR pwd;
hr=pPwdElement->put_value(strName);
if(SUCCEEDED(hr)){
if(pwd.Length()!=0){
CComBSTR msg;
msg=pwd;
CString str(msg);
return str;
}
else{
return "";
}
}
}
}
}
}
   pDoc2->Release();
   return "";
 }



////////////////////////////////////////////////////////
//get password from IE
//If it works, it is written by masterz,otherwise I don't
//know who writes it^_^
///////////////////////////////////////////////////////
#import <mshtml.tlb> // Internet Explorer 5
#import <shdocvw.dll>
#include "Shlwapi.h"
#include <tchar.h>
#include <stdio.h>
#pragma comment(lib,"Shlwapi.lib")

bool GetPassword(MSHTML::IHTMLDocument2Ptr spHtmlDocument,char* buf,int buflen)
{
buf[0]=0;
if(spHtmlDocument==NULL)
return false;
bool bret = false;
MSHTML::IHTMLElementPtr spHtmlElement;
spHtmlDocument->get_body(&spHtmlElement);
if(spHtmlElement==NULL)
return false;
HRESULT hr;
MSHTML::IHTMLElementCollection* pColl=NULL;
hr=spHtmlDocument->get_all(&pColl);
if(pColl!=NULL&&SUCCEEDED(hr))
{
long lcount = 0;
pColl->get_length(&lcount);
for(int i=0;i<lcount;i++)
{
_variant_t index;
index.vt=VT_I4;
index.intVal=i;
IDispatchPtr disp;
disp=pColl->item(index,index);
if(disp==NULL)
hr=E_FAIL;
else
{
MSHTML::IHTMLInputElementPtr pInput(disp);
if(pInput)
{
BSTR bstrtype;
pInput->get_type(&bstrtype);
printf(_bstr_t(bstrtype));
if(StrCmpW(bstrtype,L"password")==0)
{
BSTR bstrval;
pInput->get_value(&bstrval);
bret = true;
wsprintf(buf,"%s",(char*)_bstr_t(bstrval));
SysFreeString(bstrval);
}
SysFreeString(bstrtype);
}
}
}
pColl->Release();
}
return bret;
}
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
SHDocVw::IShellWindowsPtr m_spSHWinds;
if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) == S_OK)
{
IDispatchPtr spDisp;
long nCount = m_spSHWinds->GetCount();
for (long i = 0; i < nCount; i++)
{
_variant_t va(i, VT_I4);
spDisp = m_spSHWinds->Item(va);
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
if (spBrowser != NULL)
{
IDispatchPtr spDisp;
if(spBrowser->get_Document(&spDisp) == S_OK && spDisp!= 0 )
{
MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp);
{
char tmpbuf[128];
if(GetPassword(spHtmlDocument,tmpbuf,127))
{
printf("password is:%s/n",tmpbuf);

}
}

}

}
}

}
else 
{
printf("Shell Windows interface is not avilable/n");
}
CoUninitialize();
return 0;
}

//////////////////////////////////////////
//Get IWebBrowser2 from IHTMLDocument2
CComQIPtr<IServiceProvider> isp = pIHTMLDocument2;
CComQIPtr<IWebBrowser2> iwb2;
isp->QueryService(IID_IWebBrowserApp,IID_IWebBrowser2, (void**)&iwb2);


//////////////////////////////////////////////////
//CHtmlView and mouse message
add virtual function PreTranslateMessage(MSG* pMsg) to your derived view class,
if(pMsg->message==WM_LBUTTONDOWN)
{
//do what you want.
}
//////////////////////////////////////////////////////////
//activex control embeded in IE, get IE pointer
void CmfcactivexhotkeyCtrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
if (!pdc)
return;
LPOLECLIENTSITE  pSite = this->GetClientSite();
if( pSite)
{
IOleContainer* pCont = NULL;
if(SUCCEEDED(pSite->GetContainer(&pCont)))
{
IWebBrowser2* pBrowser= NULL;
IServiceProvider* pProv = NULL;
if(SUCCEEDED(pCont->QueryInterface(IID_IServiceProvider, (void**)&pProv)))
{
pProv->QueryService(IID_IWebBrowserApp,IID_IWebBrowser2, (void**)&pBrowser);
if(pBrowser)
{
BSTR bslocation;
pBrowser->get_LocationURL(&bslocation);
CString stmp(bslocation);
pdc->TextOut(0,0,stmp); OutputDebugString(stmp);
SysFreeString(bslocation);
pBrowser->Release();
pdc->TextOut(rcBounds.left,rcBounds.top, "IID_IWebBrowser2 found by QueryService");
pBrowser->Release();
}
}
pCont->Release();
}
else
pdc->TextOut(rcBounds.left,rcBounds.top, "GetContainer not found");
pSite->Release();
}
else
pdc->TextOut(rcBounds.left,rcBounds.top, "site not found");
}
////////////////////////////////////////////////////////////////////////////
//close IE from activex control
void CmfcactivexhotkeyCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// masterz , 2004/05/16, VC2003
LPOLECLIENTSITE  pSite = this->GetClientSite();
if( pSite)
{
IOleContainer* pCont = NULL;
if(SUCCEEDED(pSite->GetContainer(&pCont)))
{
IWebBrowser2* pBrowser= NULL;
IServiceProvider* pProv = NULL;
if(SUCCEEDED(pCont->QueryInterface(IID_IServiceProvider, (void**)&pProv)))
{
pProv->QueryService(IID_IWebBrowserApp,IID_IWebBrowser2, (void**)&pBrowser);
if(pBrowser)
{
pBrowser->Quit(); OutputDebugString(__FUNCTION__);
pBrowser->Release();
}
}
pCont->Release();

}
pSite->Release();
}
COleControl::OnLButtonDblClk(nFlags, point);
}

阅读全文
0 0

相关文章推荐

img
取 消
img