积极答复者
WPF的WebBrowser控件怎么不能相应鼠标MouseDown事件和触控事件?

问题
-
WPF的WebBrowser控件怎么不能相应鼠标MouseDown事件和触控事件?
- 已更改类型 Franklin ChenMicrosoft employee, Moderator 2013年12月23日 7:09 Question
答案
-
你好,
确实如此,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.- 已标记为答案 Franklin ChenMicrosoft employee, Moderator 2013年12月30日 10:55
全部回复
-
你好,
确实如此,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.- 已标记为答案 Franklin ChenMicrosoft employee, Moderator 2013年12月30日 10:55
-
你好,
据我所知,没有其他更好的方法了,也希望有更好的方案出现。
新年快乐:)
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. -
我这里怎么不行呢,你能否把你这个工程传给我看看!
下载地址:http://sdrv.ms/1g59ot5We 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. -
是的,这段代码吧所有的跳转链接替换为'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.