Using WebBrowser control to implement Baidu automatic search

Source: Internet
Author: User

(1) Create a new MFC dialog Box Project

(2) Add webbrower Control to the dialog box, Add Method: Click menu bar, Select Toolbox item , In the pop-Up Selection Toolbox Item dialog box, select COM component ->microsoft Web browser-> OK, add to the dialog box.




(3) Add CWebBrowser2.h and CWebBrowser2.cpp to the project,XXXdlg.h Contains the header file

#include "CWebbrowser2.h", define variable CWebBrowser2 m_web;

(4) XXXdlg.cpp add a control to a variable dynamically bound to the DoDataExchange

DDX_Control (PDX, Idc_explorer1, M_web);

(5) Add the XXXdlg.cpp initialization function

VARIANT VT;

COleVariant vaurl=_t ("http://www.baidu.com");

M_web. Navigate2 (VAURL,&VT,&VT,&VT,&VT);

Run the program, you can see the automatic loading Baidu page.


(6) XXXdlg.h Add a statement to further manipulate the page when the page is loaded

Declare_eventsink_map ()

void Documentcompleteexplorer (Lpdispatch pdisp, variant* URL);

(7) Add in XXXdlg.cpp

Begin_eventsink_map (Cbaidudlg, CDialogEx)

On_event (Cbaidudlg, Idc_explorer1, 259, Cbaidudlg::D ocumentcompleteexplorer, Vts_dispatch VTS_PVARIANT)

End_eventsink_map ()

(8) Implement Documentcompleteexplorer

Analysis Baidu source code can get the input box ID is KW1, the button ID is su1.



<span style= "FONT-SIZE:18PX;" >void Cbaidudlg::D ocumentcompleteexplorer (lpdispatch pdisp, variant* URL) {HRESULT hr; Lpunknown Lpunknown; Lpunknown LPUNKNOWNWB = NULL; Lpunknown LPUNKNOWNDC = null;ihtmlelementcollection *objallelement=null;ihtmldocument2 *objDocument=NULL; CString Strurl,strtemp;lpunknown = M_web. GetControlUnknown (); ASSERT (Lpunknown); if (lpunknown) {hr = Lpunknown->queryinterface (IID_IUnknown, (lpvoid*) &AMP;LPUNKNOWNWB); ASSERT (SUCCEEDED (HR)), if (FAILED (hr)) Return;hr = Pdisp->queryinterface (IID_IUnknown, (lpvoid*) &AMP;LPUNKNOWNDC) ; ASSERT (SUCCEEDED (HR)); if (SUCCEEDED (hr) && LPUNKNOWNWB = = LPUNKNOWNDC) {//the document has finished Loading.strurl=m_web.get_locationurl (); if (Strurl.isempty ()) {return;} Objdocument= (ihtmldocument2*) m_web.get_document (); Objdocument->get_all (&objallelement); if (StrUrl==_T (" http://www.baidu.com/")) {Ccomptr<idispatch>pdisp;objallelement->item (COleVariant (_t (" KW1 ")), COleVariant ((long) 0), &pdisp); Ccomqiptr<ihtmlElement, &iid_ihtmlelement>pelement;if (pdisp==null) {return;} Else{pelement=pdisp;pelement->put_innertext (_t ("Test"));//Fill form}variant name; CComBSTR tag; Long index; Objallelement-> get_length (&index); Name.vt=vt_i4;for (long i=0; I <index; i++)//traverse all elements, get the value {name.lval = I;idispatch * Pdispatch=null by ID property); Objalle Lement-> item (Name,name,&pdispatch); ihtmlelement* spelement; Pdispatch-> QueryInterface (Iid_ihtmlelement, (void**) &spelement); BSTR tag; SPELEMENT-&GT;GET_ID (&tag); CString SS (TAG); if (ss = = "SU1") Spelement->click (); Spelement->release ();}}} if (LPUNKNOWNWB) lpunknownwb->release (); if (LPUNKNOWNDC) lpunknowndc->release ();} </span>

Run can see Baidu automatic search


attached: VS2008 platform Source Download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.