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

时间:2022-02-05 06:42:10

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

}