djlzxzy

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

    通过应用程序操作google搜索,用户输入要搜索的内容,然后在google中搜索;若开始时并没有IE实例运行,则打开一个默认的IE。

   

    1. 加入对Microsoft Internet Controls的引用;

    2. 加入对Microsoft HTML Object Library的引用;

    3. 通过mshtml.IHTMLDocument2、SHDocVw.InternetExplorer、SHDocVw.ShellWindowsClass获取当前打开的google搜索页面的IE窗口句柄;

    4. 根据3返回的句柄,获得当前打开的google页面的mshtml.IHTMLDocument2对象;

    5. 根据4返回的IHTMLDocument2对象,获得搜索输入框和提交按钮(可查看google页面源文件,确认输入框和提交按钮的类型和名字);

    6. 在搜索输入框中输入要搜索的内容,并执行提交按钮的click动作即可进行搜索;

 

    注:本文测试在中文系统下,若在其他语言系统下,需修改StatusText的判断。

 

    几个对象和接口的简单解释:

 

    1. ShellWindows Object
          The ShellWindows object represents a collection of the open windows that belong to the Shell. Methods are provided that can be used to control and execute commands within the Shell. There are also methods that can be used to obtain other Shell-related objects.

    2. InternetExplorer Object

          Controls a remote instance of Microsoft Internet Explorer through Automation.

 

    3. IHTMLDocument2 Interface

          Stock Implementation: mshtml.dll

          Inherits from: IDispatch interface

          Header and IDL files: Mshtml.h, Mshtml.idl

          This interface retrieves information about the document, and examines and modifies the HTML elements and text within the document.

          Typically, every window object has a corresponding document object that you can retrieve by calling the QueryInterface method with the IID_IHTMLDocument or IID_IHTMLDocument2 interface identifiers. Windows that contain HTML documents always have valid document objects, but windows that contain documents in other formats might not.

 

    4. IHTMLInputElement Interface
          Stock Implementation: mshtml.dll

          Inherits from: IDispatch interface

          Header and IDL files: Mshtml.h, Mshtml.idl 


          This interface specifies any type of input control.

    5. IHTMLElement Interface
          Stock Implementation: mshtml.dll

          Inherits from: IDispatch interface

          Header and IDL files: Mshtml.h, Mshtml.idl 


          This interface provides the ability to programmatically access the properties and methods that are common to all element objects.

 

     具体解释可参考msdn。

 

 

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8.   
  9. using System.Runtime.InteropServices;  
  10.   
  11. namespace TestAutoSearchInGoogle  
  12. {  
  13.     public partial class FrmMain : Form  
  14.     {  
  15.         public FrmMain()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.         private void exitBtn_Click(object sender, EventArgs e)  
  21.         {  
  22.             this.Close();  
  23.         }  
  24.   
  25.         private void searchBtn_Click(object sender, EventArgs e)  
  26.         {  
  27.             if(searchText.Text.Equals(""))  
  28.             {  
  29.                 MessageBox.Show("please input the search text""information prompt");  
  30.                 return;  
  31.             }  
  32.             AutoSearchInGoogle.Search(searchText.Text);  
  33.         }  
  34.     }  
  35.   
  36.     public class AutoSearchInGoogle  
  37.     {  
  38.         [DllImport("User32.dll", CharSet = CharSet.Auto)]  
  39.         public static extern int SetForegroundWindow(int hwnd);   
  40.  
  41.         #region Search   
  42.         public static void Search(string searchText)  
  43.         {  
  44.             SHDocVw.InternetExplorer ieWnd = GetIEWndOfGoogle();  
  45.             mshtml.IHTMLDocument2 ieDoc = GetIEDocOfGoogle(ref ieWnd);  
  46.   
  47.             System.Diagnostics.Trace.Assert(ieDoc != null);  
  48.             SearchTextInGoogle(ieDoc, searchText);  
  49.   
  50.             //activate ie window   
  51.             SetForegroundWindow(ieWnd.HWND);              
  52.         }   
  53.         #endregion   
  54.  
  55.         #region get ie window of google page   
  56.         public static SHDocVw.InternetExplorer GetIEWndOfGoogle()  
  57.         {  
  58.             mshtml.IHTMLDocument2 ieDoc;  
  59.             SHDocVw.InternetExplorer ieWnd = null;  
  60.             SHDocVw.ShellWindowsClass shellWindows = new SHDocVw.ShellWindowsClass();  
  61.   
  62.             foreach (SHDocVw.InternetExplorer ie in shellWindows)  
  63.             {  
  64.                 //if it is ie window   
  65.                 if (ie.FullName.ToUpper().IndexOf("IEXPLORE.EXE") > 0)  
  66.                 {  
  67.                     //get the document displayed   
  68.                     ieDoc = (mshtml.IHTMLDocument2)ie.Document;  
  69.                     if (ieDoc.title.ToUpper().IndexOf("GOOGLE") >= 0)  
  70.                     {  
  71.                         ieWnd = ie;  
  72.                         break;  
  73.                     }  
  74.                 }  
  75.             }  
  76.               
  77.             shellWindows = null;  
  78.   
  79.             return ieWnd;  
  80.         }   
  81.         #endregion   
  82.  
  83.         #region get ie document of google page   
  84.         public static mshtml.IHTMLDocument2 GetIEDocOfGoogle(ref SHDocVw.InternetExplorer ieWnd)  
  85.         {  
  86.             object missing = null;  
  87.             mshtml.IHTMLDocument2 ieDoc;  
  88.   
  89.             if (ieWnd == null)  
  90.             {  
  91.                 ieWnd = new SHDocVw.InternetExplorer();  
  92.                 ieWnd.Visible = true;  
  93.                 ieWnd.Navigate("http://www.google.com"ref missing, ref missing, ref missing, ref missing);  
  94.   
  95.                 //wait for loading completed, or using DocumentComplete Event   
  96.                 while (ieWnd.StatusText.IndexOf("完成") == -1)  
  97.                     Application.DoEvents();  
  98.             }  
  99.   
  100.             ieDoc = (mshtml.IHTMLDocument2)ieWnd.Document;  
  101.             return ieDoc;  
  102.         }   
  103.         #endregion   
  104.  
  105.         #region Search the given text in google   
  106.         ///// <summary>   
  107.         /// search the given text in google home page   
  108.         /// we can see the source file of google home page to confirm the elements we need   
  109.         /// the html file of google home page is as follows   
  110.         ///    
  111.         /// <table cellpadding=0 cellspacing=0>   
  112.         ///     <tr valign=top>   
  113.         ///         <td width=25%> </td>   
  114.         ///         <td align=center nowrap>   
  115.         ///             <input name=hl type=hidden value=zh-CN>   
  116.         ///             <input autocomplete="off" maxlength=2048 name=q size=55 title="Google 搜索" value="">   
  117.         ///             <br>   
  118.         ///             <input name=btnG type=submit value="Google 搜索">   
  119.         ///             <input name=btnI type=submit value=" 手气不错 ">   
  120.         ///         </td>   
  121.         ///         ...   
  122.         ///// </summary>           
  123.         public static void SearchTextInGoogle(mshtml.IHTMLDocument2 ieDoc, string searchText)  
  124.         {  
  125.             mshtml.HTMLInputElementClass input;  
  126.   
  127.             //set the text to be searched   
  128.             foreach (mshtml.IHTMLElement ieElement in ieDoc.all)  
  129.             {  
  130.                 //if its tag is input and name is q(question)   
  131.                 if (ieElement.tagName.ToUpper().Equals("INPUT"))  
  132.                 {  
  133.                     input = ((mshtml.HTMLInputElementClass)ieElement);  
  134.                     if (input.name == "q")  
  135.                     {  
  136.                         input.value = searchText;  
  137.                         break;  
  138.                     }  
  139.                 }  
  140.             }  
  141.   
  142.             //click the submit button to search   
  143.             foreach (mshtml.IHTMLElement ieElement in ieDoc.all)  
  144.             {  
  145.                 //if its tag is input   
  146.                 if (ieElement.tagName.ToUpper().Equals("INPUT"))  
  147.                 {  
  148.                     input = (mshtml.HTMLInputElementClass)ieElement;  
  149.                     if (input.name == "btnG")  
  150.                     {  
  151.                         input.click();  
  152.                         break;  
  153.                     }  
  154.                 }  
  155.             }  
  156.         }   
  157.         #endregion   
  158.     }  
  159. }  

 

