WPF加载HTML、WPF与JavaScript交互
目录
一、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调用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);
}
六、创建服务器,提供数据接口、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交互的更多相关文章
- WPF加载等待动画
原文:WPF加载等待动画 原文地址:https://www.codeproject.com/Articles/57984/WPF-Loading-Wait-Adorner 界面遮罩 <UserC ...
- WPF加载Winform窗体时 报错:子控件不能为顶级窗体
一.wpf项目中引用WindowsFormsIntegration和System.Windows.Forms 二.Form1.Designer.cs 的 partial class Form1 设置为 ...
- WPF 加载等待动画
原文:WPF 加载等待动画 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_29844879/article/details/80216587 ...
- js文件加载太慢,JavaScript文件加载加速
原文出自:https://blog.csdn.net/seesun2012 js脚本加载太慢,JavaScript脚本加载加速(亲测有效) 测试背景: JS文件大小:6.1kB 传统形式加载js文件: ...
- WPF加载程序集中字符串资源
WPF资源 WPF资源使用其实的也是resources格式嵌入资源,默认的资源名称为"应用程序名.g.resources",不过WPF资源使用的pack URI来访问资源. 添加图 ...
- WPF 加载 WINFORM控件 异常: 调度程序进程已挂起,但消息仍在处理中
在加载TradeAtServer的统计中的 单个合约盈亏情况 异常:,调度程序进程已挂起,但消息仍在处理中 发现可能是属性设置引发的问题 比如DateTimePikcer.Value+= set, g ...
- RequireJS加载ArcGIS API for JavaScript
1.在main.js中配置ArcGIS API for JavaScript require.config({ paths : { //arcgisJS "esri": " ...
- 网页加载进度的实现--JavaScript基础
总结了一些网页加载进度的实现方式…… 1.定时器实现加载进度 <!DOCTYPE html><html lang="en"><head> < ...
- js怎么动态加载js文件(JavaScript性能优化篇)
下面介绍一种JS代码优化的一个小技巧,通过动态加载引入js外部文件来提高网页加载速度 [基本优化] 将所有需要的<script>标签都放在</body>之前,确保脚本执行之前完 ...
随机推荐
- java 对mongodb的操作
java 对mongodb的操作 1.1连单台mongodb Mongo mg = newMongo();//默认连本机127.0.0.1 端口为27017 Mongo mg = newMongo( ...
- 线程池大小设置,CPU的核心数、线程数的关系和区别,同步与堵塞完全是两码事
线程池应该设置多少线程合适,怎么样估算出来.最近接触到一些相关资料,现作如下总结. 最开始接触线程池的时候,没有想到就仅仅是设置一个线程池的大小居然还有这么多的学问,汗颜啊. 首先,需要考虑到线程池所 ...
- 从零开始,运行一个android例子程序
电脑上连个eclipse都没装,怎么玩android?一穷二白的你, 下面就跟随我,从零开始,一步一步操作,运行我们的第一个android应用程序.我一直相信,学习开发,只有在调试过程中学的是最快的. ...
- 【GitLab】【GitHub】GitLab和GitHub的双向同步
有这种需求,需要GitLab上的现有代码库,同步到GitHub上. 又有一种需求,需要将GitHub上的代码库,同步到gitlab上. 一.GitLab上的代码库,自动同步到GitHub上 大致需要三 ...
- 【IntelliJ IDEA】idea设置UTF-8的位置
如下图,JetBrains系列所有IDE都可以设置的位置
- 自己用图片做的可旋转、不确定进度的ProgressBar
今天看到一个人将图片写个动画,然后就当做progressbar用了,思路挺棒的. 原文地址:http://blog.csdn.net/sweetvvck/article/details/2388253 ...
- java 生成zip文件并导出
总结一下,关于Java下载zip文件并导出的方法,浏览器导出. String downloadName = "下载文件名称.zip"; downloadName = Browser ...
- IP组播
1 IP组播基础 IP组播技术有效地解决了单点发送.多点接收的问题.组播源只发送一份数据,被传递的信息在距组播源尽可能远的网络节点才开始被复制和分发,并且只发送给需要该信息的接收者. 说明: 本章 ...
- 凝聚法层次聚类之ward linkage method
凝聚法分层聚类中有一堆方法可以用来算两点(pair)之间的距离:欧式,欧式平方,manhattan等,还有一堆方法可以算类(cluster)与类之间的距离,什么single-linkage.compl ...
- [转]通过查看mysql 配置参数、状态来优化你的mysql
From : http://wangwei007.blog.51cto.com/68019/967278 mysql的监控方法大致分为两类: 1.连接到mysql数据库内部,使用show status ...