WebBrowser控件中的可单击链接处于编辑模式

时间:2009-08-11 15:53:53

标签: c# winforms webbrowser-control

我在编辑模式下使用Windows.Forms WebBrowser控件在我们的应用程序中启用html编辑(.net 3.5 C#窗体)。问题是在编辑模式下,html中的链接不可点击(即悬停在它们上面不显示手形鼠标光标,单击它们只是将光标插入该点,而不是导航到该链接)。

我意识到这是设计的,但是可以在保持链接功能的同时启用编辑模式吗?我们希望用户在能够编辑内容的同时浏览一组本地html页面。

我正在设置这样的编辑模式,如果它改变了什么:

webBrowser.Document.ExecCommand("EditMode", false, null);

2 个答案:

答案 0 :(得分:2)

这是一个小型的WinForm应用程序似乎有效。设置编辑模式时,它会记录所有A标签的所有链接位置,然后检查鼠标的光标位置。如果有标签的框架或嵌套,我不确定OffsetRectangle是否给出了正确的值,但应用程序中的示例html是有效的。

如果需要,可以修改它以从其他标签捕获onclick等。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private readonly Dictionary<Rectangle, Uri> _links = new Dictionary<Rectangle, Uri>();
        private readonly Regex _reLink = new Regex("href=['\"](?<link>.*?)['\"]");

        private const string _html =
            "<html><body><br><div style='top:20px;left:50px;border:solid 1px red'><a href='http://www.cnn.com'>CNN</a></div><br><font size='14'>xx</font><a href='http://stackoverflow.com'>stackoverflow</a></body></html>";

        private bool _isEditMode;

        public Form1()
        {
            InitializeComponent();
            webBrowser1.DocumentText = _html;
            webBrowser1.Document.Click += Document_Click;
            webBrowser1.Document.MouseMove += Document_MouseMove;
        }

        private void Document_MouseMove(object sender, HtmlElementEventArgs e)
        {
            if (!_isEditMode) return;
            ChangeCursorIfOverLink(e);
        }

        private void ChangeCursorIfOverLink(HtmlElementEventArgs e)
        {
            foreach (KeyValuePair<Rectangle, Uri> link in _links)
            {
                if (CursorWithinControl(e, link.Key))
                {
                    if (Cursor.Current != Cursors.Hand)
                        Cursor.Current = Cursors.Hand;
                    return;
                }
            }
            Cursor.Current = Cursors.Default;
        }

        private void Document_Click(object sender, HtmlElementEventArgs e)
        {
            NavigateLinkInEditMode(e);
        }

        private void NavigateLinkInEditMode(HtmlElementEventArgs e)
        {
            if (_isEditMode)
            {
                foreach (KeyValuePair<Rectangle, Uri> link in _links)
                {
                    if (CursorWithinControl(e, link.Key))
                    {
                        webBrowser1.Navigate(link.Value);
                        return;
                    }
                }
            }
        }

        private bool CursorWithinControl(HtmlElementEventArgs e, Rectangle rectangle)
        {
            return e.MousePosition.X >= rectangle.Left
                   && e.MousePosition.X <= rectangle.Left + rectangle.Width
                   && e.MousePosition.Y >= rectangle.Top
                   && e.MousePosition.Y <= rectangle.Top + rectangle.Height;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RecordLinkPositions();
            webBrowser1.Document.ExecCommand("EditMode", false, null);
            webBrowser1.DocumentText = _html;
            _isEditMode = true;
        }

        private void RecordLinkPositions()
        {
            foreach (HtmlElement element in webBrowser1.Document.All)
            {
                if (element.TagName == "A")
                {
                    string url = _reLink.Match(element.OuterHtml).Groups["link"].Value;
                    _links.Add(element.OffsetRectangle, new Uri(url));
                }
            }
        }
    }
}

答案 1 :(得分:0)

我会检查用户何时悬停在某个链接上,并可选择在状态栏中显示某些内容。允许用户按CTRL +单击链接将其打开。

编辑: 这可能很有用:http://www.codeproject.com/KB/mobile/browsermouseevents.aspx?msg=2732899

相关问题