Using WebBrowser control to implement Baidu automatic search

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/one_in_one/article/details/26372981

(1) Create a new MFC dialog Box project

(2) Add Webbrower Control to the dialog box, Add Method: Click menu Bar Tool, select Toolbox item, and in the Popup Select Toolbox Item dialog box Select COM component->microsoft Web browser-> OK, add to dialog box.

(3) Add the header file and implementation of class CWebBrowser2

Right-click Project, add Class->visual C + +, Mfc->activex control, MFC class, click Add button, add Class from registry source, available ActiveX controls select Microsoft Web browser<1.0>-> Interface Selection iwebbrowser2-> Click > button, click the Finish button, You can add CWebBrowser2.h and CWebBrowser2.cpp to your project, XXXdlg.h contains header files

#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) Realizing Documentcompleteexplorer

Analysis Baidu source code can get the input box ID is KW1, the button ID is su1. Due to Baidu page label changes, their own attention to replace.

[CPP]View Plaincopy
  1. <span style="FONT-SIZE:18PX;" >void Cbaidudlg::D ocumentcompleteexplorer (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 out 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++)//Traverse all elements, get value by ID property
  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 can see Baidu automatic search

Attached: VS2008 platform Source Download

Using WebBrowser control to implement Baidu automatic search

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.