using mshtml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
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;

namespace Web_wpf
{
////c#调用web里的js,以及js调用C#里的函数, c#自己生成js代码,实现对web的控制

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
initWb();
}
public void initWb()
{

wb1.Navigate("http://www.wjf.com/test.html");//= new Uri(file.FullName);

// IHTMLDocument2 doc = wb1.Document;

#region MyRegion
// //System.IO.FileInfo file = new System.IO.FileInfo("index.htm");
//// WebBrowser控件显示的网页路径
//wb1.Navigate("http://www.wjf.com/test.html");//= new Uri(file.FullName);
//// 将当前类设置为可由脚本访问
//info info = new info();
//info.mw = this;
//wb1.ObjectForScripting = info;
#endregion
}

public void kuayu()
{
//IHTMLDocument2 doc = (mshtml.IHTMLDocument2)wb1.Document;
//FramesCollection frames = doc.parentWindow.frames;
//String testValue = doc.GetElementById("search").GetAttribute("value");
////IHTMLDocument3 doc = ;

//IHTMLDocument3 baiduDoc = CorssDomainHelper.GetDocumentFromWindow(frames[0].DomWindow as IHTMLWindow2);
//baiduDoc.getElementById("kw").setAttribute("value", testValue);
//baiduDoc.getElementById("su").click();
}

private void CallIHTMLwindow2()
{
string scriptline01 = @"function ShowPageInfo() {";
string scriptline02 = @" var numLinks = document.links.length; ";
string scriptline03 = @" var numForms = document.forms.length; ";
string scriptline04 = @" var numImages = document.images.length; ";
string scriptline05 = @" var numScripts = document.scripts.length; ";
string scriptline06 = @" alert('网页的统计结果:\r\n链接数:' + numLinks + ";
string scriptline07 = @" '\r\n表单数:' + numForms + ";
string scriptline08 = @" '\r\n图像数:' + numImages + ";
string scriptline09 = @" '\r\n脚本数:' + numScripts);}";
string scriptline10 = @"ShowPageInfo();";
string strScript = scriptline01 + scriptline02 + scriptline03 + scriptline04 + scriptline05 +
scriptline06 + scriptline07 + scriptline08 + scriptline09 + scriptline10;
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)wb1.Document;
mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)doc.parentWindow;

win.execScript(strScript, "Javascript");
}
//C# call js
private void Button_Click(object sender, RoutedEventArgs e)
{
wb1.InvokeScript("messageBox", new string[] { "123", "456" });
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
CallIHTMLwindow2();
}
}

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]//  注意: 类定义前需要加上下面两行,否则调用失败!
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class info
{
public MainWindow mw;
public void ShowMessage(string message)
{
MessageBox.Show(message);
// mw.Close();
}
}

}

