/// <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); }
/// <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); } } }
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 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); }