Web浏览器和自定义上下文菜单栏-问题 [英] Web browser and custom Context menu strip -problems

查看:72
本文介绍了Web浏览器和自定义上下文菜单栏-问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WebControl中,当鼠标光标为手"(通常在链接,图像上)时,我希望自定义contextMenu处于打开或关闭状态.我试着做点东西,但是不起作用 [右键单击时,始终显示ContextMenuStrip] .为什么会这样?
这是代码:

In a webControl, when the mouse cursor is "Hand" (usually over links,images), I want a custom contextMenu to be on or off. I tried to make something but is not working [the ContextMenuStrip is always showing,when I right click]. Why is this happening?
here is the code:

public Form1()
{
    InitializeComponent();
    //I already set this in properties - showing here for clarity
    webBrowser1.IsWebBrowserContextMenuEnabled = false;
    webBrowser1.ContextMenuStrip = contextMenuStrip1;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (this.Cursor == Cursors.Hand) contextMenuStrip1.Visible = true;
    else contextMenuStrip1.Visible = false;
}



我想我必须为Cursor创建一个新事件...但也无法正常工作.我真的不知道该怎么做.我不是制作事件的专家.我不太了解它们.



I suppose I must create a new event for Cursor...but is not working either. I don''t really know how to do it right. Im not expert in making events. I don''t understand them fully.

public Form1()
        {
            InitializeComponent();
            CursorChanged += new EventHandler(Form1_CursorChanged);
        }
void Form1_CursorChanged(object sender, EventArgs e)
       {
            if (this.Cursor == Cursors.Hand) contextMenuStrip1.Visible = true;
            else contextMenuStrip1.Visible = false;
       }


---------------------
我知道了.至少你能给我写信我该怎么办?
编写一个伪代码或只是描述要执行的操作.
Thanks


---------------------
I see.At least can you write me what can I do?
Write a pseudocode or just describe what to do if its lot to do.
Thanks

推荐答案

检查一下,它将完成您想要做的事情.我仍然不能保证这段代码,因为我以前从未做过.我只是想出了这种混乱. :)让我知道它是否对您有用.

///方法3///
这是我为此可以想出的最后也是最后一个方法.
对于我所做的小测试,它似乎适用于所有内容!
我不确定性能,尽管如此,我猜只有时间会证明一切.
我实际上是用您的想法来利用计时器的.
计时器每100毫秒更新一次所有链接MouseDown事件(如果需要,您可以尝试使用更高的值来尝试改善性能),所以我不确定,至少对于包含大量链接的网站,这将如何影响应用程序的性能.
无论如何,就像我说的那样,它似乎可以与我尝试过的所有功能配合使用(以及Google即时预测).

让我知道它如何为您工作.

Check this out it will do what you are wanting to do. I cannot guarantee this code in anyway though because I have never done this before. I just came up with this messing around. :) Let me know if it works for you.

/// METHOD 3 ///
Here is my last and final method I could come up with for doing this.
For the little testing I done it seems to work for everything!
I am not sure about performance though for that I guess only time will tell.
I actually used your idea to make use of a timer.
The timer updates all of the links MouseDown events every 100 milliseconds (you can try experimenting with higher values to possibly improve performance if needed) so I am not sure how this will affect the performance of your app at least with websites that have lots of links.
Anyway like I said it seems to work fine with everything I tried (Google instant predictions as well).

Let me know how it works for you.

public partial class Form1 : Form
{
    Timer updateLinksTimer;

    public Form1()
    {
        InitializeComponent();

        updateLinksTimer = new Timer();
        // 100 = 100ms you can experiment with different values
        updateLinksTimer.Interval = 100;
        updateLinksTimer.Enabled = true;
        updateLinksTimer.Tick += new EventHandler(updateLinksTimer_Tick);

        webBrowser1.IsWebBrowserContextMenuEnabled = false;
        webBrowser1.ContextMenuStrip = contextMenuStrip_Text;

        contextMenuStrip_Links.Closed +=
            new ToolStripDropDownClosedEventHandler(contextMenuStrip_Links_Closed);

        webBrowser1.Navigate("google.com");
    }

