Use the WebBrowser control to implement Baidu automatic search and webbrowser Control
(1) Create an MFC dialog box project
(2) Add the WebBrower control in the dialog box. Add the following method: choose tool> toolbox> select COM component> Microsoft Web Browser> OK in the pop-up toolbox and add it to the dialog box.
(3) add header files and implementations of class CWebBrowser2
Right-click Project> Add> class> Visual C ++> MFC> ActiveX Control> MFC class> Add class from registry source, select Microsoft Web Browser <1.0>-> IWebBrowser2 for available ActiveX controls-> click> button-> Click Finish to add CWebBrowser2.h and CWebBrowser2.cpp to the project, XXXdlg. h contains header files
# Include "CWebbrowser2.h", define the variable CWebBrowser2 m_web;
(4) Add the control and variable dynamic binding in DoDataExchange of XXXdlg. cpp
DDX_Control (pDX, idc_assumer1, m_web );
(5) add XXXdlg. cpp to the initialization Function
VARIANT vt;
COleVariant vaUrl = _ T ("http://www.baidu.com ");
M_web.Navigate2 (vaUrl, & vt );
Run the program and you can see that the Baidu page is automatically loaded.
(6) Add a declaration in XXXdlg. h to perform further operations on the page after the page is loaded.
DECLARE_EVENTSINK_MAP ()
Void DocumentCompleteExplorer (LPDISPATCH pDisp, VARIANT * URL );
(7) add XXXdlg. cpp
BEGIN_EVENTSINK_MAP (CbaiduDlg, CDialogEx)
ON_EVENT (CbaiduDlg, idc_trigger1, 259, CbaiduDlg: DocumentCompleteExplorer, VTS_DISPATCH VTS_PVARIANT)
END_EVENTSINK_MAP ()
(8) Implement DocumentCompleteExplorer
After analyzing Baidu source code, we can see that the input box id is kw1 and the button id is su1. Due to the changes in Baidu page labels, pay attention to the replacement.
[Cpp]View plain copy
- <Span style = "font-size: 18px;"> void CbaiduDlg: DocumentCompleteExplorer (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 *) & lpUnknownWB );
- ASSERT (SUCCEEDED (hr ));
- If (FAILED (hr ))
- Return;
- Hr = pDisp-> QueryInterface (IID_IUnknown, (LPVOID *) & 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 the form
- }
- VARIANT name;
- CComBSTR tag;
- Long index;
- ObjAllElement-> get_length (& index );
- Name. vt = VT_I4;
- For (long I = 0; I <index; I ++) // traverses all elements and obtains values through the id attribute.
- {
- Name. lVal = I;
- IDispatch * pDispatch = NULL;
- ObjAllElement-> item (name, name, & pDispatch );
- IHTMLElement * spElement;
- PDispatch-> QueryInterface (IID_IHTMLElement, (void **) & spElement );
- BSTR tag;
- SpElement-> get_id (& tag );
- CString ss (tag );
- If (ss = "su1 ")
- SpElement-> click ();
- SpElement-> Release ();
- }
- }
- }
- }
- If (lpUnknownWB)
- LpUnknownWB-> Release ();
- If (lpUnknownDC)
- LpUnknownDC-> Release ();
- } </Span>
Run the command to view Baidu's automatic search
Attachment: Download the source code of VS2008 Platform