Domanda

Ho un controllo del browser web e sono in grado di ottenere la parola selezionata dall'utente. Sto salvando questa parola in un file e con essa sto anche salvando il suo offset e lunghezza byte.

Diciamo che ho un po 'di testo nel mio controllo del browser Web come " Hello Hey Hello " supponiamo che l'utente abbia selezionato l'ultimo saluto.

Ora quella parola viene salvata con me insieme ad altre informazioni come la sua lunghezza ecc.

Devo fornire una funzione per evidenziare la parola selezionata quando l'utente ricarica il file e mi invia quella parola insieme alla sua lunghezza e offset dei byte

Esiste un modo per farlo.

È stato utile?

Soluzione

dovrai importare il riferimento dell'assembly Microsoft.mshtml se non l'hai già fatto e aggiungere

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

ecco uno snippet che potrebbe essere utile anche:

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

Altri suggerimenti

Sono un programmatore alle prime armi è il mio campione migliore. Trascorri molto tempo.

Basta collegare la tua libreria

Progetto - aggiungi un collegamento - Panoramica - 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;

                        }
                    }

                }
            }
        }
      }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top