0

I have a pretty simple problem but I can't get it to work. I want a MessageBox to appear each time I left click inside my form. I didn't know how to capture it on the whole form so I started of trying to capture my left click inside my WebBrowser1. However, nothing really happens when trying to trigger the event.

I declared the action as WebBrowser1_Mousedown.

private void WebBrowser1_Mousedown(object sender, MouseButtonEventArgs e)
{
        if (e.LeftButton == MouseButtonState.Pressed)
        {

            MessageBox.Show("test");
        }

 }

What am I doing wrong?

My relevant XAML as follows:

<Window x:Class="IndianBrowser.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="488.806" Width="807.089" MouseDown="Window_MouseDown">

and now trying with the webbrowser:

<WebBrowser x:Name="WebBrowser1" HorizontalAlignment="Stretch" Height="auto"    Margin="0,85,0,0" VerticalAlignment="Stretch" Width="auto" MouseDown="WebBrowser1_Mousedown"/>
proah
  • 354
  • 4
  • 16
  • AS you're using WPF, you're most likely hosting the webbrowser in another XAML element (window at least) so maybe you can catch the event on that control and see on what control the event is triggered. – Terry Jan 16 '14 at 08:34
  • Thanks, of course I can capture the event in the whole XAML window. But the problem still persists. Nothing simply happens. – proah Jan 16 '14 at 08:41
  • @Djerry I'm trying to use the `private void Window_MouseDown(object sender, MouseButtonEventArgs e)` but the messagebox doesn't show up when I try to trigger it. – proah Jan 16 '14 at 08:52
  • Can you share your XAML, so to see which controls you use? – Terry Jan 16 '14 at 08:52
  • Sure, added relevant info in the main post. – proah Jan 16 '14 at 08:59
  • possible duplicate of [WPF WebBrowser Mouse Events not working as expected](http://stackoverflow.com/questions/2189510/wpf-webbrowser-mouse-events-not-working-as-expected) – Terry Jan 16 '14 at 09:11

1 Answers1

1

If you look into MSDN documentation for WebBrowser class, you'll see that mouse events are not supported. What you can do instead is subscribe for HtmlDocument.MouseDown event.

Update


Here is small snippet that demonstrates how to do this in WPF, NOTE you will have to add reference to Microsoft.mshtml assembly:

public MainWindow()
{
    InitializeComponent();
    this.webBrowser1.Navigated += webBrowser1_Navigated;
    this.webBrowser1.Source = new Uri("your url");
}

void webBrowser1_Navigated(object sender, NavigationEventArgs e)
{
    HTMLDocumentClass document = this.webBrowser1.Document as HTMLDocumentClass;
    document.HTMLDocumentEvents2_Event_onclick += document_HTMLDocumentEvents2_Event_onclick;
}

bool document_HTMLDocumentEvents2_Event_onclick(IHTMLEventObj pEvtObj)
{
    // here you can check if the clicked element is your form
    // if (pEvtObj.fromElement.id == "some id")
    MessageBox.Show("test");
    return true;
}
Yurii
  • 4,811
  • 7
  • 32
  • 41
  • Hmm, I can't get the `HtmlElementEventArgs` as posted in your link to work. Could you show an example? My initial problem of wanting to capture my whole form still persists. Is it possible to do by making a transparent rectangle? Maybe it's possible to use the `MouseButtonEventArgs` then? – proah Jan 16 '14 at 08:37
  • @proah, I've updated the answer, please take a look. – Yurii Jan 16 '14 at 09:13
  • I get the error `Interop type 'mshtml.HTMLDocumentClass' cannot be embedded. Use the applicable interface instead.` – proah Jan 16 '14 at 09:29
  • Select `Microsoft.mshtml` reference in Solution Explorer and set 'Embed Interop Types' in Properties window to `False`. – Yurii Jan 16 '14 at 09:33
  • Additional question, is it possible to do the same thing but with `hover`? – proah Jan 16 '14 at 10:03
  • Check `HTMLDocumentEvents2_Event_onmouseover` event of the `HTMLDocumentClass`. – Yurii Jan 16 '14 at 10:23