WPF加載HTML、WPF與JavaScript交互

目錄javascript

1、WebBrowser加載遠程網頁css

2、WebBrowser加載本地網頁,注:不能夠加載本地樣式CSS和腳本JS文件html

3、WebBrowser隱藏網頁的JavaScript錯誤java

4、網頁屏蔽鼠標右鍵、Ctrl+N、Shift+F十、F十一、F5刷新、退格鍵jquery

5、WPF程序與網頁JavaScript交互web

6、建立服務器,提供數據接口、Script、CSS文件服務器


1、WebBrowser加載遠程網頁app

wbrExam.Source = new Uri("http://cnblogs.com/sntetwt");

2、WebBrowser加載本地網頁,注:不能夠加載本地樣式CSS和腳本JS文件less

Uri uri = new Uri("pack://application:,,,/WPFSctipt;component/res/template.html", UriKind.Absolute);
Stream source = Application.GetResourceStream(uri).Stream;
wbrExam.NavigateToStream(source);

3、WebBrowser隱藏網頁的JavaScript錯誤ide

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 });
    }
}

4、網頁屏蔽鼠標右鍵、Ctrl+N、Shift+F十、F十一、F5刷新、退格鍵

<script type="text/javascript">
	//屏蔽鼠標右鍵、Ctrl+N、Shift+F十、F十一、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>

5、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調用JS
private void Button_Click(object sender, RoutedEventArgs e)
{
    this.wbrExam.InvokeScript("invokeScript", new object[] { "CS調用JS" });
}

  

JS調用CS
window.external.HandleMessage("JS調用CS");

  

//提供給CS調用
function invokeScript(args) {
            alert(args);
}

6、建立服務器,提供數據接口、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+F十、F十一、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>
相關文章
相關標籤/搜索