输入DirectUIHWND,搜索结果如下:

 

 

IE编程——通过应用程序打开google并进行搜索

 

    通过应用程序操作google搜索,用户输入要搜索的内容,然后在google中搜索;若开始时并没有IE实例运行,则打开一个默认的IE。

   

    1. 加入对Microsoft Internet Controls的引用;

    2. 加入对Microsoft HTML Object Library的引用;

    3. 通过mshtml.IHTMLDocument2、SHDocVw.InternetExplorer、SHDocVw.ShellWindowsClass获取当前打开的google搜索页面的IE窗口句柄;

    4. 根据3返回的句柄,获得当前打开的google页面的mshtml.IHTMLDocument2对象;

    5. 根据4返回的IHTMLDocument2对象,获得搜索输入框和提交按钮(可查看google页面源文件,确认输入框和提交按钮的类型和名字);

    6. 在搜索输入框中输入要搜索的内容,并执行提交按钮的click动作即可进行搜索;

 

    注:本文测试在中文系统下,若在其他语言系统下,需修改StatusText的判断。

 

    几个对象和接口的简单解释:

 

    1. ShellWindows Object
          The ShellWindows object represents a collection of the open windows that belong to the Shell. Methods are provided that can be used to control and execute commands within the Shell. There are also methods that can be used to obtain other Shell-related objects.

    2. InternetExplorer Object

          Controls a remote instance of Microsoft Internet Explorer through Automation.

 

    3. IHTMLDocument2 Interface

          Stock Implementation: mshtml.dll

          Inherits from: IDispatch interface

          Header and IDL files: Mshtml.h, Mshtml.idl

          This interface retrieves information about the document, and examines and modifies the HTML elements and text within the document.

          Typically, every window object has a corresponding document object that you can retrieve by calling the QueryInterface method with the IID_IHTMLDocument or IID_IHTMLDocument2 interface identifiers. Windows that contain HTML documents always have valid document objects, but windows that contain documents in other formats might not.

 

    4. IHTMLInputElement Interface
          Stock Implementation: mshtml.dll

          Inherits from: IDispatch interface

          Header and IDL files: Mshtml.h, Mshtml.idl 


          This interface specifies any type of input control.

    5. IHTMLElement Interface
          Stock Implementation: mshtml.dll

          Inherits from: IDispatch interface

          Header and IDL files: Mshtml.h, Mshtml.idl 


          This interface provides the ability to programmatically access the properties and methods that are common to all element objects.

 

     具体解释可参考msdn。

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8.   
  9. using System.Runtime.InteropServices;  
  10.   
  11. namespace TestAutoSearchInGoogle  
  12. {  
  13.     public partial class FrmMain : Form  
  14.     {  
  15.         public FrmMain()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.         private void exitBtn_Click(object sender, EventArgs e)  
  21.         {  
  22.             this.Close();  
  23.         }  
  24.   
  25.         private void searchBtn_Click(object sender, EventArgs e)  
  26.         {  
  27.             if(searchText.Text.Equals(""))  
  28.             {  
  29.                 MessageBox.Show("please input the search text""information prompt");  
  30.                 return;  
  31.             }  
  32.             AutoSearchInGoogle.Search(searchText.Text);  
  33.         }  
  34.     }  
  35.   
  36.     public class AutoSearchInGoogle  
  37.     {  
  38.         [DllImport("User32.dll", CharSet = CharSet.Auto)]  
  39.         public static extern int SetForegroundWindow(int hwnd);   
  40.  
  41.         #region Search   
  42.         public static void Search(string searchText)  
  43.         {  
  44.             SHDocVw.InternetExplorer ieWnd = GetIEWndOfGoogle();  
  45.             mshtml.IHTMLDocument2 ieDoc = GetIEDocOfGoogle(ref ieWnd);  
  46.   
  47.             System.Diagnostics.Trace.Assert(ieDoc != null);  
  48.             SearchTextInGoogle(ieDoc, searchText);  
  49.   
  50.             //activate ie window   
  51.             SetForegroundWindow(ieWnd.HWND);              
  52.         }   
  53.         #endregion   
  54.  
  55.         #region get ie window of google page   
  56.         public static SHDocVw.InternetExplorer GetIEWndOfGoogle()  
  57.         {  
  58.             mshtml.IHTMLDocument2 ieDoc;  
  59.             SHDocVw.InternetExplorer ieWnd = null;  
  60.             SHDocVw.ShellWindowsClass shellWindows = new SHDocVw.ShellWindowsClass();  
  61.   
  62.             foreach (SHDocVw.InternetExplorer ie in shellWindows)  
  63.             {  
  64.                 //if it is ie window   
  65.                 if (ie.FullName.ToUpper().IndexOf("IEXPLORE.EXE") > 0)  
  66.                 {  
  67.                     //get the document displayed   
  68.                     ieDoc = (mshtml.IHTMLDocument2)ie.Document;  
  69.                     if (ieDoc.title.ToUpper().IndexOf("GOOGLE") >= 0)  
  70.                     {  
  71.                         ieWnd = ie;  
  72.                         break;  
  73.                     }  
  74.                 }  
  75.             }  
  76.               
  77.             shellWindows = null;  
  78.   
  79.             return ieWnd;  
  80.         }   
  81.         #endregion   
  82.  
  83.         #region get ie document of google page   
  84.         public static mshtml.IHTMLDocument2 GetIEDocOfGoogle(ref SHDocVw.InternetExplorer ieWnd)  
  85.         {  
  86.             object missing = null;  
  87.             mshtml.IHTMLDocument2 ieDoc;  
  88.   
  89.             if (ieWnd == null)  
  90.             {  
  91.                 ieWnd = new SHDocVw.InternetExplorer();  
  92.                 ieWnd.Visible = true;  
  93.                 ieWnd.Navigate("http://www.google.com"ref missing, ref missing, ref missing, ref missing);  
  94.   
  95.                 //wait for loading completed, or using DocumentComplete Event   
  96.                 while (ieWnd.StatusText.IndexOf("完成") == -1)  
  97.                     Application.DoEvents();  
  98.             }  
  99.   
  100.             ieDoc = (mshtml.IHTMLDocument2)ieWnd.Document;  
  101.             return ieDoc;  
  102.         }   
  103.         #endregion   
  104.  
  105.         #region Search the given text in google   
  106.         ///// <summary>   
  107.         /// search the given text in google home page   
  108.         /// we can see the source file of google home page to confirm the elements we need   
  109.         /// the html file of google home page is as follows   
  110.         ///    
  111.         /// <table cellpadding=0 cellspacing=0>   
  112.         ///     <tr valign=top>   
  113.         ///         <td width=25%> </td>   
  114.         ///         <td align=center nowrap>   
  115.         ///             <input name=hl type=hidden value=zh-CN>   
  116.         ///             <input autocomplete="off" maxlength=2048 name=q size=55 title="Google 搜索" value="">   
  117.         ///             <br>   
  118.         ///             <input name=btnG type=submit value="Google 搜索">   
  119.         ///             <input name=btnI type=submit value=" 手气不错 ">   
  120.         ///         </td>   
  121.         ///         ...   
  122.         ///// </summary>           
  123.         public static void SearchTextInGoogle(mshtml.IHTMLDocument2 ieDoc, string searchText)  
  124.         {  
  125.             mshtml.HTMLInputElementClass input;  
  126.   
  127.             //set the text to be searched   
  128.             foreach (mshtml.IHTMLElement ieElement in ieDoc.all)  
  129.             {  
  130.                 //if its tag is input and name is q(question)   
  131.                 if (ieElement.tagName.ToUpper().Equals("INPUT"))  
  132.                 {  
  133.                     input = ((mshtml.HTMLInputElementClass)ieElement);  
  134.                     if (input.name == "q")  
  135.                     {  
  136.                         input.value = searchText;  
  137.                         break;  
  138.                     }  
  139.                 }  
  140.             }  
  141.   
  142.             //click the submit button to search   
  143.             foreach (mshtml.IHTMLElement ieElement in ieDoc.all)  
  144.             {  
  145.                 //if its tag is input   
  146.                 if (ieElement.tagName.ToUpper().Equals("INPUT"))  
  147.                 {  
  148.                     input = (mshtml.HTMLInputElementClass)ieElement;  
  149.                     if (input.name == "btnG")  
  150.                     {  
  151.                         input.click();  
  152.                         break;  
  153.                     }  
  154.                 }  
  155.             }  
  156.         }   
  157.         #endregion   
  158.     }  
  159. }  

 

输入DirectUIHWND,搜索结果如下:

 

 

posted on 2011-08-02 23:53  djlzxzy  阅读(376)  评论(1编辑  收藏  举报