获取IWebBrowser2指针的方法(一)

在Internet Explorer编程中,获取WebBrowser指针通常是一件很重要的事情,因为有了WebBrowser指针,我们就有了对IE完整的控制权。我们就可以对IE浏览器为所欲为了,想干什么都可以。比方说获取或者设置DOM控件的值。调用页面中的JavaScript,或者控制浏览器的行为 比方说刷新,前进、后退等等等等。。

下面两篇文章中,我将介绍两种方法来获取IWebBrowser2指针。

第一种

使用OLEACC.dll动态库中的ObjectFromLresult函数来获取。

第二种

使用IShellWindows 获取当前浏览器个数,然后遍历获取浏览器对象和IWebBrowser指针。

参见:获取IWebBrowser2指针的方法(二)

 

下面介绍第一种方法:

在MSDN中对ObjectFromLresult函数的解释

The LresultFromObject function returns a reference, that is similar to a handle, to the specified object. Servers return this reference when handling WM_GETOBJECT.

所以就必须要调用::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );和SendMessageTimeout返回的与WM_GETOBJECT消息关联的值。这样就可以利用ObjectFromLresult来获取IHTMLDocument2指针,有了IHTMLDocument2,剩下的事情就是QueryInterface来获取IWebBrowser2指针,这样就很轻松了。

下面看下代码:

//用来接收EnumChildWindows的回调函数
//根据类名Internet Explorer_Server来判断是否是Internet Explorer控件
BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam) 
{
	TCHAR buf[100];
	::GetClassName( hwnd, (LPTSTR)&buf, 100 );
	if ( _tcscmp( buf, _T("Internet Explorer_Server") ) == 0 ) 
	{
		*(HWND*)lParam = hwnd; return FALSE; 
	} 
	else return TRUE; 
};
IWebBrowser2* GetWBByhWnd(HWND hIEWindow) 
{
	CoInitialize( NULL );
	// Explicitly load MSAA so we know if it's installed 
	HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
	IWebBrowser2* pWebBrowser2=NULL;
	if ( hInst != NULL ) 
	{ 
		HWND hWnd ;
		if(hIEWindow==NULL)
		{
			hWnd= FindWindow("IEFrame", NULL);
			if(hWnd==NULL)
				hWnd= FindWindow("CabinetWClass", NULL);
			if( hWnd == NULL)
			{
				MessageBox (NULL,"No Running instance of Internet Explorer!","message", MB_OK);
			}
			// walk Shell DocObject View->Internet Explorer_Server
			HWND hWndChild = FindWindowEx(hWnd, 0, "Shell DocObject View", NULL);
			if(hWndChild !=0)
			{
				hWndChild = FindWindowEx(hWndChild, 0, "Internet Explorer_Server", NULL);
			}
			hWnd=hWndChild;
		}
		else
		{
			hWnd = hIEWindow;
			HWND hWndChild=NULL; 
			// Get 1st document window 
			EnumChildWindows( hWnd, EnumChildProc, (LPARAM)&hWndChild ); 
			if ( hWndChild ) 
			{
				CComPtr<IHTMLDocument2> spDoc;
				LRESULT lRes; 
				UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
				::SendMessageTimeout( hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes ); 
				LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
				if ( pfObjectFromLresult != NULL ) 
				{
					HRESULT hr;
					hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument2, 0, (void**)&spDoc );
					if ( SUCCEEDED(hr) ) 
					{
						CComPtr<IHTMLWindow2>spWnd2;
						CComPtr<IServiceProvider>spServiceProv;
						hr=spDoc->get_parentWindow ((IHTMLWindow2**)&spWnd2);
						if(SUCCEEDED(hr))
						{
							hr=spWnd2->QueryInterface (IID_IServiceProvider,(void**)&spServiceProv);
							if(SUCCEEDED(hr))
							{
								hr = spServiceProv->QueryService(SID_SWebBrowserApp,IID_IWebBrowser2,(void**)&pWebBrowser2);
								if(SUCCEEDED(hr))
								{
									return pWebBrowser2;
								}
								
							}
						}
					}
				}
			} // else document not ready 
		} // else Internet Explorer is not running 
		::FreeLibrary( hInst ); 
	} // else Active Accessibility is not installed 
	CoUninitialize(); 
	return NULL;
}

转载:http://blog.csdn.net/tingsking18/article/details/4620065
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取IWebBrowser2的元素id,可以使用以下步骤: 1. 通过IWebBrowser2::get_Document方法获取IHTMLDocument2接口。 2. 使用IHTMLDocument2::get_all方法获取IHTMLElementCollection接口,并使用它来搜索指定的元素。 3. 在IHTMLElementCollection使用IHTMLElementCollection::item方法获取指定项的IHTMLElement接口。 4. 在IHTMLElement使用IHTMLElement::get_id方法获取元素的id属性。 下面是一个示例代码: ``` IWebBrowser2* pWebBrowser = // 获取WebBrowser控件指针 IHTMLDocument2* pHTMLDoc = NULL; pWebBrowser->get_Document((IDispatch**)&pHTMLDoc); // 获取所有元素 IHTMLElementCollection* pAllElements = NULL; pHTMLDoc->get_all(&pAllElements); // 搜索指定元素 VARIANT vIndex; vIndex.vt = VT_I4; vIndex.lVal = 0; BSTR bstrTagName = L"input"; BSTR bstrType = L"text"; BSTR bstrName = L"name"; IHTMLElement* pInputElement = NULL; while (pAllElements->item(bstrTagName, vIndex, (IDispatch**)&pInputElement) == S_OK) { // 检查元素的type和name属性 CComBSTR bstrTypeValue; CComBSTR bstrNameValue; pInputElement->get_type(&bstrTypeValue); pInputElement->get_name(&bstrNameValue); if (bstrTypeValue == bstrType && bstrNameValue == bstrName) { // 获取元素的id属性 CComBSTR bstrIdValue; pInputElement->get_id(&bstrIdValue); // 在这里使用元素的id属性 break; } // 下一个元素 vIndex.lVal++; pInputElement->Release(); } pAllElements->Release(); ``` 注意,在使用完IHTMLElement接口后必须调用Release方法来释放它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值