小弟现在在VC6.0下新建了一个对话框程序,在里面加了一个WebBrowser2控件,现在窗口上有两个按钮:button1,button2
在单击button1后WebBrowser2这个控件会打开一个网页,例如www.baidu.com,现在我想实现,在单击button2时,在不移动鼠标的情况下,模拟鼠标在网页上单击了"百度"的"MP3"这个文字超级链接?请问要怎么做?
我原来想通过m_web.SendMessage(WM_LBUTTONDOWN, x, y)这种方法来实现,但好像不行,不知道为什么;网上有人也说用IHtmlElement这种方法,但都说得不够清楚,不知道有哪位高手能帮忙解决一下,小弟跪谢!

解决方案 »

  1.   

    为WebBrowser2控件导入类,就可以操纵其接口,用Document方法获取页面,然后获取网页元素,查找到你需要的链接,获取链接的页面地址,然后用Navigate方法打开页面即可。
      

  2.   

    楼上的方法不行,楼主的是要模拟。用IHtmlElement是正确的。
      

  3.   

    我是用C++ builder 写的,有些类型需要改一下,希望能对你有帮助bool  ClickLinks(WideString strLinkName,IDispatch* pDisp)
    {
       if(pDisp != NULL)
            {
                    IHTMLDocument2* pHTMLDocument2=NULL;
                    HRESULT hr;
                    hr = pDisp->QueryInterface(IID_IHTMLDocument2,
                    (void**)&pHTMLDocument2);                if(hr == S_OK)
                    {
                            IHTMLElementCollection* pColl = NULL;
                            hr = pHTMLDocument2->get_all(&pColl);
                            if(hr==S_OK && pColl!=NULL)
                            {
                                    LONG celem;
                                    hr = pColl->get_length(&celem);
                                    if(hr == S_OK)
                                    {
                                            for(int i=0;i<celem;i++)
                                            {
                                               VARIANT varIndex;
                                               varIndex.vt = VT_UINT;
                                               varIndex.lVal = i;
                                               VARIANT var2;
                                               VariantInit(&var2);
                                               IDispatch* pDisp;
                                               hr = pColl->item(varIndex,var2,&pDisp);                                           if(hr == S_OK)
                                               {
                                                             IHTMLElement* pElem;
                                                             hr = pDisp->QueryInterface(IID_IHTMLElement,
                                                             (void**)&pElem);
                                                             if(hr == S_OK)
                                                             {                                                           //BSTR bstr;
                                                             //  hr=pElem->get_tagName(&bstr);
                                                            //   WideString strTag;
                                                           //    strTag = bstr;
                                                               //--------------------------------
                                                    IHTMLAnchorElement* pLink;
                                                    hr = pDisp->QueryInterface(
                                                        IID_IHTMLAnchorElement,
                                                        (void **)&pLink );                                                if ( hr == S_OK )
                                                       {
                                                        BSTR bstr;
                                                        hr = pLink->get_href(&bstr);
                                                        if(hr == S_OK)
                                                        {
                                                       
                                                        WideString mStr = bstr;
                                                        if(mStr.Pos(strLinkName)>=0)
                                                          {
                                                          //Is this a User Id frield
                                                              pElem->click();
                                                              i=celem;
                                                              //Paste the User Id
                                                          }
                                                        }
                                                        pLink->Release();
                                                    }
                                                      //---------------------------
                                                             }
                                               }                                        }
                                            pColl->Release();
                                    }
                                    pHTMLDocument2->Release();
                            }                }       pDisp->Release();        }
            return true;
    }
      

  4.   

    lz可以直接在button2的处理中将browser->navigate到“http://mp3.baidu.com/”就行了。
      

  5.   

    自己的部分代码, 可能比较乱这个是找到一个按钮名字:B2 类型:submit 实现点击(www.hao123.com 坐下角的搜索按钮)
    BOOL bFound = FALSE;_bstr_t bStrWindowName;void ProcessElement(IHTMLElement * pElt)
    {
    CComPtr<IHTMLElement> pElement;
    pElement = pElt;
    _bstr_t bStrName, bStrTitle;
    CComBSTR cbName, cbTitle; pElement->get_tagName(&cbName);//获取tagName
    bStrName = cbName;
    pElement->get_title(&cbTitle);
    bStrTitle = cbTitle;
    LogStr("d:\\Logstr.log", " IHTMLElement: get_tagName: %s get_title: %s", 
    (CHAR *)bStrName, (CHAR *)bStrTitle); if(bFound)
    {
    pElement->click();
    bFound = FALSE;
    LogStr("d:\\Logstr.log", "click: get_tagName: %s get_title: %s", 
    (CHAR *)bStrName, (CHAR *)bStrTitle); }}
    void ProcessInputElement(IHTMLInputElement * pInput)
    {
    CComPtr<IHTMLInputElement> pInputElement;
    pInputElement = pInput; _bstr_t bStrName, bStrType;
    CComBSTR cbName, cbType; pInputElement->get_name(&cbName);
    bStrName = cbName;
    pInputElement->get_type(&cbType);
    bStrType = cbType; LogStr("d:\\Logstr.log", " IHTMLInputElement: get_name: %s get_type: %s", 
    (CHAR *)bStrName, (CHAR *)bStrType); if(bStrName.length() == 0)
    return;
    if(bStrType.length() == 0)
    return; if(stricmp("B2", bStrName) == 0
    && stricmp("submit", bStrType) == 0)
    {
    bFound = TRUE;
    LogStr("d:\\Logstr.log", "Found: IHTMLInputElement: get_name: %s get_type: %s", 
    (CHAR *)bStrName, (CHAR *)bStrType);
    }}void CAutoRefreshDlg::OnButtonRefresh() 
    {
    VARIANT id, index;
    CComPtr<IDispatch> spDispatch;
    CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
    CComPtr<IHTMLElement> pElement;
    CComPtr<IHTMLInputElement> pInput;

    CComPtr<IHTMLElementCollection> pElementCol;
    CComPtr<IHTMLFormElement> pFormElement;
    CComPtr<IHTMLInputTextElement> pInputElement;
    _bstr_t bStrText; // TODO: Add your control notification handler code here
    int n = m_ctrlIE.GetItemCount();//GetCount(); for (int i = 0; i < n; i ++)
    {
    IWebBrowser2 *pBrowser = (IWebBrowser2 *)m_ctrlIE.GetItemData(i);
    if (pBrowser)
    {
    pBrowser->Release();
    }
    } m_ctrlIE.DeleteAllItems();
    LogStr("d:\\Logstr.log", (CHAR *)NULL); if(m_spSHWinds == NULL)
    return;
    n = m_spSHWinds->GetCount();
    for (int i = 0; i < n; i++)
    {
    _variant_t v = (long)i;
    IDispatchPtr spDisp = m_spSHWinds->Item(v); SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);   //生成一个IE窗口的智能指针
    if (spBrowser == NULL)
    continue; spDispatch.Release();
    if (SUCCEEDED(spBrowser->get_Document( &spDispatch)))
    pDoc2 = spDispatch;
    if(pDoc2==NULL)
    continue;
    CComBSTR value;
    pDoc2->get_cookie(&value);
    bStrText = "cookie: ";
    bStrText += (BSTR)value;
    m_ctrlIE.InsertItem(0, bStrText);
    pElementCol.Release();
    if (FAILED(pDoc2->get_forms(&pElementCol)))
    continue; // AfxMessageBox("IHTMLElementCollection");
    long p=0;
    if(FAILED(pElementCol->get_length(&p)))
    continue;
    if(p == 0)
    continue;#if 1
    _bstr_t bStrWindowName = spBrowser->GetLocationName();   //窗口名称 LogStr("d:\\Logstr.log", "Window: %s", (CHAR *)bStrWindowName); int nPos = m_ctrlIE.InsertItem(0, bStrWindowName);
    // spBrowser->AddRef();
    // void * pData = spBrowser;
    // m_ctrlIE.SetItemData(nPos, (DWORD)(pData));
    #endif // AfxMessageBox("1");
    for(long i=0;i<=(p-1);i++)
    { V_VT(&id) = VT_I4;
    V_I4(&id) = i;
    V_VT(&index) = VT_I4;
    // V_I4(&index) = 0;
    V_I4(&index) = i;
    spDispatch.Release();
    if(FAILED(pElementCol->item(id,index, &spDispatch)))
    continue; // AfxMessageBox("2");
    pFormElement.Release();
    if(FAILED(spDispatch->QueryInterface(IID_IHTMLFormElement,(void**)&pFormElement)))
    continue;
    // AfxMessageBox("IHTMLFormElement");
    long q=0;
    if(FAILED(pFormElement->get_length(&q)))
    continue;
    for(long j=0;j<=(q-1);j++)
    {
    V_VT(&id) = VT_I4;
    V_I4(&id) = j;
    V_VT(&index) = VT_I4;
    V_I4(&index) = 0;
    spDispatch.Release();
    if(FAILED(pFormElement->item(id,index, &spDispatch)))
    continue; pInput.Release();
    if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLInputElement,(void**)&pInput)))
    {
    ProcessInputElement(pInput);
    } pElement.Release();
    if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLElement,(void**)&pElement)))
    {
    ProcessElement(pElement);
    } pInputElement.Release();
    if(FAILED(spDispatch->QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputElement)))
    continue; //AfxMessageBox("IHTMLInputTextElement");
    CComBSTR value;
    CComBSTR type;
    CComBSTR name; pInputElement->get_type(&type); CString strtype(type);
    strtype.MakeUpper();
    if(strtype.Find("TEXT")!=-1)
    {
    CComPtr<IHTMLFormElement> pDict;
    pInputElement->get_value(&value); pInputElement->get_form(&pDict); CString str(value);
    if(!str.IsEmpty())
    m_ctrlIE.InsertItem(0, _bstr_t(value)+_bstr_t("  【strtype:TEXT】")); }
    else if(strtype.Find("PASSWORD")!=-1)
    {
    pInputElement->get_value(&value);
    CString str(value);
    if(!str.IsEmpty())
    m_ctrlIE.InsertItem(0, _bstr_t(value) + _bstr_t("  【strtype:PASSWORD】"));
    }
    }
    } }
    }
      

  6.   

    SendInput
    http://www.codeguru.com/forum/showthread.php?t=377394