• WPF webbowser 交互


    //

    <Window x:Class="WpfApp1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:winform="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
    
    
        xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
    
            <wfi:WindowsFormsHost x:Name="c_wfh" Margin="0,90,0,0">
                <winform:WebBrowser ScriptErrorsSuppressed="True"  x:Name="web">
                </winform:WebBrowser>
            </wfi:WindowsFormsHost>
            <Button Content="Button" HorizontalAlignment="Left" Margin="34,10,0,0" VerticalAlignment="Top" Width="120" Height="75" Click="Button_Click"/>
        </Grid>
    </Window>
    

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    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 WpfApp1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                web.ObjectForScripting = new OprateBasic();
                Loaded += MainWindow_Loaded;
                web.Navigate("https://www.runoob.com/w3cnote/javascript-settimeout-usage.html");
            }
    
            private void MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
                WebbowserHelper.SetWebBrowserFeatures(11);
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
    
                //  web.Document.InvokeScript("eval", new string[] { "function tt(){ window.external.NotifyMsg(new Date()); } ;setTimeout('tt', 3000);" }); 
                // web.Document.InvokeScript("eval", new string[] { "if(document.getElementsByClassName('fff').length>0){window.external.NotifyMsg(66)} else{window.external.NotifyMsg(0)};" });
                //下面这种也可以返回数据
                var i = web.Document.InvokeScript("eval", new string[] { "if(document.getElementsByClassName('fff').length>0){  1;} else{  0;}" });
                Title = "" + i;
    
            }
    
        }
    
        [System.Runtime.InteropServices.ComVisible(true)]
        public class OprateBasic
        {
            public void NotifyMsg(string msg)
            {
                MessageBox.Show(msg);
            }
        }
    }
    

      

  • 相关阅读:
    VBS操作Excel常见方法
    判断文件和文件夹是否存在
    vbs获取自身文件名,不带格式,不带路径
    vbs,修改文件名
    UiPath字符串函数的介绍和使用
    linux各个目录的作用
    Linux下设置memcached访问IP
    python单继承与多继承
    粘包,文件传输
    网络编程
  • 原文地址:https://www.cnblogs.com/wgscd/p/13476609.html
Copyright © 2020-2023  润新知