如题 假设要在webbrowser1中找字符串变量s,
希望的结果是得到该页上含有s的个数,提亮颜色,位置focus到第一个,点下一个后focus到下一个

解决方案 »

  1.   

    = =好了 我已经找到方法了还是自己找快点
    要用mshtml里的
    IHTMLDocument2和IHTMLTxtRange
      

  2.   

    //项目中添加Micrsoft.mshtml引用
            private void Form1_Load(object sender, EventArgs e)
            {
                webBrowser1.Navigate("http://topic.csdn.net/u/20100411/19/d8c78a3c-2968-4b5b-91f2-abe3123c7802.html");
                while (webBrowser1.IsBusy || string.IsNullOrEmpty("" + webBrowser1.Url)) Application.DoEvents();
                
                IHTMLDocument2 document = (IHTMLDocument2)webBrowser1.Document.DomDocument;
                document.parentWindow.execScript(@"
    function Hilite(options) {
    var self = this;
    options = options || {};
    for (var p in options) this[p] = options[p];
    }Hilite.prototype = {
    exact: false,
    max_nodes: 1000,
    backColors: ['#FFFF00', '#55FF55', '#AAFFAA', '#00FFFF'],
    focusColor: '#FF0000',
    focusIndex: 0,
    query: function(elm, keywords) {
    var self = this;
    this.spans = [];
    if (!keywords || !elm.childNodes.length) return;
    keywords = keywords.split(/[\s,\+\.]+/);
    var qre = [];
    for (var i = 0; i < keywords.length; i++) {
    keywords[i] = keywords[i].toLowerCase();
    if (this.exact)
    qre.push('\\b' + keywords[i] + '\\b');
    else qre.push(keywords[i]);
    }
    qre = new RegExp(qre.join('|'), 'i');
    var colormapper = {};
    for(var i = 0; i < keywords.length; i++) {
    colormapper[keywords[i]] = this.backColors[i % this.backColors.length];
    }
    var textproc = function(node) {
    var match = qre.exec(node.data);
    if(match) {
    var val = match[0];
    var k = '';
    var node2 = node.splitText(match.index);
    var node3 = node2.splitText(val.length);
    var span = node.ownerDocument.createElement('SPAN');
    self.spans.push(span);
    node.parentNode.replaceChild(span,node2);
    span.style.backgroundColor = colormapper[val.toLowerCase()];
    span.appendChild(node2);
    return span;
    } else {
    return node;
    }
    };
    this.walk(elm.childNodes[0], 1, textproc);
    },

    walk: function(node, depth, textproc) {
    var skipre = /^(script|style|textarea)/i;
    var count = 0;
    while (node && depth > 0) {
    count++;
    if (count >= this.max_nodes) {
    var handler = function() {
    this.walkElements(node,depth,textproc);
    };
    setTimeout(handler,50);
    return;
    }
    if(node.nodeType == 1) {
    if (!skipre.test(node.tagName) && node.childNodes.length > 0) {
    node=node.childNodes[0];
    depth++;
    continue;
    }
    } else if (node.nodeType == 3) {
    node = textproc(node);
    }
    if (node.nextSibling) {
    node = node.nextSibling;
    } else {
    while (depth > 0) {
    node = node.parentNode;
    depth--;
    if (node.nextSibling) {
    node = node.nextSibling;
    break;
    }
    }
    }
    }
    },
    nextFocus: function() {
    if (!this.spans || !this.spans.length) return;
    if (this.lastFocus)  this.lastFocus.style.backgroundColor = this.lastFocus.oldBackgroundColor;
    this.lastFocus = this.spans[this.focusIndex];
    this.lastFocus.oldBackgroundColor = this.lastFocus.style.backgroundColor;
    this.lastFocus.style.backgroundColor = this.focusColor;
    this.lastFocus.scrollIntoView(true);
    this.focusIndex = (this.focusIndex + 1) % this.spans.length;
    }
    };var hilite = new Hilite();
    hilite.query(document, '的 webbrowser1');
    hilite.nextFocus();
    ", "javascript");        }        private void button1_Click(object sender, EventArgs e)
            {
                if (webBrowser1.IsBusy || string.IsNullOrEmpty("" + webBrowser1.Url)) return;
                webBrowser1.Navigate(@"javascript:hilite.nextFocus();");
            }