C#控制IE6浏览器
IE编程——应用程序操作IE6
应用程序对IE6的操作分类:
1. 打开一个新的IE6窗口,并显示指定的页面;
2. 读取当前运行的所有IE6进程及其显示的页面url;
3. 读取指定的IE6窗口对应的IE6进程调用的模块及其线程;
4. 更新指定的IE6窗口url;
5. 关闭指定的IE6窗口;
将IE6操作封装为OperationOnIE6类,并将其用到的windows消息常量、windows API等也封装为类,分别为:ConstData、Win32API、CommonIEOperation,并指定这些类的命名空间为 AutoOperationOnIE6,具体可参考代码(AutoOperationOnIE6.cs文件)。
using System; using System.Text; using Microsoft.Win32; using System.Threading; using System.Collections; using System.Diagnostics; using System.Runtime.InteropServices; namespace AutoOperationOnIE6 { public class ConstData { #region define message public const int WM_SETTEXT = 0x000C; //set text public const int WM_GETTEXT = 0x000D; //get text public const int WM_KEYDOWN = 0x0100; //key down public const int WM_KEYUP = 0x0101; //key up public const int WM_ACTIVATE = 0x0006; //activate window public const int VK_RETURN = 0x0D; //key value of return #endregion } public class Win32API { #region api function [DllImport("User32.dll")] public static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("User32.dll")] public static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam); [DllImport("User32.dll")] public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam); #endregion } public class CommonIEOperation { #region variable private static string defaultBrowserName; //default ie browser name private static string defaultBrowserPath; //default ie browser path #endregion #region Properties public static string DefaultBrowserName { get { return defaultBrowserName; } set { defaultBrowserName = value; } } public static string DefaultBrowserPath { get { return defaultBrowserPath; } set { defaultBrowserPath = value; } } #endregion #region get default browser public static void GetDefaultBrowser() { string strIE = string.Empty; string strIEExe = string.Empty; string[] strsegs; try { RegistryKey regKey = Registry.ClassesRoot; regKey = regKey.OpenSubKey(@"http/shell/open/command"); strIE = regKey.GetValue("").ToString(); strsegs = strIE.Split(new char[1] { '"' }); foreach (string temp in strsegs) { if (temp.Contains(".exe") || temp.Contains(".EXE")) { strIEExe = temp.ToUpper(); break; } } int startPos = strIEExe.LastIndexOf(@"/"); int endPos = strIEExe.IndexOf(".EXE"); if (endPos > startPos) { defaultBrowserName = strIEExe.Substring(startPos + 1, (endPos - startPos) - 1); } defaultBrowserPath = strIEExe.Substring(0, strIEExe.IndexOf(".EXE") + 4); } catch (Exception) { defaultBrowserName = string.Empty; defaultBrowserPath = string.Empty; } } #endregion #region get version of IE private static int GetIEVersion() { try { RegistryKey regKey = Registry.LocalMachine; regKey = regKey.OpenSubKey(@"SOFTWARE/Microsoft/Internet Explorer"); string IEVersion = regKey.GetValue("Version").ToString(); string strVersion = IEVersion.Substring(0, 1); return int.Parse(strVersion); } catch (Exception) { return 6; } } #endregion } public class OperationOnIE6 { #region new a window with url public static Process NewUrl(string defaultIE,string url) { Process p = new Process(); p.StartInfo.FileName = defaultIE; p.StartInfo.Arguments = url; p.Start(); return p; } #endregion #region get url public static string GetUrl(int hIEWnd) { //using spy++ to find the type of window int hCtrlWnd = Win32API.FindWindowEx(hIEWnd, 0, "WorkerW", null); hCtrlWnd = Win32API.FindWindowEx(hCtrlWnd, 0, "ReBarWindow32", null); hCtrlWnd = Win32API.FindWindowEx(hCtrlWnd, 0, "ComboBoxEx32", null); hCtrlWnd = Win32API.FindWindowEx(hCtrlWnd, 0, "ComboBox", null); hCtrlWnd = Win32API.FindWindowEx(hCtrlWnd, 0, "Edit", null); StringBuilder buffer = new StringBuilder(1024); //send message to get url int num = Win32API.SendMessage(hCtrlWnd, ConstData.WM_GETTEXT, 1024, buffer); return buffer.ToString(); } #endregion #region set url public static void SetUrl(int hIEWnd, StringBuilder strUrl) { //using spy++ to find the type of window int hCtrlWnd = Win32API.FindWindowEx(hIEWnd, 0, "WorkerW", null); hCtrlWnd = Win32API.FindWindowEx(hCtrlWnd, 0, "ReBarWindow32", null); hCtrlWnd = Win32API.FindWindowEx(hCtrlWnd, 0, "ComboBoxEx32", null); hCtrlWnd = Win32API.FindWindowEx(hCtrlWnd, 0, "ComboBox", null); int hUrlWnd = Win32API.FindWindowEx(hCtrlWnd, 0, "Edit", null); //set IE url Win32API.SendMessage(hUrlWnd, ConstData.WM_SETTEXT, 0, strUrl); //refresh IE Win32API.SendMessage(hUrlWnd, ConstData.WM_KEYDOWN, ConstData.VK_RETURN, null); //activate the window Win32API.SendMessage(hIEWnd, ConstData.WM_ACTIVATE, 0, null); } #endregion } }
编写应用程序,调用命名空间AutoOperationOnIE6的类。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Collections; using System.Diagnostics; using System.Runtime.InteropServices; using AutoOperationOnIE6; namespace TestIE6Operation { public partial class FrmMain : Form { private ArrayList ie6Process = new ArrayList(); public FrmMain() { InitializeComponent(); } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void btnClear_Click(object sender, EventArgs e) { urlList.Items.Clear(); tbInfo.Clear(); tbModules.Clear(); tbThreads.Clear(); ie6Process.Clear(); tbUrl.Text = "about:blank"; } private void btnGetUrl_Click(object sender, EventArgs e) { ie6Process.Clear(); urlList.Items.Clear(); Process[] process = Process.GetProcessesByName("IEXPLORE"); foreach (Process curie in process) { IntPtr ieHandle = curie.MainWindowHandle; string strurl = OperationOnIE6.GetUrl(ieHandle.ToInt32()); ie6Process.Add(curie); urlList.Items.Add(strurl); } } private void urlList_MouseDoubleClick(object sender, MouseEventArgs e) { if (urlList.Items.Count==0) return; int index = urlList.SelectedIndex; Process curprocess = (Process)ie6Process[index]; string strinfo = string.Empty; strinfo += "machine name : " + curprocess.MachineName + "/r/n"; strinfo += "main module : " + curprocess.MainModule.ToString() + "/r/n"; strinfo += "process id : " + curprocess.Id.ToString() + "/r/n"; strinfo += "window handle: " + curprocess.MainWindowHandle.ToString() + "/r/n"; strinfo += "window title : " + curprocess.MainWindowTitle; tbInfo.Text = strinfo; strinfo = string.Empty; ProcessModuleCollection modules = curprocess.Modules; for (int i = 0; i < modules.Count;i++ ) { strinfo += "module Name : " + modules[i].ModuleName + "/r/n"; strinfo += "file Name : " + modules[i].FileName + "/r/n"; strinfo += "file Version: " + modules[i].FileVersionInfo.FileVersion + "/r/n"; strinfo += "company name: " + modules[i].FileVersionInfo.CompanyName + "/r/n"; strinfo += "comments : " + modules[i].FileVersionInfo.Comments + "/r/n/r/n"; } tbModules.Text = strinfo; strinfo = string.Empty; ProcessThreadCollection threads = curprocess.Threads; for (int i = 0; i < threads.Count; i++) { strinfo += "thread id : " + threads[i].Id + "/r/n"; strinfo += "os kernel : " + threads[i].PrivilegedProcessorTime.ToString() + "/r/n"; strinfo += "start time: " + threads[i].StartTime+ "/r/n"; strinfo += "total time: " + threads[i].TotalProcessorTime.ToString() + "/r/n"; strinfo += "user time : " + threads[i].UserProcessorTime+ "/r/n/r/n"; } tbThreads.Text = strinfo; } private void btnNewUrl_Click(object sender, EventArgs e) { if(tbUrl.Text==string.Empty) { MessageBox.Show("please input a url", "prompt information", MessageBoxButtons.OK, MessageBoxIcon.Information); tbUrl.Focus(); return; } CommonIEOperation.GetDefaultBrowser(); Process p = OperationOnIE6.NewUrl(CommonIEOperation.DefaultBrowserPath, tbUrl.Text); urlList.Items.Add(tbUrl.Text); ie6Process.Add(p); } private void btnSetUrl_Click(object sender, EventArgs e) { if (urlList.Items.Count == 0) return; if(urlList.SelectedIndex<0) { MessageBox.Show("please select a url", "prompt information", MessageBoxButtons.OK, MessageBoxIcon.Information); urlList.Focus(); return; } if (tbUrl.Text == string.Empty) { MessageBox.Show("please input a url", "prompt information", MessageBoxButtons.OK, MessageBoxIcon.Information); tbUrl.Focus(); return; } int index = urlList.SelectedIndex; int hIEWnd=((Process)ie6Process[index]).MainWindowHandle.ToInt32(); StringBuilder strUrl = new StringBuilder(); strUrl.Append(tbUrl.Text); OperationOnIE6.SetUrl(hIEWnd, strUrl); urlList.Items[index] = strUrl; } private void btnDelete_Click(object sender, EventArgs e) { if (urlList.Items.Count > 0) { if (urlList.SelectedIndex < 0) { MessageBox.Show("please select a url", "prompt information", MessageBoxButtons.OK, MessageBoxIcon.Information); urlList.Focus(); return; } int index = urlList.SelectedIndex; ((Process)ie6Process[index]).Kill(); ie6Process.RemoveAt(index); urlList.Items.RemoveAt(index); if (index > 0) urlList.SelectedIndex = index - 1; else { if (urlList.Items.Count > 0) urlList.SelectedIndex = 0; else urlList.SelectedIndex = -1; } } } } }
运行结果如下:
来源:http://blog.csdn.net/livelylittlefish/article/details/2956816
加支付宝好友偷能量挖...