C# Web 浏览器,点击并突出显示

标签 c# dom

在我开始编写代码之前,我想看看这里是否有人知道已经构建的任何开源(或付费)等价物。

我正在寻找一个浏览器控件,用户可以在其中预览网页,然后突出显示其中的元素,突出显示后,我可以获得所选元素的 div 或 id。

有人见过这样的东西吗?

最佳答案

这是一个使用 .NET WebBrowser 控件的原始版本,它使用 Internet Explorer。

namespace WindowsFormsApplication1
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Windows.Forms;

    public partial class Form1 : System.Windows.Forms.Form
    {
        private HtmlDocument document;

        private IDictionary<HtmlElement, string> elementStyles = new Dictionary<HtmlElement, string>();

        public Form1()
        {
            InitializeComponent();
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            this.Text = e.Url.ToString();
            this.document = this.webBrowser1.Document;
            this.document.MouseOver += new HtmlElementEventHandler(document_MouseOver);
            this.document.MouseLeave += new HtmlElementEventHandler(document_MouseLeave);
        }

        private void document_MouseLeave(object sender, HtmlElementEventArgs e)
        {
            HtmlElement element = e.FromElement;
            if (this.elementStyles.ContainsKey(element))
            {
                string style = this.elementStyles[element];
                this.elementStyles.Remove(element);
                element.Style = style;
            }
        }

        private void document_MouseOver(object sender, HtmlElementEventArgs e)
        {
            HtmlElement element = e.ToElement;
            if (!this.elementStyles.ContainsKey(element))
            {
                string style = element.Style;
                this.elementStyles.Add(element, style);
                element.Style = style + "; background-color: #ffc;";
                this.Text = element.Id ?? "(no id)";
            }
        }
    }
}

关于C# Web 浏览器,点击并突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1090754/

相关文章:

c# - 如何使用多线程 C# 执行控制台应用程序

javascript - firefox 查看源代码

javascript - DocumentFragments 的宽度和高度

javascript - 如果其中一个 <li> 更改位置,则立即在多个 <ul> 中移动列表元素顺序

javascript - 使用 jQuery 在祖先和多个后代之间插入元素

c# - 提高 C# 项目的语言版本号有什么风险?

c# - 在 XamarinForms 应用程序中使用 Android 定位服务

c# - 如何通过 FromSqlRaw 调用 EF Core 3.0 中的存储过程

c# - Outlook VSTO 功能区到主页 tabControlId

javascript - 查找已选中复选框的顺序