static void Main(string[] args) { SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); //创建Microsoft Internet Controls对象 string filename; string IeTitle = "Google";//IE窗口标题,自己设定 foreach (SHDocVw.InternetExplorer ie in shellWindows) //遍历IE游览器和文件游览器窗口 { filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();//提取游览器程序的文件名,并转化为小写 if (filename.Equals("iexplore") && IeTitle.Equals(ie.LocationName)) //如果文件名等于IE AND 窗口标题是 IeTitle 则提取出网页内容 { Console.WriteLine("网址: {0}", ie.LocationURL); mshtml.IHTMLDocument2 htmlDoc = ie.Document as mshtml.IHTMLDocument2; //提取出网页内容 mshtml.IHTMLElement input = (mshtml.IHTMLElement)htmlDoc.all.item("q", 0); //找到页面的中输入框 mshtml.IHTMLElement input2 = (mshtml.IHTMLElement)htmlDoc.all.item("btnG", 0); //找到页面中的提交按钮 input.title = "请在此输入你想要搜索的内容"; //给他们增加提示 input2.title = "这个提交按钮,点此开始搜索"; Console.WriteLine("网页内容: {0}", ((htmlDoc != null) ? htmlDoc.body.outerHTML : "***Failed***")); //输出代码 } } Console.ReadKey(); }
public void AddShortcutFolder() { SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); string filename = ""; foreach (SHDocVw.InternetExplorer ie in shellWindows) { if (ie.LocationURL != "") { filename = ie.LocationURL; } } if (shellWindows.Count > 0) { int n = filename.Length; if (n > 0) { filename = filename.Replace("file:///", ""); filename = filename.Replace("%20", " "); filename = filename.Replace("%26", "&"); } else { filename = ""; } } ShortcutInfo inf = new ShortcutInfo(); inf.Url = filename; while (!ShortcutAskingProcess(ref inf)) { ; } }
private string GetWindowsExplorerPathFromWindowHandle(int handle) { // Add a project COM reference to Microsoft Internet Controls 1.1 SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); string fileName; string path = ""; foreach (SHDocVw.InternetExplorer ie in shellWindows) { fileName = Path.GetFileNameWithoutExtension(ie.FullName).ToLower(); if (fileName.Equals("explorer") && ie.HWND == handle) { path = ie.LocationURL; path = path.ToLower(); path = path.Replace("file://", ""); if (path.StartsWith("/")) { path = path.Substring(1); } path = path.Replace("/", "\\"); if (!path.Contains(":")) // unc paths { path = "\\\\" + path; } break; } } return(path); }
/** * 单张票据识别 */ public BillHtml getBillInfoHtml() { BillHtml billHtml = new BillHtml(); SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); foreach (SHDocVw.InternetExplorer ie in shellWindows) { string url = ie.LocationURL; if (url.Contains("https://ent.cmbc.com.cn:8443/eweb/static/commonPage/DraftDetail.html")) //民生银行 { billHtml.GetBankContent = new BankContentCmbc(); } if (billHtml.GetBankContent != null) { mshtml.IHTMLDocument2 htmlDoc = (mshtml.IHTMLDocument2)ie.Document; string aa = htmlDoc != null?htmlDoc.body.outerHTML.ToString() : "***Failed***"; billHtml.HtmlBody = aa; break; } } return(billHtml); }
private SHDocVw.InternetExplorer GetWindowsExplorer(IntPtr hWnd) { SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); string filename; foreach (SHDocVw.InternetExplorer ie in shellWindows) { try { if (File.Exists(ie.FullName)) { filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower(); if (filename.Equals("explorer")) { try { if (ie.HWND == hWnd.ToInt32()) { return(ie); } } catch { } } } } catch { // POSSIBLE PROBLEM HERE } } return(null); }
public static treeelement[] GetRootElements() { SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); SHDocVw.WebBrowser Browser = null; mshtml.HTMLDocument Document = null; foreach (SHDocVw.InternetExplorer _ie in shellWindows) { var filename = System.IO.Path.GetFileNameWithoutExtension(_ie.FullName).ToLower(); if (filename.Equals("iexplore")) { //Debug.WriteLine("Web Site : {0}", _ie.LocationURL); try { Browser = _ie as SHDocVw.WebBrowser; Document = (Browser.Document as mshtml.HTMLDocument); } catch (Exception ex) { Log.Error(ex, ""); } } } if (Document == null) { Log.Warning("Failed locating an Internet Explore instance"); return(new treeelement[] { }); } var e = new IETreeElement(null, true, new IEElement(Document.documentElement)); return(new treeelement[] { e }); }
public void AddWeblink() { SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); string filename = "www."; foreach (SHDocVw.InternetExplorer ie in shellWindows) { filename = ie.LocationURL; break; } int n = filename.IndexOf("file:///"); if (n == 0) { filename = "www."; } ShortcutInfo inf = new ShortcutInfo(); if (AskShortCutInfo(ref inf, "Enter new weblink", m_lWeblink, true)) { m_lWeblink.Add(inf); SortShortcutInfo(m_lWeblink); UpdateScriptWeblink(); TransferProfileInCurrentScript(); } }
public static Browser GetBrowser(string url = null) { var result = new Browser(); SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); foreach (SHDocVw.InternetExplorer _ie in shellWindows) { var filename = System.IO.Path.GetFileNameWithoutExtension(_ie.FullName).ToLower(); if (filename.Equals("iexplore")) { // Log.Debug(string.Format("Web Site : {0}", _ie.LocationURL)); try { result.wBrowser = _ie as SHDocVw.WebBrowser; // result.IE = _ie; } catch (Exception ex) { Log.Error(ex, ""); } } } if (result.wBrowser == null && !string.IsNullOrEmpty(url)) { object m = null; SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer(); // Open the URL ie.Navigate2(url, ref m, ref m, ref m, ref m); ie.Visible = true; var sw = new Stopwatch(); sw.Start(); while (result.wBrowser == null) { try { var doc = ie.Document as mshtml.HTMLDocument; var timeout = TimeSpan.FromSeconds(5); while (sw.Elapsed < timeout && doc.readyState != "complete" && doc.readyState != "interactive") { // Log.Debug("pending complete, readyState: " + doc.readyState); System.Threading.Thread.Sleep(100); } result.wBrowser = ie as SHDocVw.WebBrowser; } catch (Exception) { } } } if (result.wBrowser == null) { return(null); } result.Document = result.wBrowser.Document as mshtml.HTMLDocument; result.title = result.Document.title; result.findPanel(); return(result); }
//make sure IE is running and has the google page loaded public static HTMLDocument AttachToIEFromWebAddressExample() { // get ie instance from the windows api, and use the address to locate the page var ie = new SHDocVw.ShellWindowsClass().GetDocumentByAddress("google"); // inject a simple alert return(ie); }
public static void IEEvent() { var _doc = new SHDocVw.ShellWindowsClass().GetDocumentByAddress("google"); var docEvents = (_doc as HTMLDocumentEvents2_Event); if (docEvents == null) { Console.WriteLine("Doc events is null"); return; } docEvents.onreadystatechange += DocEventsOnOnreadystatechange; }
/// <summary> /// Check to see if windows explorer is open to the specified path /// </summary> /// <param name="path">path of file or directory</param> /// <returns>Internet Explorer object or null</returns> private SHDocVw.InternetExplorer GetExplorerForPath(string path) { path = Path.GetDirectoryName(path); SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); foreach (SHDocVw.InternetExplorer ie in shellWindows) { string filename = Path.GetFileNameWithoutExtension(ie.FullName); if (filename.Equals("explorer", StringComparison.OrdinalIgnoreCase)) { Uri uri = new Uri(ie.LocationURL); if (uri.LocalPath == path) { return ie; } } } return null; }
public static (bool Ok, string FilePath) GetCurrentFilePath() { var cpr = GetCurrentProcessInfo(); if (cpr.ProcessName != "explorer") { return(false, string.Empty); } var status = GetGUICursorStatus(cpr.ProcessId); if (status.Ok) { if ((int)status.GUIInfo.hwndCaret != 0) { return(false, string.Empty); } } try { var windows = new SHDocVw.ShellWindowsClass(); FolderItems fi = null; foreach (SHDocVw.InternetExplorer window in windows) { var filename = Path.GetFileNameWithoutExtension(window.FullName).ToLowerInvariant(); if (filename == "explorer" && window.HWND == cpr.Hwnd.ToInt32()) { fi = ((IShellFolderViewDual2)window.Document).SelectedItems(); if (fi != null && fi.Count == 1) { return(true, fi.Item(0).Path); } } } SendKeys.SendWait("^c"); DataObject data = new DataObject(OleGetClipboard()); var files = data.GetFileDropList(); if (files.Count == 1) { return(true, files[0].ToString()); } } catch (Exception e) { } return(false, string.Empty); }
public static void AttachToIEUsingMainNativeWindowIntPtr() { // find all process by IE and select the MainWindowHandle var ieProcess = Process.GetProcessesByName("iexplore").Select(x => x.MainWindowHandle); HTMLDocument _doc; // loop through all main window handles ( selecting all processes will select sub processes) foreach (var intPtr in ieProcess) { // try to get the IE Instance _doc = new SHDocVw.ShellWindowsClass().GetDocumentByPtr(intPtr); // if you if (_doc != null) { CallScript(_doc, "AttachToIEUsingMainNativeWindowIntPtr"); break; } } }
private static string GetIExploreUrl(IntPtr hWnd) { string url = null; try { SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); foreach (SHDocVw.InternetExplorer ie in shellWindows) { if (ie.HWND == hWnd.ToInt32()) { url = ie.LocationURL; break; } } } catch (Exception ex) { TraceLogger.Instance.WriteException(ex); } return(url); }
private static Shell32.FolderItem ReadFileInfo() { IntPtr handle = GetForegroundWindow(); List <string> selected = new List <string>(); SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); foreach (SHDocVw.ShellBrowserWindow window in shellWindows) { if (window.HWND == (int)handle) { Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems(); if (items.Count == 1) { return(items.Item(0)); } } } return(null); }
private void findBrowser() { var wbs = new SHDocVw.ShellWindowsClass().Cast <SHDocVw.WebBrowser>().ToList(); foreach (var w in wbs) { try { var doc = (w.Document as mshtml.HTMLDocument); if (doc != null) { wBrowser = w as SHDocVw.WebBrowser; var _Document = (wBrowser.Document as mshtml.HTMLDocument); var _Document2 = (wBrowser.Document as mshtml.IHTMLDocument2); findPanel(); } } catch (Exception) { } } }
public void CloseBySelector(Selector selector, TimeSpan timeout, bool Force) { string url = ""; var f = selector.First(); var p = f.Properties.Where(x => x.Name == "url").FirstOrDefault(); if (p != null) { url = p.Value; } SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); foreach (SHDocVw.InternetExplorer _ie in shellWindows) { var filename = System.IO.Path.GetFileNameWithoutExtension(_ie.FullName).ToLower(); if (filename.Equals("iexplore")) { try { var wBrowser = _ie as SHDocVw.WebBrowser; if (wBrowser.LocationURL == url || string.IsNullOrEmpty(url)) { using (var automation = Interfaces.AutomationUtil.getAutomation()) { var _ele = automation.FromHandle(new IntPtr(_ie.HWND)); using (var app = new FlaUI.Core.Application(_ele.Properties.ProcessId.Value, false)) { app.Close(); } } } } catch (Exception ex) { Log.Error(ex, ""); } } } }
private void List_Click(object sender, EventArgs e) { this.listViewUrls.Items.Clear(); DateTime now = DateTime.Now; IntPtr activeHWND = GetForegroundWindow(); string activeWindowTitle = GetActiveWindowTitle(activeHWND); if (!activeHWND.Equals(lastActiveWindow)) { lastAppTickCount = 0; lastActiveWindow = activeHWND; } lastAppTickCount++; if (lastHiddenTime.AddMinutes(5) < now) { ShowWindow((IntPtr)lastHiddenWindow, 2); lastHiddenTime = DateTime.MaxValue; lastHiddenWindow = IntPtr.Zero; } if (lastAppTickCount > 1200) { ShowWindow((IntPtr)activeHWND, 0); // hide window lastHiddenWindow = activeHWND; lastHiddenTime = now; lastAppTickCount = 0; return; } SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); string filename; foreach (SHDocVw.InternetExplorer ie in shellWindows) { filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower(); if ((IntPtr)ie.HWND == GetForegroundWindow()) { if (filename.Equals("iexplore")) { if (ie.Height > 100) { string url = ie.LocationURL; Uri uri = new Uri(url); if (pf.onTickPerSecond(uri)) { IntPtr hwnd = (IntPtr)ie.HWND; MoveWindow(hwnd, 0, 0, 0, 0, false); } } } } } }
public void testrun() { while (true) { // Obtain the handle of the active window. IntPtr handle = GetForegroundWindow(); //Console.WriteLine(buff.ToString()); //Console.WriteLine(handle.ToString()); // Update the controls. if (GetWindowText(handle, buff, chars) > 0) { //Console.WriteLine(buff.ToString()); //Console.WriteLine(handle.ToString()); if (buff.ToString().Contains("Internet")) { SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); string filename; foreach (SHDocVw.InternetExplorer ie in shellWindows) { filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower(); if (filename.Equals("iexplore") && ie.HWND == handle.ToInt32() && ie.Document != null) { mshtml.IHTMLDocument2 htmlDoc = ie.Document as mshtml.IHTMLDocument2; //SQL.Connect(); try { int site_id = 0; if (SQL.Get(@"select id from sites where ENTREPRISE = '" + webform_tmp.one_site["ENTREPRISE"].ToString().Replace("'", "''") + "'") != null) site_id = Convert.ToInt16(SQL.Get(@"select id from sites where ENTREPRISE = '" + webform_tmp.one_site["ENTREPRISE"].ToString().Replace("'", "''") + "'")); /* DataRow row = webform_tmp.dtAnnIn.NewRow(); row["id"] = site_id; row["Entreprise"] = webform_tmp.one_site["ENTREPRISE"].ToString(); row["source"] = htmlDoc.body.innerHTML; row["source_hash"] = webform_tmp.CreateMD5Hash(htmlDoc.title); webform_tmp.dtAnnIn.Rows.Add(row); */ //webform_tmp.dtAnnIn.Rows.Add(site_id, webform_tmp.one_site["ENTREPRISE"].ToString(), htmlDoc.body.InnerHtml, webform_tmp.CreateMD5Hash(htmlDoc.body.InnerHtml)); if (webform_tmp.extract && site_id != 0) { bool insertdone = false; insertdone = SQL.Requet(@"insert into pages (id_site, source, source_hash) values (" + site_id + ", '" + htmlDoc.body.innerHTML.Replace("'", "''") + "', '" + webform_tmp.CreateMD5Hash(htmlDoc.body.innerText.ToString()) + "')"); if (insertdone) { webform_tmp.allanonnces.Add(htmlDoc.body.innerHTML.ToString()); } else { WebForm.insertfail++; if (WebForm.insertfail > 5 && webform_tmp.one_site["MAXPAGES"].ToString() == "99") webform_tmp.webClose(); } } } catch (Exception exc) { Console.WriteLine("testrun : "+exc.Message); } //SQL.Disconnect(); try { ie.Quit(); } catch { } return; } } } } Thread.Sleep(1000); } }
private void button1_Click(object sender, EventArgs e) { SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); int a = shellWindows.Count; string urls = ""; string htmlContent = ""; string textBox2 = this.textBox2.Text; if (a == 0) { this.groupBox2.Text = "请确认已打开浏览器窗口"; } int num = 0; foreach (SHDocVw.InternetExplorer ie in shellWindows) { //filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower(); string url = ie.LocationURL; if (url.Contains("http")) { num++; urls = urls + url + "\n"; if (num == int.Parse(textBox2)) { mshtml.IHTMLDocument2 htmlDoc = (mshtml.IHTMLDocument2)ie.Document; IHTMLFramesCollection2 frames = htmlDoc.frames; int len = frames.length; int i = 0; IHTMLWindow2 mainFrame = null; IHTMLWindow2 windowFrame = null; for (; i < len; i++) { IHTMLWindow2 frame2 = frames.item(i) as IHTMLWindow2; //if ("mainFrame".Equals(frame2.name)) { // htmlContent= frame2.document.body.innerHTML.ToString(); //} if (frame2.name != null && frame2.name.Contains("window")) { htmlContent = frame2.document.body.innerHTML.ToString(); windowFrame = frame2; //HTMLIFrame fe = (HTMLIFrame)frames.item(i).document; //fe.contentWindow.execScript("doShut()", "javascript"); //触发点击事件 //mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)htmlDoc.parentWindow; //frame2.execScript(";", "javascript");//使用JS } if (frame2.name != null && frame2.name.Contains("mainFrame")) { htmlContent = frame2.document.body.innerHTML.ToString(); mainFrame = frame2; } } mainFrame.execScript("showRHBillDetailForAuth('3785441458','1','15000096057223')"); //windowFrame.execScript("doShut()"); //string aa = htmlDoc != null ? htmlDoc.body.outerHTML.ToString() : "***Failed***"; //htmlContent = aa; //string aa = htmlDoc.body.innerHTML.ToString(); //htmlContent = aa; //触发点击事件 //mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)htmlDoc.parentWindow; //win.execScript("doSubmit()", "javascript");//使用JS } } } this.groupBox2.Text = urls; this.richTextBox1.Text = htmlContent; this.textBox1.Text = num.ToString(); //Form2 form2 = new Form2(); //form2.Show(); ////this.Close(); //this.Hide(); }