none
WPF的WebBrowser控件怎么不能相应鼠标MouseDown事件和触控事件? RRS feed

答案

  • 你好,

    确实如此,WPF中的WebBrowser控件功能不是很强大,你的问题,可以通过以下方式实现:

    1. 使用WindowsFormHost 控件Host一个Winform的WebBrowser控件
    2. 通过向Document嵌入js脚本来实现鼠标事件的相应

    示例代码:

    XAML:

    Loaded="Window_Loaded"  >
    <Grid Name="gridMain" >
            <WindowsFormsHost Name="winFormHost" Opacity = "0">
                <wf:WebBrowser x:Name="webBrowser" Navigating="webBrowser_Navigating" />
            </WindowsFormsHost>
        </Grid>
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
                webBrowser.Navigating += new WebBrowserNavigatingEventHandler(webBrowser_Navigating);
                webBrowser.Navigate("http://www.bing.com");
    
                //Add reference: COM->Microsoft Internet Controls
                SHDocVw.WebBrowser wb = webBrowser.ActiveXInstance as SHDocVw.WebBrowser;
                wb.NavigateComplete2 += new SHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(wb_NavigateComplete2);
    }
    
    private void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
                if (e.Url.ToString().ToLower().Trim('/') == "cmd://onmousedown") 
                {
                    System.Windows.MessageBox.Show("OK!");
                    e.Cancel = true;
                }
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年12月23日 7:08
    版主

全部回复

  • 你好,

    确实如此,WPF中的WebBrowser控件功能不是很强大,你的问题,可以通过以下方式实现:

    1. 使用WindowsFormHost 控件Host一个Winform的WebBrowser控件
    2. 通过向Document嵌入js脚本来实现鼠标事件的相应

    示例代码:

    XAML:

    Loaded="Window_Loaded"  >
    <Grid Name="gridMain" >
            <WindowsFormsHost Name="winFormHost" Opacity = "0">
                <wf:WebBrowser x:Name="webBrowser" Navigating="webBrowser_Navigating" />
            </WindowsFormsHost>
        </Grid>
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
                webBrowser.Navigating += new WebBrowserNavigatingEventHandler(webBrowser_Navigating);
                webBrowser.Navigate("http://www.bing.com");
    
                //Add reference: COM->Microsoft Internet Controls
                SHDocVw.WebBrowser wb = webBrowser.ActiveXInstance as SHDocVw.WebBrowser;
                wb.NavigateComplete2 += new SHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(wb_NavigateComplete2);
    }
    
    private void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
                if (e.Url.ToString().ToLower().Trim('/') == "cmd://onmousedown") 
                {
                    System.Windows.MessageBox.Show("OK!");
                    e.Cancel = true;
                }
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年12月23日 7:08
    版主
  • 还有没有其他可解决的方法,

    现在不想使用WindowsFormHost,Winform的控件,因为winform的控件有窗口,不能在canvas上面设置前后顺序

    2014年1月2日 6:35
  • 你好,

    据我所知,没有其他更好的方法了,也希望有更好的方案出现。

    新年快乐:)


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年1月3日 1:47
    版主

  • 请问怎样捕捉WebBrowser页面的滚动条滚动事件?
    2014年1月3日 6:39
  • 我这里怎么不行呢,你能否把你这个工程传给我看看!
    2014年1月3日 8:23
  • 我这里怎么不行呢,你能否把你这个工程传给我看看!

    下载地址:http://sdrv.ms/1g59ot5

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年1月3日 9:03
    版主
  • 还是不行的,这样点击的时候可以相应,但是点击链接之后不能跳转了???
    2014年1月6日 7:57
  • 是的,这段代码吧所有的跳转链接替换为'cmd://onmousedown'

    doc.parentWindow.execScript("document.onmousedown=function(e) { window.location='cmd://onmousedown' }", "javascript");
    目前还没有别的办法可以去即可以抓住鼠标按下事件,还可以正常跳转。

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年1月6日 10:04
    版主