21xrx.com
2024-09-19 09:28:56 Thursday
登录
文章检索 我的文章 写文章
如何在C++中获取IE控件表单的action和传递参数?
2023-07-05 09:07:09 深夜i     --     --
C++ IE控件 表单 action 参数

在C++中,可以使用Microsoft Visual Studio提供的MFC库来获取IE控件表单的action和传递参数。这里我们主要介绍两种方法:使用COleSafeArray类和使用IDispatch接口。

使用COleSafeArray类:

1. 首先获取IE控件的Document对象。可以通过获取IE控件的DOM树来获取Document对象。例如:

CComPtr pDoc;

CComPtr pWebBrowser = GetWebBrowser();

pWebBrowser->get_Document(&pDoc);

2. 获取表单元素对象。遍历当前Document的所有元素节点,找到表单元素。例如:

CComPtr pElements;

CComPtr pElement1;

pDoc->get_all(&pElements);

long elementCount = 0;

pElements->get_length(&elementCount);

for (long i = 0; i < elementCount; i++)

{

CComVariant index(i);

CComQIPtr pElement = NULL;

if (SUCCEEDED(pElements->item(index, index, &pElement)))

{

 CComBSTR pTagName(NULL);

 if (SUCCEEDED(pElement->get_tagName(&pTagName)) && pTagName)

 {

  if (wcscmp(pTagName, L"form") == 0)

  //找到表单对象

 }

}

}

3. 获取表单的action和传递的参数。对于表单元素,可以使用IHTMLFormElement接口来获取action和传递的参数。例如:

pElement1->QueryInterface(IID_IHTMLFormElement, (void**)&pFormElem);

BSTR bstrAction = NULL;

pFormElem->get_action(&bstrAction);

COleSafeArray vntElements;

pFormElem->get_elements(&vntElements);

LONG lDim = vntElements.GetDim();

if (lDim == 1)

{

CComVariant varIndex(0L, VT_I4);

IDispatchPtr spDisp;

vntElements.GetElement(varIndex, &spDisp);

CComQIPtr spElem(spDisp);

if (spElem != NULL)

{

 BSTR bstrType = NULL;

 CComQIPtr spInputElement(spElem);

 if (spInputElement != NULL)

 {

  //获取input类型

  spInputElement->get_type(&bstrType);

  if (wcscmp(bstrType, L"text") == 0)

  {

  //获取input文本值

  BSTR bstrName = NULL;

  spInputElement->get_name(&bstrName);

  BSTR bstrValue = NULL;

  spInputElement->get_value(&bstrValue);

  }

 }

}

}

使用IDispatch接口:

1. 首先获取WebBrowser对象的IDispatch接口,然后通过其Invoke方法执行JavaScript代码,获取表单元素和属性。例如:

CComQIPtr pDisp;

pWebBrowser->get_Document(&pDisp);

OLECHAR FAR* wcsParam = _T("document.forms[0].elements['login'].value;");

DISPPARAMS params = {0};

HRESULT hr = pDisp->Invoke(0x0, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &params, NULL, NULL, NULL);

2. 解析返回值,获取表单元素值。例如:

IDispatch *pDispResult = NULL;

_variant_t varResult;

if (V_VT(&varResult) == VT_DISPATCH)

{

pDispResult = V_DISPATCH(&varResult);

//解析返回对象

CComQIPtr spInputElement(pDispResult);

if (spInputElement != NULL)

{

 BSTR bstrType = NULL;

 spInputElement->get_type(&bstrType);

 if (wcscmp(bstrType, L"text") == 0)

 {

  //获取input文本值

  BSTR bstrName = NULL;

  spInputElement->get_name(&bstrName);

  BSTR bstrValue = NULL;

  spInputElement->get_value(&bstrValue);

 }

}

}

这些方法都可以实现在C++中获取IE控件表单的action和传递参数,具体实现方法可以根据具体需求选择合适的方法。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复