如何为Awesomium WebBrowser控件编写AddressBar

时间:2015-07-03 08:37:32

标签: c# .net winforms awesomium address-bar

我为Awesomium WebBrowser控件的AddressBar编写了以下代码。 一个可以显示悬停网址的标签......

    private void webBrowser_extra_MouseMove(object sender, MouseEventArgs e)
    {
        string js = @"(function foo(val) { return val; })();
        document.onmouseover = function(e){
            targ = getTarget(e);
            targ.style.border = '1px solid #F00';
            if(targ.tagName == 'A' || targ.tagName == 'a')
            {
                //alert('a');
                var href = targ.getAttribute('href');
                //alert(href);
                foo(href);
            }
        };

        document.onmouseout = function(e){
            getTarget(e).style.border = 'none';
        };

        function getTarget(e){
            if (e.target) return e.target;
            else if (e.srcElement) return e.srcElement;
        }";

        dynamic link = webBrowser_extra.ExecuteJavascriptWithResult(js);
        if (link != null)
        {
                lbl_hover.Text = "Address : " + link;
                toolTip1.SetToolTip(lbl_hover, "Address : " + link);
        }
    }

但我总是在lbl_hover中有[object] [object]! 我如何修复这些代码或如何为我的目的编写更好的代码?

1 个答案:

答案 0 :(得分:2)

如果您需要知道一个悬停的URL,那么为此目的,Awesomium WebControl有一个事件TargetURLChanged。

/// <summary>
/// Occurs when the target URL has changed. This
/// is usually the result of hovering over a link on a page.
/// </summary>
public event UrlEventHandler TargetURLChanged;