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

    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('网页的统计结果: 链接数:' + numLinks + ";
    string scriptline07 = @" ' 表单数:' + numForms + ";
    string scriptline08 = @" ' 图像数:' + numImages + ";
    string scriptline09 = @" ' 脚本数:' + 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();
    }
    }

    }

  • 相关阅读:
    sql help
    asp.net学习之Repeater控件
    Log4Net使用指南
    43个PSD to XHTML,CSS教程
    .NET(C#)基于Socket编程实现平行主机之间网络通讯有图片传输的Demo演示
    .NET 4.0新增命名空间:System.Collections.Concurrent
    错误日志记录类
    Microsoft ScriptControl 控件使用指南
    用dynamic增强C#泛型表达力
    xPath介绍
  • 原文地址:https://www.cnblogs.com/wwwfj/p/3673774.html
Copyright © 2011-2022 走看看