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();
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);
}
}
}
}
/// 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;
}
/// 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;
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)
{
webBrowser1.ContextMenuStrip = contextMenuStrip1;
}
void link_MouseLeave(object sender, HtmlElementEventArgs e)
{
webBrowser1.ContextMenuStrip = contextMenuStrip2;
}
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;
}
}
}
}