static public IHTMLElement GetFrame(mshtml.IHTMLDocument2 doc) { IHTMLElement frame = null; mshtml.IHTMLWindow2 window2 = null; try { window2 = doc.parentWindow as mshtml.IHTMLWindow2; if (window2 != null) { return(((mshtml.HTMLWindow2)window2).frameElement as IHTMLElement); } } catch (UnauthorizedAccessException) { //主页面和IFRAME页面处于不同域名的时候会报UnauthorizedAccessException,下面通过IHTMLWindow2->IWebBrowser2 //比较IWebBrowser2和iframe元素url定位iframe SHDocVw.IWebBrowser2 browser = CrossFrameIE.GetIWebBrowser2(window2); mshtml.IHTMLDocument3 parentDoc = browser.Parent as mshtml.IHTMLDocument3; mshtml.IHTMLElementCollection framesCollection = parentDoc.getElementsByTagName("iframe") as mshtml.IHTMLElementCollection; foreach (mshtml.IHTMLElement f in framesCollection) { SHDocVw.IWebBrowser2 wb2 = (SHDocVw.IWebBrowser2)((mshtml.HTMLFrameElement)f); if (wb2.LocationURL == browser.LocationURL) { frame = f as IHTMLElement; break; } } } catch (Exception) { return(null); } return(frame); }
public static bool IsDTDDocument(object document) { // XHtml declare flag string string DocTypeContent = @"-//W3C//DTD"; mshtml.IHTMLDocument3 document3 = (mshtml.IHTMLDocument3)document; mshtml.IHTMLDOMChildrenCollection domChilds = (mshtml.IHTMLDOMChildrenCollection)document3.childNodes; mshtml.IHTMLDOMNode domNode = (mshtml.IHTMLDOMNode)domChilds.item(0); return(domNode.nodeValue.ToString().Contains(DocTypeContent)); }
/*自定义挂载函数*****************************************************************************************************************************/ /// <summary> /// 时间:2016年9月14日13:07:25 /// 自定义挂载函数 /// 1.加载文件内容 /// 找到匹配的网址URL /// 再将些参数传给函数-OnDocumentComplete_query_iframe_like_url(like_url)处理 /// </summary> /// <param name="pDisp"></param> /// <param name="URL"></param> public void OnDocumentComplete_start(object display, ref object URL) { this.Debug_config = util.getFilename("debug_config"); /*初始化:读取Debug_Msg 配置是否==‘开’*/ /**日志**/ this.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; //程序执行位置代码块的方法名 this.msg = methodName + "进入此方法!"; /***/ util.log_to(this.Debug_config, this.methodName, this.msg); /**日志**/ mshtml.HTMLDocument doc = (mshtml.HTMLDocument)webBrowser.Document; mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)webBrowser.Document; mshtml.IHTMLDocument3 doc3 = (mshtml.IHTMLDocument3)webBrowser.Document; mshtml.IHTMLDocument4 doc4 = (mshtml.IHTMLDocument4)webBrowser.Document; mshtml.IHTMLDocument5 doc5 = (mshtml.IHTMLDocument5)webBrowser.Document; string strUrl = URL.ToString(); //this.strFilterKeys = this.get_file_str(); this.msg = "当前网址=" + strUrl; util.log_to(this.Debug_config, this.methodName, this.msg); string Directory_path = System.Reflection.Assembly.GetExecutingAssembly().Location; /*********************(读取分隔符)*********************/ // @"\url_fgf.txt";//读取分隔符 string url_fgf_str = util.getFilename("url_fgf"); //System.Windows.Forms.MessageBox.Show("分隔符-url_fgf_str.ToCharArray()=" + url_fgf_str.ToCharArray()); /*********************(读取URL)*********************/ //@"\url.txt";//读取URL string urls = util.getFilename("url"); this.msg = "读取配置文件中》URL=" + urls; util.log_to(this.Debug_config, this.methodName, this.msg); /*********************(读取URL)*********************/ string[] url_list = urls.Split(url_fgf_str.ToCharArray()); foreach (string url in url_list) { if (strUrl.Contains(url))//判断-是否包含-URL网址 { int frames = doc.parentWindow.frames.length; int doc3_iframe_lenght = doc3.getElementsByTagName("iframe").length; //iframe 数量 //int doc2_iframe_lenght=doc2.getElementsByTagName("iframe").length; //iframe 数量 e不能正常出数 this.msg = methodName + " \nLv5>匹配到网址-url:>" + url + "关键字的地址!\n frames=" + frames + "\n doc3_iframe_lenght=" + doc3_iframe_lenght; util.log_to(this.Debug_config, this.methodName, this.msg); //将当前网址URL-匹配到-文件中URL传给相关函数处理 this.OnDocumentComplete_query_iframe_like_url(url); } else { this.msg = "没有找到匹配项=" + url; util.log_to(this.Debug_config, this.methodName, this.msg); //this.webBrowser.StatusText = strUrl; } } }
/*插入远程JS填屏插件*****************************************************************************************************************************/ /// <summary> /// 时间:2016年9月14日13:54:38 /// 2:在当前网页所有的IFrame中查找(iframe.src like url ) /// a.先找到IFrame.doc /// b.在DOC中插入远程JS插件 /// </summary> /// <param name="like_url"></param> public void OnDocumentComplete_query_iframe_like_url(string like_url) { /**日志**/ this.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;//程序执行位置代码块的方法名 this.msg = methodName + "进入此方法!"; /***/ util.log_to(this.Debug_config, this.methodName, this.msg); /**日志**/ mshtml.HTMLDocument document = (mshtml.HTMLDocument)webBrowser.Document; mshtml.HTMLDocument doc = (mshtml.HTMLDocument)webBrowser.Document; mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)webBrowser.Document; mshtml.IHTMLDocument3 doc3 = (mshtml.IHTMLDocument3)webBrowser.Document; mshtml.IHTMLDocument4 doc4 = (mshtml.IHTMLDocument4)webBrowser.Document; mshtml.IHTMLDocument5 doc5 = (mshtml.IHTMLDocument5)webBrowser.Document; mshtml.IHTMLElement img = (mshtml.IHTMLElement)doc2.all.item("button", 0); int frames = doc.parentWindow.frames.length; int doc3_iframe_lenght = doc3.getElementsByTagName("iframe").length; //iframe 数量 //int doc2_iframe_lenght=doc2.getElementsByTagName("iframe").length; //iframe 数量 e不能正常出数 int inputcount = doc2.all.length; this.msg = "\n frames:" + frames + " \n doc3_iframe_lenght:" + doc3_iframe_lenght; util.log_to(this.Debug_config, this.methodName, this.msg); /******* 第一层--------查找当前首层网页里》所有(IFrame)*******/ if (doc3 != null) { foreach (IHTMLElement iframe_obj in doc3.getElementsByTagName("iframe")) { //判断-当前IFrame是否匹配 >iframe.src( Contains == Like) string iframe_obj_src = iframe_obj.getAttribute("src").ToString(); this.msg = "IFrame.name=" + iframe_obj.getAttribute("name").ToString(); util.log_to(this.Debug_config, this.methodName, this.msg); if (iframe_obj_src.Contains(like_url)) { //取出匹配到的(网页文档-Document2) mshtml.IHTMLFrameBase2 FrameBase2 = (mshtml.IHTMLFrameBase2)iframe_obj; mshtml.IHTMLDocument2 Document2 = FrameBase2.contentWindow.document; string iframe_name = iframe_obj.getAttribute("name").ToString(); this.msg = this.methodName + " \nLv5>找到匹配的URL-IFrame.name=>" + iframe_name + "\nLv5>*****找到匹配的URL-IFrame.src=>" + iframe_obj_src + " 包含匹配字符〉" + like_url; util.log_to(this.Debug_config, this.methodName, this.msg); //@"\url_remote_js.txt"--获取配置文件中JS的URL string url_remote_js = util.getFilename("url_remote_js"); util.add_doc_element_test(Document2, url_remote_js); //string iframe_obj_name = iframe_obj.getAttribute("name").ToString(); //iframe_name_arrylist.Add(iframe_obj_name);//将找到的Iframe.name 属性保存起来 /***************************/ } else { this.msg = "\nLv1>*URL-IFrame.src=> " + iframe_obj_src + " 不包含匹配字符〉" + like_url; util.log_to(this.Debug_config, this.methodName, this.msg); } } } else { this.msg = "doc3=null"; util.log_to(this.Debug_config, this.methodName, this.msg); } }
public static void TakeScreenShot() { // Thread.Sleep(20000); WebBrowser m_browser = null; ShellWindows shellWindows = new ShellWindowsClass(); //Find first availble browser window. //Application can easily be modified to loop through and capture all open windows. string filename; foreach (SHDocVw.WebBrowser ie in shellWindows) { filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower(); if (filename.Equals("iexplore")) { m_browser = ie; break; } } //Assign Browser Document mshtml.IHTMLDocument2 myDoc = (mshtml.IHTMLDocument2)m_browser.Document; mshtml.IHTMLDocument3 doc3 = (mshtml.IHTMLDocument3)myDoc; int heightsize = 0; int widthsize = 0; int screenHeight = 0; int screenWidth = 0; //if (!IsDTDDocument(myDoc)) //{ // myDoc.body.setAttribute("scroll", "Yes", 0); // //Get Browser Window Height // heightsize = (int)myDoc.body.getAttribute("scrollHeight", 0); // widthsize = (int)myDoc.body.getAttribute("scrollWidth", 0); // //Get Screen Height // screenHeight = (int)myDoc.body.getAttribute("clientHeight", 0); // screenWidth = (int)myDoc.body.getAttribute("clientWidth", 0); //} //else { doc3.documentElement.setAttribute("scroll", "Yes", 0); //Get Browser Window Height heightsize = (int)doc3.documentElement.getAttribute("scrollHeight", 0); widthsize = (int)doc3.documentElement.getAttribute("scrollWidth", 0); //Get Screen Height screenHeight = (int)doc3.documentElement.getAttribute("clientHeight", 0); screenWidth = (int)doc3.documentElement.getAttribute("clientWidth", 0); } //Get bitmap to hold screen fragment. Bitmap bm = new Bitmap(screenWidth, screenHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555); //Create a target bitmap to draw into. Bitmap bm2 = new Bitmap(widthsize, heightsize, System.Drawing.Imaging.PixelFormat.Format16bppRgb555); Graphics g2 = Graphics.FromImage(bm2); Graphics g = null; IntPtr hdc; Image screenfrag = null; int brwTop = 0; int brwLeft = 0; int myPage = 0; IntPtr myIntptr = (IntPtr)m_browser.HWND; //Get inner browser window. int hwndInt = myIntptr.ToInt32(); IntPtr hwnd = myIntptr; hwnd = GetWindow(hwnd, GW_CHILD); StringBuilder sbc = new StringBuilder(256); //Get Browser "Document" Handle while (hwndInt != 0) { hwndInt = hwnd.ToInt32(); GetClassName(hwndInt, sbc, 256); if (sbc.ToString().IndexOf("Shell DocObject View", 0) > -1) // pre-IE7 { hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Internet Explorer_Server", IntPtr.Zero); break; } if (sbc.ToString().IndexOf("TabWindowClass", 0) > -1) // IE7 { hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Shell DocObject View", IntPtr.Zero); hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Internet Explorer_Server", IntPtr.Zero); break; } if (sbc.ToString().IndexOf("Frame Tab", 0) > -1) // IE8 { hwnd = FindWindowEx(hwnd, IntPtr.Zero, "TabWindowClass", IntPtr.Zero); hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Shell DocObject View", IntPtr.Zero); hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Internet Explorer_Server", IntPtr.Zero); break; } hwnd = GetWindow(hwnd, GW_HWNDNEXT); } //Get Screen Height (for bottom up screen drawing) while ((myPage * screenHeight) < heightsize) { //if (!IsDTDDocument(myDoc)) // myDoc.body.setAttribute("scrollTop", (screenHeight - 5) * myPage, 0); //else doc3.documentElement.setAttribute("scrollTop", (screenHeight - 5) * myPage, 0); ++myPage; } //Rollback the page count by one --myPage; int myPageWidth = 0; while ((myPageWidth * screenWidth) < widthsize) { //if (!IsDTDDocument(myDoc)) // myDoc.body.setAttribute("scrollLeft", (screenWidth - 5) * myPageWidth, 0); //else doc3.documentElement.setAttribute("scrollLeft", (screenWidth - 5) * myPageWidth, 0); //if (!IsDTDDocument(myDoc)) // brwLeft = (int)myDoc.body.getAttribute("scrollLeft", 0); //else brwLeft = (int)doc3.documentElement.getAttribute("scrollLeft", 0); for (int i = myPage; i >= 0; --i) { //Shoot visible window g = Graphics.FromImage(bm); hdc = g.GetHdc(); //if (!IsDTDDocument(myDoc)) // myDoc.body.setAttribute("scrollTop", (screenHeight - 5) * i, 0); //else doc3.documentElement.setAttribute("scrollTop", (screenHeight - 5) * i, 0); //if (!IsDTDDocument(myDoc)) // brwTop = (int)myDoc.body.getAttribute("scrollTop", 0); //else brwTop = (int)doc3.documentElement.getAttribute("scrollTop", 0); PrintWindow(hwnd, hdc, 0); g.ReleaseHdc(hdc); g.Flush(); screenfrag = Image.FromHbitmap(bm.GetHbitmap()); g2.DrawImage(screenfrag, brwLeft, brwTop); } ++myPageWidth; } //Reduce Resolution Size double myResolution = Convert.ToDouble(100) * 0.01; int finalWidth = (int)((widthsize) * myResolution); int finalHeight = (int)((heightsize) * myResolution); Bitmap finalImage = new Bitmap(finalWidth, finalHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555); Graphics gFinal = Graphics.FromImage((Image)finalImage); gFinal.DrawImage(bm2, 0, 0, finalWidth, finalHeight); //Get Time Stamp DateTime myTime = DateTime.Now; String format = "MM.dd.hh.mm.ss"; //Create Directory to save image to. Directory.CreateDirectory("D:\\IECapture"); //Write Image. EncoderParameters eps = new EncoderParameters(1); eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Convert.ToInt64(100)); ImageCodecInfo ici = GetEncoderInfo("image/jpeg"); finalImage.Save(@"D:\\IECapture\Captured_" + myTime.ToString(format) + ".jpg", ici, eps); //Clean Up. myDoc = null; g.Dispose(); g2.Dispose(); gFinal.Dispose(); bm.Dispose(); bm2.Dispose(); finalImage.Dispose(); }