كيفية تسليط الضوء على كلمة محددة في WebBrowser Control C#

StackOverflow https://stackoverflow.com/questions/1412648

  •  06-07-2019
  •  | 
  •  

سؤال

لديّ عنصر تحكم WebBrowser وأتمكن من الحصول على الكلمة المحددة من قبل المستخدم. أقوم بحفظ هذه الكلمة في ملف ومعه أقوم أيضًا بحفظ إزاحة البايت والطول.

دعنا نقول أن لدي بعض النص في التحكم في متصفح الويب الخاص بي على أنه "Hello Hey Hello" ، دعنا نقول أن المستخدم قد حدد Hello Last.

الآن يتم حفظ هذه الكلمة معي مع معلومات أخرى مثل طولها وما إلى ذلك.

أحتاج إلى توفير ميزة لتسليط الضوء على الكلمة المحددة عند إعادة تحميل المستخدم الملف ويرسل لي هذه الكلمة مع طولها وموفرة البايت

هل هناك أي طريقة للقيام بذلك.

هل كانت مفيدة؟

المحلول

ستحتاج إلى استيراد مرجع تجميع microsoft.mshtml إذا لم تكن قد لم تكن كذلك ، وأضف

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

فيما يلي مقتطف قد يكون مفيدًا أيضًا:

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

نصائح أخرى

أنا مبرمج مبتدئ هو أفضل عيناتي. فقط اقض الكثير من الوقت.

فقط قم بتوصيل مكتبتك

المشروع - إضافة رابط - نظرة عامة - 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;

                        }
                    }

                }
            }
        }
      }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top