How to Highlight a specific word in WebBrowser control C#

后端 未结 2 1979
猫巷女王i
猫巷女王i 2021-02-08 17:35

I have a webbrowser control and I am able to get the selected word by the user. I am saving this word in a file and with it I am also saving its byte offset and length.

2条回答
  •  长情又很酷
    2021-02-08 18:09

    I am a novice programmer is my best samples. Just spend a lot of time.

    Just connect your library

    Project - add a link - Overview - windows - system32 - mshtml.tlb

    using mshtml;
    
      private void button1_Click(object sender, EventArgs e)
        {
    
        webBrowser1.Refresh();
    
            Application.DoEvents();
    
            if (webBrowser1.Document != null)
            {
    
                IHTMLDocument2 document = webBrowser1.Document.DomDocument as IHTMLDocument2;
                if (document != null)
                {
                    IHTMLSelectionObject currentSelection = document.selection;
    
                    IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
    
                    if (range != null)
                    {
                        String search = textBox1.Text;
                        if (search == "")
                        {
                            MessageBox.Show("not selected");                          
                        }
                        else
                        {
                        line1:
                            if ((range.findText(search)) && (range.htmlText != "span style='background-color: rgb(255, 255, 0);'>" + textBox1.Text + ""))
                            {
                                range.select();
                                range.pasteHTML("" + textBox1.Text.ToLower() + "");
                                goto line1;
    
                            }
                        }
    
                    }
                }
            }
          }
    

提交回复
热议问题