    void contextMenuStrip_Links_Closed(object sender, ToolStripDropDownClosedEventArgs e)
    {
        webBrowser1.ContextMenuStrip = contextMenuStrip_Text;
    }

    void DocumentLink_MouseDown(object sender, HtmlElementEventArgs e)
    {
        webBrowser1.ContextMenuStrip = contextMenuStrip_Links;
    }

    private void updateLinksTimer_Tick(object sender, EventArgs e)
    {
        if (webBrowser1.Document != null)
        {
            for (int i = 0; i < webBrowser1.Document.Links.Count; i++)
            {
                webBrowser1.Document.Links[i].MouseDown -= DocumentLink_MouseDown;
                webBrowser1.Document.Links[i].MouseDown +=
                    new HtmlElementEventHandler(DocumentLink_MouseDown);
            }
        }
    }
}





///方法2///
注意:到目前为止,我无法使用此方法来处理也是链接的图像!但是,它要简单得多,似乎没有其他方法所具有的google搜索问题.





/// METHOD 2 ///
NOTE: So far I cannot get this method to work with images that are also links! However it is much simpler and seems to not have the problems with google search that the other method has.

public Form1()
{
    InitializeComponent();

    webBrowser1.IsWebBrowserContextMenuEnabled = false;
    webBrowser1.ContextMenuStrip = contextMenuStrip_Text;

    webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

    webBrowser1.Navigate("http://www.google.com/");
}

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    webBrowser1.Document.MouseDown += new HtmlElementEventHandler(Document_MouseDown);
}

void Document_MouseDown(object sender, HtmlElementEventArgs e)
{
    HtmlElement elem = webBrowser1.Document.GetElementFromPoint(e.ClientMousePosition);

    for (int i = 0; i < webBrowser1.Document.Links.Count; i++)
    {
        if (elem == webBrowser1.Document.Links[i])
        {
            webBrowser1.ContextMenuStrip = contextMenuStrip_Links;
            return;
        }
    }

    webBrowser1.ContextMenuStrip = contextMenuStrip_Text;
}





///方法1///





/// METHOD 1 ///

public partial class Form1 : Form
{
    HtmlElementCollection links = null;

    public Form1()
    {
        InitializeComponent();

        webBrowser1.Navigating += 
            new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);

        webBrowser1.DocumentCompleted +=
            new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

        webBrowser1.IsWebBrowserContextMenuEnabled = false;
        webBrowser1.ContextMenuStrip = contextMenuStrip2;// You're contextMenuStrip_Text

        webBrowser1.Navigate("http://www.google.com/");
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        links = webBrowser1.Document.Links;
        for (int i = 0; i < links.Count; i++)
        {
            links[i].MouseEnter += new HtmlElementEventHandler(link_MouseEnter);
            links[i].MouseLeave += new HtmlElementEventHandler(link_MouseLeave);
        }
    }

    void link_MouseEnter(object sender, HtmlElementEventArgs e)
    {
        // show custom context menu strip for links
        webBrowser1.ContextMenuStrip = contextMenuStrip1;// You're contextMenuStrip_Links
    }

    void link_MouseLeave(object sender, HtmlElementEventArgs e)
    {
        // show custom context menu strip for everything else
        webBrowser1.ContextMenuStrip = contextMenuStrip2;// You're contextMenuStrip_Text
    }

    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        if (links != null)
        {
            for (int i = 0; i < links.Count; i++)
            {
                links[i].MouseEnter -= link_MouseEnter;
                links[i].MouseLeave -= link_MouseLeave;
            }
        }
    }
}


我认为您会发现当图标更改时,Web浏览器控件不会向您的表单发送事件.另外,如果您不太了解事件的工作原理,则应该在线阅读一些内容.使用您自己的右键菜单控制Web浏览器的目的是什么?考虑到浏览器有它自己的右键单击菜单,如果可能的话我也会感到惊讶.
I think you''ll find the web browser control is not going to send an event to your form when the icon changes. Also, if you don''t really understand how events work, you should do some reading on that online. What''s the purpose of a web browser control with your own right click menu ? I''d be surprised if that was possible, too, given that the browser has it''s own right click menu.


这篇关于Web浏览器和自定义上下文菜单栏-问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