/// <summary> /// Helper function to build up the list of text nodes containing the desired text /// </summary> private void verifyLabelElements(IHTMLDocument2 document) { if (labelElements == null) { labelElements = new ArrayList(); IHTMLBodyElement bodyElement = document.body as IHTMLBodyElement; if (bodyElement == null) { return; } IHTMLTxtRange textRange = bodyElement.createTextRange(); if (textRange == null) { return; } // Use the findText feature to search for text in the body // Add all matching ranges to the collection // See http://msdn2.microsoft.com/en-us/library/aa741525.aspx for details on the flags // Note that this is not multi-lingual while (textRange.findText(labelText, 0, 0)) { labelElements.Add(textRange.duplicate()); // Move the pointer to just past the current range and search the balance of the doc textRange.moveStart("Character", textRange.htmlText.Length); // Not sure why, but MS find dialog uses this to get the range to the end textRange.moveEnd("Textedit", 1); } } }
/// <summary> /// Search the document from the current selection, and reset the /// the selection to the text found, if successful. /// </summary> /// <param name="text">the text for which to search</param> /// <param name="forward">true for forward search, false for backward</param> /// <param name="matchWholeWord">true to match whole word, false otherwise</param> /// <param name="matchCase">true to match case, false otherwise</param> /// <returns></returns> public bool Search(string text, bool forward, bool matchWholeWord, bool matchCase) { bool success = false; if (webBrowser1.Document != null) { IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2; IHTMLBodyElement body = doc.body as IHTMLBodyElement; if (body != null) { IHTMLTxtRange range; if (doc.selection != null) { range = doc.selection.createRange() as IHTMLTxtRange; IHTMLTxtRange dup = range.duplicate(); dup.collapse(true); // if selection is degenerate, then search whole body if (range.isEqual(dup)) { range = body.createTextRange(); } else { if (forward) { range.moveStart("character", 1); } else { range.moveEnd("character", -1); } } } else { range = body.createTextRange(); } int flags = 0; if (matchWholeWord) { flags += 2; } if (matchCase) { flags += 4; } success = range.findText(text, forward ? 999999 : -999999, flags); if (success) { range.select(); range.scrollIntoView(!forward); } } } return(success); }
public void FindText(String sText) { #if !__MonoCS__ IHTMLDocument2 doc = (IHTMLDocument2)axWebBrowser1.Document; IHTMLBodyElement bod = (IHTMLBodyElement)doc.body; IHTMLTxtRange txt = bod.createTextRange(); txt.findText(sText, 0x7fffffff, 0); #endif }
private void DisableScrollbars(IHTMLDocument2 document2) { try { setAttribute("scroll", "no"); IHTMLBodyElement body = (IHTMLBodyElement)document2.body; body.scroll = "no"; document2.body.style.borderStyle = "none"; } catch (Exception ex) { LOG.Warn("Can't disable scroll", ex); } }
private void _browserControl_DocumentComplete(object sender, BrowserDocumentEventArgs e) { Timer timer = null; try { // unsubscribe from the event _browserControl.DocumentComplete -= new BrowserDocumentEventHandler(_browserControl_DocumentComplete); // get the document IHTMLDocument2 document = _browserControl.Document as IHTMLDocument2; // eliminate borders, scroll bars, and margins IHTMLElement element = document.body; element.style.borderStyle = "none"; IHTMLBodyElement body = element as IHTMLBodyElement; body.scroll = "no"; body.leftMargin = 0; body.rightMargin = 0; body.topMargin = 0; body.bottomMargin = 0; // set the width and height of the browser control to the correct // values for the snapshot // width specified by the caller _browserControl.Width = _htmlScreenCaptureCore.ContentWidth; if (_htmlScreenCaptureCore.MaximumHeight > 0) { _browserControl.Height = _htmlScreenCaptureCore.MaximumHeight; } else { // height of the content calculated based on this width IHTMLElement2 element2 = element as IHTMLElement2; _browserControl.Height = element2.scrollHeight; } // release UI thread to load the video thumbnail on screen // (the Tick may need to fire more than once to allow enough // time and message processing for an embedded object to // be fully initialized) timer = new Timer(); timer.Interval = WAIT_INTERVAL; timer.Tick += new EventHandler(timer_Tick); timer.Start(); } catch (Exception ex) { _htmlScreenCaptureCore.SetException(ex); CleanupAndExit(timer); } }
private void Browser_LoadCompleted(NavigationEventArgs e) { IHTMLDocument2 doc = (IHTMLDocument2)Browser.Document; IHTMLBodyElement body = (IHTMLBodyElement)doc.body; doc.charset = "utf-8"; ((IHTMLElement3)body).contentEditable = "true"; _body = (HTMLBody)doc.body; dynamic d = doc; d.attachEvent("onkeyup", _listener); d.attachEvent("onblur", _listener); d.attachEvent("onchange", _listener); d.attachEvent("oninput", _listener); d.attachEvent("onpaste", _listener); d.attachEvent("oncut", _listener); }
private void searchWord(string keyword, int findIndex, bool fromBeging = false) { IHTMLTxtRange searchRange = null; IHTMLDocument2 document = (IHTMLDocument2)wb.Document.DomDocument; if (keyword == "") { return; } if (fromBeging) { IHTMLBodyElement body = (IHTMLBodyElement)document.body; searchRange = (IHTMLTxtRange)body.createTextRange(); } else if (document.selection.type.ToLower() != "none") { searchRange = (IHTMLTxtRange)document.selection.createRange(); if (findIndex > 0) { searchRange.moveStart("character", 1); searchRange.collapse(true); } else { searchRange.moveStart("character", -1); searchRange.moveEnd("character", -100000); searchRange.collapse(true); } } else { IHTMLBodyElement body = (IHTMLBodyElement)document.body; searchRange = (IHTMLTxtRange)body.createTextRange(); } Trace.WriteLine(searchRange.text); if (searchRange.findText(keyword, findIndex, 0))// 如果找到了,就选取(高亮显示)该关键字;否则弹出消息。 { searchRange.select(); } else { MessageBox.Show("已搜索到文档结尾。"); } }
private bool FindString(HtmlElement elem, string str) { bool strFound = false; IHTMLTxtRange rng = null; try { if (rng != null) { rng.collapse(false); strFound = rng.findText(str, 1000000000, 0); if (strFound) { rng.select(); rng.scrollIntoView(true); } } if (rng == null) { IHTMLDocument2 doc = elem.Document.DomDocument as IHTMLDocument2; IHTMLBodyElement body = doc.body as IHTMLBodyElement; rng = body.createTextRange(); rng.moveToElementText(elem.DomElement as IHTMLElement); strFound = rng.findText(str, 1000000000, 0); if (strFound) { rng.select(); rng.scrollIntoView(true); } } } catch { } return(strFound); }
private int HighlightKeyword() { int matches = -1; if (!string.IsNullOrEmpty(messageProvider.Keyword)) { const string strbg = "BackColor"; const string strf = "ForeColor"; const string sword = "Character"; const string stextedit = "Textedit"; //You may be tempted to use Color.Yellow.ToArgb(). But, //the value returned includes the A value which //causes confusion for MSHTML. i.e. Color.Cyan is interpreted as yellow int background = MakeRGB(Color.FromName("Yellow")); //int foreground = MakeRGB(Color.FromName("Yellow")); IHTMLDocument2 document = webBrowser.Document.DomDocument as IHTMLDocument2; if (document != null) { IHTMLElement pElem = document.body; IHTMLBodyElement pBodyelem = pElem as IHTMLBodyElement; if (pBodyelem == null) { return(matches); } IHTMLTxtRange range = pBodyelem.createTextRange(); if (range == null) { return(matches); } //IHTMLSelectionObject currentSelection = document.selection; //IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange; IHTMLTxtRange firstRange = null; while (range.findText(messageProvider.Keyword, messageProvider.Keyword.Length, 0)) { if (matches == -1) { matches = 0; firstRange = range.duplicate(); } if (background != 0) { range.execCommand(strbg, false, background); } //if (foreground != 0) //range.execCommand(strf, false, foreground); range.moveStart(sword, 1); range.moveEnd(stextedit, 1); matches += 1; } if (firstRange != null) { firstRange.scrollIntoView(true); } } } return(matches); }