尝试突出显示 WebBrowser 控件中的单词

发布于 2024-10-31 07:19:02 字数 205 浏览 1 评论 0原文

我试图让我的 WebBrowser 控件突出显示一些单词。 我发现这个 示例 但我不知道如何将其转换为 c#。 任何帮助将不胜感激:)

i am trying to make my WebBrowser control highlight some words.
i found this example but i don't get how to translate that to c#.
any help would be appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迟月 2024-11-07 07:19:02

代码已经是 C# 语言了。您需要添加引用来获取接口类型定义。它们是 COM 类型,从 Internet Explorer 类型库导入。

项目 + 添加引用,浏览选项卡,选择 c:\windows\system32\mshtml.tlb。将 using mshtml; 放在源代码文件的顶部。该代码对于 COM 类型相当草率,如果您使用 VS2010,则必须在“引用”节点中选择添加的引用 (MSHTML),并将“嵌入互操作类型”属性设置为 False,将“复制本地”属性设置为 True。部署 Microsoft.mshtml.dll 互操作库,您将找到构建目录以及您的程序。

The code is already in C#. You need to add a reference to get the interface type definitions. They are COM types, imported from an Internet Explorer type library.

Project + Add Reference, Browse tab, select c:\windows\system32\mshtml.tlb. Put using mshtml; at the top of your source code file. The code is pretty sloppy with the COM types, if you use VS2010 then you'll have to select the added reference (MSHTML) in the References node and set the Embed Interop Types property to False and the Copy Local property to True. Deploy the Microsoft.mshtml.dll interop library you'll find the build directory along with your program.

自此以后,行同陌路 2024-11-07 07:19:02

这是如何进行的。

Private Sub WebBrowser_DocumentCompleted(sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser.DocumentCompleted
        Dim doc2 As mshtml.IHTMLDocument2 = WebBrowser.Document.DomDocument
        Dim ReplacementTag As String = "<span style='background-color: rgb(255, 255, 0);'>"
        Dim strBuilder As StringBuilder = New StringBuilder(doc2.body.outerHTML)
        Dim HTMLString As String = strBuilder.ToString()
        If Me.m_NoteType = ExtractionNoteType.SearchResult Then
            Dim SearchWords As New List(Of String)
            SearchWords.AddRange(Me.txtNoteSearch.Text.Trim.Split(" "))
            For Each item As String In SearchWords
                Dim index As Integer = HTMLString.IndexOf(item, 0, StringComparison.InvariantCultureIgnoreCase)
                ''If index > 0 Then
                While (index > 0 AndAlso index < HTMLString.Length)
                    HTMLString = HTMLString.Insert(index, ReplacementTag)
                    HTMLString = HTMLString.Insert(index + item.Length + ReplacementTag.Length, "</span>")
                    index = HTMLString.IndexOf(item, index + item.Length + ReplacementTag.Length + 7, StringComparison.InvariantCultureIgnoreCase)
                End While
                ''End If
                ''strBuilder.Replace(item, "<span style='background-color: rgb(255, 255, 0);'>" + item + "</span>")
            Next
        Else
            ''strBuilder.Replace("<span style='background-color: rgb(255, 255, 0);'>", "<span style='background-color: rgb(255, 255, 255);'>")
        End If
        doc2.body.innerHTML = HTMLString
    End Sub

The example mentioned will actually replace the content. So it might get wrong output when finding text matches with the text with Capital letters. So might be useful to use indexOf and Insert rather than Replace.

Here is How.

Private Sub WebBrowser_DocumentCompleted(sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser.DocumentCompleted
        Dim doc2 As mshtml.IHTMLDocument2 = WebBrowser.Document.DomDocument
        Dim ReplacementTag As String = "<span style='background-color: rgb(255, 255, 0);'>"
        Dim strBuilder As StringBuilder = New StringBuilder(doc2.body.outerHTML)
        Dim HTMLString As String = strBuilder.ToString()
        If Me.m_NoteType = ExtractionNoteType.SearchResult Then
            Dim SearchWords As New List(Of String)
            SearchWords.AddRange(Me.txtNoteSearch.Text.Trim.Split(" "))
            For Each item As String In SearchWords
                Dim index As Integer = HTMLString.IndexOf(item, 0, StringComparison.InvariantCultureIgnoreCase)
                ''If index > 0 Then
                While (index > 0 AndAlso index < HTMLString.Length)
                    HTMLString = HTMLString.Insert(index, ReplacementTag)
                    HTMLString = HTMLString.Insert(index + item.Length + ReplacementTag.Length, "</span>")
                    index = HTMLString.IndexOf(item, index + item.Length + ReplacementTag.Length + 7, StringComparison.InvariantCultureIgnoreCase)
                End While
                ''End If
                ''strBuilder.Replace(item, "<span style='background-color: rgb(255, 255, 0);'>" + item + "</span>")
            Next
        Else
            ''strBuilder.Replace("<span style='background-color: rgb(255, 255, 0);'>", "<span style='background-color: rgb(255, 255, 255);'>")
        End If
        doc2.body.innerHTML = HTMLString
    End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文