Pregunta

Tengo un control de navegador web y puedo obtener la palabra seleccionada por el usuario. Estoy guardando esta palabra en un archivo y con ella también estoy guardando su byte offset y longitud.

Vamos a decir que tengo texto en el control de mi navegador web como "Hola Hola Hola" supongamos que el usuario ha seleccionado el último saludo.

Ahora esa palabra se guarda conmigo junto con otra información como su longitud, etc.

Necesito proporcionar una función para resaltar la palabra seleccionada cuando el usuario vuelve a cargar el archivo y me envía esa palabra junto con su longitud y desplazamiento de bytes

¿Hay alguna forma de hacerlo?

¿Fue útil?

Solución

necesitará importar la referencia de ensamblaje Microsoft.mshtml si aún no lo ha hecho, y agregar

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

aquí hay un fragmento que podría ser útil también:

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

Otros consejos

Soy un programador novato es mi mejor muestra. Solo pasa mucho tiempo.

Solo conecta tu biblioteca

Proyecto - agregar un enlace - Descripción general - 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 bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top