Pergunta

Eu tenho um controle WebBrowser e sou capaz de obter a palavra selecionada pelo usuário. Estou salvando essa palavra em um arquivo e, com ele, também estou salvando seu deslocamento e comprimento de byte.

Lets diz que tenho algum texto no meu controle de navegador da web como "Olá ei, olá", digamos que o usuário selecionou o último olá.

Agora essa palavra é salva comigo junto com outras informações, como seu comprimento etc.

Preciso fornecer um recurso para destacar a palavra selecionada quando o usuário recarrega o arquivo e me envia essa palavra junto com seu comprimento e deslocamento de byte

Há alguma forma de fazer isso.

Foi útil?

Solução

Você precisará importar a referência de montagem do Microsoft.MSHTML, se ainda não o fez, e adicione

using mshtml;

        if (webBrowser1.Document != null)
        {
            IHTMLDocument2 document = webBrowser1.Document.DomDocument as IHTMLDocument2;
            if (document != null)
            {
                IHTMLBodyElement bodyElement = document.body as IHTMLBodyElement;
                if (bodyElement != null)
                {
                    IHTMLTxtRange trg = bodyElement.createTextRange();


                    if (trg != null)
                    {
                        const String SearchString = "Privacy"; // This is the search string you're looking for.
                        const int wordStartOffset = 421; // This is the starting position in the HTML where the word you're looking for starts at.
                        int wordEndOffset = SearchString.Length;
                        trg.move("character", wordStartOffset);
                        trg.moveEnd("character", wordEndOffset);

                        trg.select();
                    }
                }
            }
        }

Aqui está um trecho que pode ser útil também:

        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)
                {
                   const String search = "Privacy";

                   if (range.findText(search, search.Length, 2))
                   {
                       range.select();
                   }
                }
            }
        }

Outras dicas

Eu sou um programador iniciante, são minhas melhores amostras. Apenas gaste muito tempo.

Basta conectar sua biblioteca

Projeto - Adicionar um link - Visão geral - 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 + "</span>"))
                        {
                            range.select();
                            range.pasteHTML("<span style='background-color: rgb(255, 255, 0);'>" + textBox1.Text.ToLower() + "</span>");
                            goto line1;

                        }
                    }

                }
            }
        }
      }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top