標籤:xmlns info com flags input service jquer 資料 out
目錄
一、WebBrowser載入遠程網頁
二、WebBrowser載入本地網頁,註:不可以載入本機樣式CSS和指令碼JS檔案
三、WebBrowser隱藏網頁的JavaScript錯誤
四、網頁屏蔽滑鼠右鍵、Ctrl+N、Shift+F10、F11、F5重新整理、退格鍵
五、WPF程式與網頁JavaScript互動
六、建立伺服器,提供資料介面、Script、CSS檔案
一、WebBrowser載入遠程網頁
wbrExam.Source = new Uri("http://cnblogs.com/sntetwt");
二、WebBrowser載入本地網頁,註:不可以載入本機樣式CSS和指令碼JS檔案
Uri uri = new Uri("pack://application:,,,/WPFSctipt;component/res/template.html", UriKind.Absolute);Stream source = Application.GetResourceStream(uri).Stream;wbrExam.NavigateToStream(source);
三、WebBrowser隱藏網頁的JavaScript錯誤
this.wbrExam.SuppressScriptErrors(true);
/// <summary>/// WebBrowser隱藏網頁的JavaScript錯誤/// </summary>public static class WebBrowserExtensions{ public static void SuppressScriptErrors(this WebBrowser webBrowser, bool hide) { FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic); if (fiComWebBrowser == null) return; object objComWebBrowser = fiComWebBrowser.GetValue(webBrowser); if (objComWebBrowser == null) return; objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { hide }); }}
四、網頁屏蔽滑鼠右鍵、Ctrl+N、Shift+F10、F11、F5重新整理、退格鍵
<script type="text/javascript">//屏蔽滑鼠右鍵、Ctrl+N、Shift+F10、F11、F5重新整理、退格鍵 document.oncontextmenu = function(){event.returnValue=false;}//屏蔽滑鼠右鍵 window.onhelp = function(){return false} //屏蔽F1協助 document.onkeydown = function(){ if ((window.event.altKey)&& ((window.event.keyCode==37) || //屏蔽 Alt+ 方向鍵 ← (window.event.keyCode == 39))) { //屏蔽 Alt+ 方向鍵 → event.returnValue = false; return false;} /* 註:這還不是真正地屏蔽Alt+方向鍵, 因為Alt+方向鍵彈出警告框時,按住Alt鍵不放, 用滑鼠點掉警告框,這種屏蔽方法就失效了。*/ if ((event.keyCode==8) || //屏蔽退格刪除鍵 (event.keyCode==116) || //屏蔽 F5 重新整理鍵 (event.ctrlKe && event.keyCode==82)) { //Ctrl + R event.keyCode=0; event.returnValue=false; } if (event.keyCode==122){event.keyCode=0;event.returnValue=false;} //屏蔽F11 if (event.ctrlKey && event.keyCode==78) event.returnValue=false; //屏蔽Ctrl+n if (event.shiftKey && event.keyCode==121)event.returnValue=false; //屏蔽shift+F10 if (window.event.srcElement.tagName == "A" && window.event.shiftKey) window.event.returnValue = false; //屏蔽shift加滑鼠左鍵新開一網頁 if ((window.event.altKey)&&(window.event.keyCode==115)){ //屏蔽Alt+F4 window.showModelessDialog("about:blank","","dialogWidth:1px;dialogheight:1px"); return false; } } </script>
五、WPF程式與網頁JavaScript互動
public void Message(string str){ MessageBox.Show(str);}/// <summary>/// WebBrowser與JavaScript互動/// </summary>[System.Runtime.InteropServices.ComVisible(true)]public class OprateBasic{ private MainWindow instance; public OprateBasic(MainWindow instance) { this.instance = instance; } //提供給JS調用 public void HandleMessage(string p) { instance.Message(p); }}//CS調用JSprivate void Button_Click(object sender, RoutedEventArgs e){ this.wbrExam.InvokeScript("invokeScript", new object[] { "CS調用JS" });}
JS調用CSwindow.external.HandleMessage("JS調用CS");
//提供給CS調用function invokeScript(args) { alert(args);}
六、建立伺服器,提供資料介面、Script、CSS檔案
總結:因為載入HTML檔案的時候,HTML沒有路徑,所以載入不了JS和CSS等外部檔案
建立遠端站台提供資料介面和外部檔案
完整的CS程式如下
using System;using System.Collections.Generic;using System.Linq;using System.IO;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Reflection;namespace WPFSctipt{ /// <summary> /// MainWindow.xaml 的互動邏輯 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_ContentRendered(object sender, EventArgs e) { //載入遠程網頁 //wbrExam.Source = new Uri("http://cnblogs.com/sntetwt"); Uri uri = new Uri("pack://application:,,,/WPFSctipt;component/res/template.html", UriKind.Absolute); Stream source = Application.GetResourceStream(uri).Stream; //WebBrowser隱藏網頁的JavaScript錯誤 this.wbrExam.SuppressScriptErrors(true); //WebBrowser與JavaScript互動 this.wbrExam.ObjectForScripting = new OprateBasic(this); //載入本地HTML檔案 wbrExam.NavigateToStream(source); } public void Message(string str) { MessageBox.Show(str); } /// <summary> /// WebBrowser與JavaScript互動 /// </summary> [System.Runtime.InteropServices.ComVisible(true)] public class OprateBasic { private MainWindow instance; public OprateBasic(MainWindow instance) { this.instance = instance; } //提供給JS調用 public void HandleMessage(string p) { instance.Message(p); } } //CS調用JS private void Button_Click(object sender, RoutedEventArgs e) { this.wbrExam.InvokeScript("invokeScript", new object[] { "CS調用JS" }); } } /// <summary> /// WebBrowser隱藏網頁的JavaScript錯誤 /// </summary> public static class WebBrowserExtensions { public static void SuppressScriptErrors(this WebBrowser webBrowser, bool hide) { FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic); if (fiComWebBrowser == null) return; object objComWebBrowser = fiComWebBrowser.GetValue(webBrowser); if (objComWebBrowser == null) return; objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { hide }); } }}
ASMX檔案代碼
<Window x:Class="WPFSctipt.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF載入HTML、WPF與JavaScript互動" Height="600" Width="800" ContentRendered="Window_ContentRendered"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="500"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <WebBrowser Grid.Row="0" Name="wbrExam" Height="500" VerticalAlignment="Top" Width="800"/> <Button Grid.Row="1" Content="CS調用JS" VerticalAlignment="Top" Width="200" Height="40" Margin="10" Click="Button_Click" /> </Grid></Window>
HTML代碼
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=8" /> <title></title> <style type="text/css"> body{background:red;} .div{height:400px;} </style> <script type="text/javascript" src="http://common.cnblogs.com/script/jquery.js"></script><script type="text/javascript">//屏蔽滑鼠右鍵、Ctrl+N、Shift+F10、F11、F5重新整理、退格鍵 document.oncontextmenu = function(){event.returnValue=false;}//屏蔽滑鼠右鍵 window.onhelp = function(){return false} //屏蔽F1協助 document.onkeydown = function(){ if ((window.event.altKey)&& ((window.event.keyCode==37) || //屏蔽 Alt+ 方向鍵 ← (window.event.keyCode == 39))) { //屏蔽 Alt+ 方向鍵 → event.returnValue = false; return false; } /* 註:這還不是真正地屏蔽Alt+方向鍵, 因為Alt+方向鍵彈出警告框時,按住Alt鍵不放, 用滑鼠點掉警告框,這種屏蔽方法就失效了。*/ if ((event.keyCode==8) || //屏蔽退格刪除鍵 (event.keyCode==116) || //屏蔽 F5 重新整理鍵 (event.ctrlKe && event.keyCode==82)) { //Ctrl + R event.keyCode=0; event.returnValue=false; } if (event.keyCode==122){event.keyCode=0;event.returnValue=false;} //屏蔽F11 if (event.ctrlKey && event.keyCode==78) event.returnValue=false; //屏蔽Ctrl+n if (event.shiftKey && event.keyCode==121)event.returnValue=false; //屏蔽shift+F10 if (window.event.srcElement.tagName == "A" && window.event.shiftKey) window.event.returnValue = false; //屏蔽shift加滑鼠左鍵新開一網頁 if ((window.event.altKey)&&(window.event.keyCode==115)){ //屏蔽Alt+F4 window.showModelessDialog("about:blank","","dialogWidth:1px;dialogheight:1px"); return false; } } </script> <script type="text/javascript"> $(function () { $("div").text("JavaScript被執行"); window.external.HandleMessage("JS調用CS"); }) function invokeScript(args) { alert(args); } </script></head><body> <div class="div"></div></body></html>
WPF載入HTML、WPF與JavaScript互動