c#调用js,以及js调用C#里的函数, c#自己生成js代码,实现对web的控制的更多相关文章

  1. [转]html页面调用js文件里的函数报错onclick is not defined处理方法

    原文地址:http://blog.csdn.net/ywl570717586/article/details/53130863 今天处理html标签里的onclick功能的时候总是报错:Uncaugh ...

  2. jsp页面:js方法里嵌套java代码(是操作数据库的),如果这个js 方法没被调用,当jsp页面被解析的时候,不管这个js方法有没有被调用这段java代码都会被执行?

    jsp页面:js方法里嵌套java代码(是操作数据库的),如果这个js 方法没被调用,当jsp页面被解析的时候,不管这个js方法有没有被调用这段java代码都会被执行? 因为在解析时最新解析的就是JA ...

  3. 在js文件里调用另一个js文件里的函数

    这个是我今天解决的一个小问题,我在创建界面的时候,根据不同的界面需求对应创建了不同的js文件来搭建界面,搭建完毕之后再将各个生成页面的函数汇总到主界面上,通过visibility属性切换显示,这时候出 ...

  4. js调用php和php调用js的方法举例

    js调用php和php调用js的方法举例1 JS方式调用PHP文件并取得php中的值 举一个简单的例子来说明: 如在页面a.html中用下面这句调用: <script type="te ...

  5. JS调用PHP 和 PHP调用JS的方法举例

    http://my.oschina.net/jiangchike/blog/220988 1.JS方式调用PHP文件并取得PHP中的值举一个简单的例子来说明:如在页面test_json1中用下面这句调 ...

  6. Js文件中调用其它Js函数的方法

    在项目开发过程中,也许你会遇这样的情况.在某一Js文件中需要完成某一功能,但这一功能的大部分代码在另外一个Js文件中已经完成了,自己只需要调用这个方法再加上几句代码就可以实现所需的功能.我们知道,在h ...

  7. 【Angular JS】正确调用JQuery与Angular JS脚本 - 修复Warning: Tired to load angular more than once

    自己正在做一个小网站,使用Angular JS + Express JS + Mongo DB,在开发过程中,遇到一些问题,所以整理出来.希望对大家都有帮助. 这是今天解决的一个问题,Angular ...

  8. js调用后台,后台调用前台等方法总结

    1. javaScript函数中执行C#代码中的函数:方法一:1.首先建立一个按钮,在后台将调用或处理的内容写入button_click中;        2.在前台写一个js函数,内容为docume ...

  9. Atitit java c# php c++ js跨语言调用matlab实现边缘检测等功能attilax总结

    Atitit java c# php c++ js跨语言调用matlab实现边缘检测等功能attilax总结 1.1. 边缘检测的基本方法Canny最常用了1 1.2. 编写matlab边缘检测代码, ...

随机推荐

  1. PostgreSQL 修改列报错:cannot be cast automatically to type integer

    如果你直接使用可视化工具修改一个varchar字段为int类型的时候,可能会报错, 这里就需要自己去写一个语句去修改了 调整执行语句:ALTER TABLE table_name ALTER COLU ...

  2. uploadify上传控件使用

    uploadify是JQuery的一个上传插件,实现的效果非常不错,并且带进度显示,我将给大家演示如何使用uploadify进行图片上传, 1.点我下载http://www.uploadify.com ...

  3. UItextField常用方法

    - (void)viewDidLoad {     [super viewDidLoad];     // Do any additional setup after loading the view ...

  4. How to change Visual Studio default environment setting

    如何改变 Visual Studio 的默认环境设置: 1. 工具栏 Tools --> Import and Export Settings... 2. 选择 Reset All Settin ...

  5. UIView-图层方法

    // // ViewController.m // UIView-图层概念 // // Created by wangtouwang on 15/5/5. // Copyright (c) 2015年 ...

  6. java编程思想,对象导论

    程序设计的本质就是使用编程语言解决某一类具体问题.对问题的定义叫建模,例如定义问题域中的各种名词,动作,结果等.针对具体的问题提出的解决方案叫算法. 面向对象程序设计的挑战之一,就是在问题空间的元素和 ...

  7. shipyard docker 管理平台

    终于把shipyard弄好了. 我也是根据shipyard的官方文档,做的.在刚开始的时候觉得好难,也遇到了困难,查看了好多文档 但做完之后发现,只需要几步就能简单的配置成功,就能运行了. 修改tcp ...

  8. Docker之配置Centos_ssh

    写Dockerfile配置文件 #DockerfileFROM centos:6  #以下命令用在什么镜像中MAINTAINER cuizhipeng <cuizhipeng@126.com&g ...

  9. Python编写的Linux网络设置脚本,Debian Wheezy上测试通过

    hon编写的Linux网络设置脚本,Debian Wheezy上测试通过       阿里百川梦想创业大赛,500万创投寻找最赞的APP 技术细节参见Linux网络设置高级指南 注意事项参见程序注释 ...

  10. Delphi实用小function

    Write Log // strLog : the log content need write to log file; // strFile: the compliete file path(in ...