根据Internet Explorer_Server窗口得到IHtmlDocument2接口

本文介绍如何通过Internet Explorer_Server窗口深入解析,获取到IHtmlDocument2接口,从而实现对网页内容的控制和操作。
摘要由CSDN通过智能技术生成
  
#include <mshtml.h>
#include <atlbase.h>
#include <oleacc.h>
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;
};
//You can store the interface pointer in a member variable
//for easier access
void CDlg::OnGetDocInterface(HWND hWnd)
{
 CoInitialize( NULL );
 // Explicitly load MSAA so we know if it's installed
 HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
 if ( hInst != NULL )
 {
  if ( hWnd != NULL )
  {
   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&
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值