1

I'm making a WPF program with WebBrowser that will be playing a video. I wanted to know if there was a way to disable mouse events for the webpage inside of it, so that the video controls don't appear when the mouse goes over it.

I've tried placing objects like a button infront of the webpage and made its opacity 0, but mouse events still went through.

I've changed the WebBrowser.IsEnabled property to false, but once again, the browser's content detects mouse overs.

Is there a way to get the desired result?

1
  • A transparent control does not trigger the mouse event try setting its background to #01ffffff, thats almost fully transparent (thats a dirty way of solving your issue) Commented May 12, 2019 at 9:22

1 Answer 1

3

The WebBrowser control is not a usual wpf control. Its an activeX host that hosts the browser inside a new window that is drawn on top of your wpf window. This is known as the windows 'Airspace issue'.

This is why setting the IsEnabled property or placing another control on top of it does not work. Since the WebBrowser has its own window, the only way to prevent mouse input to your web page is to prevent mouse input to the browsers window. You can do this with an external call to EnableWindow(IntPtr hWnd, bool enable).

Here is a small sample:

MainWindow.xaml:

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Grid>
        <WebBrowser x:Name="Browser"/>
    </Grid>
</Window>

MainWindow.xaml.cs:

namespace Test
{
    static class Win32
    {
        [DllImport("user32.dll")]
        public static extern bool EnableWindow(IntPtr hWnd, bool enable);
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Win32.EnableWindow(Browser.Handle, false);
        }
    }
}

This will disable all mouse and keyboard inputs to the browser window.

1
  • Works perfectly! Thank you.
    – nreh
    Commented May 13, 2019 at 4:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.