using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Threading; namespace ExtendedWebBrowser2 { /// /// /// public class Command { private ExtendedWebBrowser _Browser; /// /// /// /// public Command(ExtendedWebBrowser host) { _Browser = host; } [DllImport("User32.DLL")] private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); [DllImport("User32.DLL")] private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); private readonly int IDM_VIEWSOURCE = 2139; private readonly uint WM_COMMAND = 0x0111; /// /// 查看源码 /// public void ViewSource() { IntPtr vHandle = _Browser.Handle; vHandle = FindWindowEx(vHandle, IntPtr.Zero, "Shell Embedding", null); vHandle = FindWindowEx(vHandle, IntPtr.Zero, "Shell DocObject View", null); vHandle = FindWindowEx(vHandle, IntPtr.Zero, "Internet Explorer_Server", null); SendMessage(vHandle, WM_COMMAND, IDM_VIEWSOURCE, (int)_Browser.Handle); } private void ExecCommand(string command) { _Browser.Document.ExecCommand(command, false, null); } /// /// 复制当前选中区 /// public void Copy() { ExecCommand("Copy"); } /// /// 粘贴当前选中区 /// public void Paste() { ExecCommand("Paste"); } /// /// 剪切当前选中区 /// public void Cut() { ExecCommand("Cut"); } /// /// 全选 /// public void SelectAll() { ExecCommand("SelectAll"); } /// /// 清除当前选中区的选中状态。 /// public void UnSelect() { ExecCommand("Unselect"); } /// /// 从当前选中区中删除全部超级链接。 /// public void UnLink() { ExecCommand("Unlink"); } /// /// 从当前选中区中删除全部书签。 /// public void UnBookmark() { ExecCommand("UnBookmark"); } /// /// 删除当前选中区 /// public void Delete() { ExecCommand("Delete"); } /// /// 重做 /// public void Redo() { ExecCommand("Redo"); } /// /// 撤销 /// public void Undo() { ExecCommand("Undo"); } /// /// 从当前选中区中删除格式化标签。 /// public void RemoveFormat() { ExecCommand("RemoveFormat"); } /// /// 点击节点 /// /// /// /// public void ClickElement(string tagName, string attr, string attrValue) { HtmlElementCollection list = _Browser.Document.GetElementsByTagName(tagName); for (int i = 0; i < list.Count; i++) { if (list[i].GetAttribute(attr).ToLower() == attrValue.ToLower()) { list[i].InvokeMember("Click"); break; } } } /// /// 点击节点 /// /// /// /// /// public void ClickElement(string tagName, string attr, string attrValue, int waitMilliSecond) { HtmlElementCollection list = _Browser.Document.GetElementsByTagName(tagName); for (int i = 0; i < list.Count; i++) { if (list[i].GetAttribute(attr).ToLower() == attrValue.ToLower()) { list[i].InvokeMember("Click"); Wait(waitMilliSecond); break; } } } /// /// 点击元素 /// /// 节点名 /// 属性 /// 属性值 /// 等待毫秒数 /// 第几个符合的元素,从1开始 public void ClickElement(string tagName, string attr, string attrValue, int waitMilliSecond,int index) { HtmlElementCollection list = _Browser.Document.GetElementsByTagName(tagName); int _index = 0; for (int i = 0; i < list.Count; i++) { if (list[i].GetAttribute(attr).ToLower() == attrValue.ToLower()) { if (_index == index) { // list[i].InvokeMember("Click"); list[i].InvokeMember("Click"); Wait(waitMilliSecond); break; } _index++; } } } /// /// 等待多时毫秒 /// /// public void Wait(int Milliseconds) { if (Milliseconds <= 0) { return; } DateTime startTime = DateTime.Now; while (true) { if (DateTime.Now >= startTime.AddMilliseconds(Milliseconds) && Milliseconds > 0) { break; } System.Windows.Forms.Application.DoEvents(); } } } }