Use the WebBrowser control to implement Baidu automatic search and webbrowser Control

Source: Internet
Author: User

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
  1. <Span style = "font-size: 18px;"> void CbaiduDlg: DocumentCompleteExplorer (LPDISPATCH pDisp, VARIANT * URL)
  2. {
  3. HRESULT hr;
  4. LPUNKNOWN lpUnknown;
  5. LPUNKNOWN lpUnknownWB = NULL;
  6. LPUNKNOWN lpUnknownDC = NULL;
  7. IHTMLElementCollection * objAllElement = NULL;
  8. IHTMLDocument2 * objDocument = NULL;
  9. CString strUrl, strTemp;
  10. LpUnknown = m_web.GetControlUnknown ();
  11. ASSERT (lpUnknown );
  12. If (lpUnknown)
  13. {
  14. Hr = lpUnknown-> QueryInterface (IID_IUnknown, (LPVOID *) & lpUnknownWB );
  15. ASSERT (SUCCEEDED (hr ));
  16. If (FAILED (hr ))
  17. Return;
  18. Hr = pDisp-> QueryInterface (IID_IUnknown, (LPVOID *) & lpUnknownDC );
  19. ASSERT (SUCCEEDED (hr ));
  20. If (SUCCEEDED (hr) & lpUnknownWB = lpUnknownDC)
  21. {
  22. // The document has finished loading.
  23. StrUrl = m_web.get_LocationURL ();
  24. If (strUrl. IsEmpty ())
  25. {
  26. Return;
  27. }
  28. ObjDocument = (IHTMLDocument2 *) m_web.get_Document ();
  29. ObjDocument-> get_all (& objAllElement );
  30. If (strUrl = _ T ("http://www.baidu.com /"))
  31. {
  32. CComPtr <IDispatch> pDisp;
  33. ObjAllElement-> item (COleVariant (_ T ("kw1"), COleVariant (long) 0), & pDisp );
  34. CComQIPtr <IHTMLElement, & IID_IHTMLElement> pElement;
  35. If (pDisp = NULL)
  36. {
  37. Return;
  38. }
  39. Else
  40. {
  41. PElement = pDisp;
  42. PElement-> put_innerText (_ T ("test"); // fill the form
  43. }
  44. VARIANT name;
  45. CComBSTR tag;
  46. Long index;
  47. ObjAllElement-> get_length (& index );
  48. Name. vt = VT_I4;
  49. For (long I = 0; I <index; I ++) // traverses all elements and obtains values through the id attribute.
  50. {
  51. Name. lVal = I;
  52. IDispatch * pDispatch = NULL;
  53. ObjAllElement-> item (name, name, & pDispatch );
  54. IHTMLElement * spElement;
  55. PDispatch-> QueryInterface (IID_IHTMLElement, (void **) & spElement );
  56. BSTR tag;
  57. SpElement-> get_id (& tag );
  58. CString ss (tag );
  59. If (ss = "su1 ")
  60. SpElement-> click ();
  61. SpElement-> Release ();
  62. }
  63. }
  64. }
  65. }
  66. If (lpUnknownWB)
  67. LpUnknownWB-> Release ();
  68. If (lpUnknownDC)
  69. LpUnknownDC-> Release ();
  70. } </Span>

Run the command to view Baidu's automatic search

Attachment: Download the source code of VS2008 Platform

Related Article

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.