MFC下进行对HTML网页Select选择按钮的控制

来源:互联网 发布:2016淘宝开学季 编辑:程序博客网 时间:2024/06/08 18:07
void CHTMLContrlDlg::OnBnClickedButtonSelect(){if (spdoc.hWndChild == NULL)return;//spdoc.hWndChild为放置IE的窗口句柄,自己根据自己的窗口获取CComPtr < IHTMLElementCollection > spElementCollection;CComPtr < IHTMLDocument2 > spDoc;LRESULT lRes;UINT nMsg = ::RegisterWindowMessage(_T("WM_HTML_GETOBJECT"));::SendMessageTimeout(spdoc.hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&(lRes));HRESULT hr = ::ObjectFromLresult(lRes, IID_IHTMLDocument2, 0, (LPVOID *)&(spDoc));if (FAILED(hr)) return;    hr = spDoc->get_all(&spElementCollection);if (FAILED(hr)){return;}long elementLength;hr = spElementCollection->get_length(&elementLength);if (FAILED(hr)){return;}    VARIANT name;CComBSTR tag;name.vt = VT_I4;for (int i = 0; i < elementLength; i++){name.lVal = i;IDispatch * pDispatch = NULL;HRESULT res = spElementCollection->item(name, name, &pDispatch);if (FAILED(res)){continue;}CComPtr<IHTMLSelectElement> pHtmlElement;hr = pDispatch->QueryInterface(IID_IHTMLSelectElement, (void**)&pHtmlElement);if (FAILED(hr)){continue;}BSTR name;BSTR bValue;long value;long selectedIndex=2;pHtmlElement->get_name(&name);pHtmlElement->get_value(&bValue);pHtmlElement->get_length(&value);pHtmlElement->put_selectedIndex(selectedIndex);i += value; }}