0

I'm building a small WPF application to be run on a tablet which has a DockPanel with some kind of content and a set of buttons which, when pressed, load media of various kinds as the content to be displayed, including images, video files, PDFs and webpages. The default content is a slideshow of images and if no input is detected after a certain amount of time, the current content should revert to the slideshow.

I've been doing this by having a timer fire every second, increasing a counter by 1 and returning to the slideshow if the counter exceeds some value, and having a PreviewMouseMove event tied to my main window which resets the counter to 0. This works fine for the images and video files, but the PDFs and webpages, which I'm displaying in a WebBrowser control, prevent the PreviewMouseMove event from firing somehow. Am I right in thinking this should be impossible? If not, is there any other way to detect mouse motion globally, or to get events from the WebBrowser? It seems like odd behavior from the WebBrowser with mouse events is common, and there are a number of questions about it but I've only been able to find examples using the WinForms implementation of the browser rather than the Windows.Controls.WebBrowser, which has a different Document property.

P...
  • 655
  • 2
  • 6
  • 14
  • I believe it's possible but remember the web browser is it's own Stack and everything in the browser is Javascript. You can set up event hanlder's via javascript to do things, but then your C# program must be notified. Look for examples on how to get a C# program to register with the web browser to be notified of events. I'll post some sample code in answer. – JWP Jun 05 '17 at 01:15

1 Answers1

0
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        //must wait for load completed
        webBrowser1.LoadCompleted += new LoadCompletedEventHandler(webBrowser1_LoadCompleted);
    }

    private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
    {
        mshtml.HTMLDocument doc;
        doc = (mshtml.HTMLDocument)webBrowser1.Document;
        //get the IHTMLDocumentEvents2 interface
        mshtml.HTMLDocumentEvents2_Event iEvent;
        iEvent = (mshtml.HTMLDocumentEvents2_Event)doc;
        //Register a handler
        iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);
    }

    //here's the handler
    private bool ClickEventHandler(mshtml.IHTMLEventObj e)
    {
        textBox1.Text = "Item Clicked";
        return true;
    }
}
JWP
  • 6,672
  • 3
  • 50
  • 74