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

    }

  • 相关阅读:
    学习总结(二十六)
    学习总结(二十五)
    在知乎学习怎么参加工作
    连分数系列
    Kalman Filter
    五子棋的学习
    Dijkstra
    三等分角、化圆为方、倍立方体
    女朋友走丢数学模型
    传染病模型
  • 原文地址:https://www.cnblogs.com/wwwfj/p/3673774.html
Copyright © 2020-2023  润新知