zoukankan      html  css  js  c++  java
  • winform与JS交互

    摘自:http://blog.csdn.net/kkkkkxiaofei/article/details/8663377

    winform中执行JS代码使用 webBrowser1.Document.InvokeScript("方法名称");

    在JS中执行winform的方法需要引用 using System.Security.Permissions

    并且在在 命名空间中添加:

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]

     [System.Runtime.InteropServices.ComVisibleAttribute(true)]

    html的内容为:

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4 <style type="text/css">
     5 body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;}
     6 #l-map{height:100%;width:78%;float:left;border-right:2px solid #bcbcbc;}
     7 #r-result{height:100%;width:20%;float:left;}
     8 </style>
     9 <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=密匙"></script>
    10 <title></title>
    11 </head>
    12     <body>
    13         <input id="mouselng" type ="hidden" />
    14         <input id="mouselat"  type = "hidden"/>
    15         
    16         <div id="allmap"></div>
    17     </body>
    18 </html>
    19 <script type="text/javascript">
    20     var map = new BMap.Map("allmap");
    21     var marker_num = 0;
    22 function loadScript() {   
    23     map.centerAndZoom("重庆", 12);
    24     map.enableScrollWheelZoom(true);
    25 }
    26 function PUTANDSEND() {
    27     map.addEventListener("rightclick", putAndsend);
    28 }
    29 function putAndsend(e) {
    30     //放标注
    31     var p1 = new BMap.Point(e.point.lng, e.point.lat);
    32     var marker = new BMap.Marker(p1); //将标注的图标改为小汽车
    33     map.addOverlay(marker);
    34     marker_num++; //标注索引,这个是个全局变量
    35     var whichCar = window.external.setWhichCar();
    36     var label = new BMap.Label(whichCar + "号车-坐标" + marker_num + ":" +
    37 "(" + e.point.lng + "," + e.point.lat + ")", { offset: new BMap.Size(20, -10) });
    38     marker.setLabel(label);
    39     //给WINFORM传值
    40     window.external.PutIntotextBox(marker_num, whichCar, e.point.lng, e.point.lat);
    41 
    42 }
    43 window.onload = loadScript;
    44 </script>

    winform中cs的内容为:

     1 using System;
     2 using System.Windows.Forms;
     3 using System.Security.Permissions;
     4 
     5 namespace TestJS
     6 {
     7     [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
     8 
     9     [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    10     public partial class Form1 : Form
    11     {
    12         public Form1()
    13         {
    14             InitializeComponent();
    15         }
    16 
    17         private void Form1_Load(object sender, EventArgs e)
    18         {
    19              string path = Application.StartupPath + @"\1.html";
    20 
    21              path = path.Replace("\\", "/");
    22              path = "file://" + path;
    23             webBrowser1.Url = new Uri(path);
    24             webBrowser1.Document.InvokeScript("loadScript");
    25             webBrowser1.ObjectForScripting = this;
    26         }
    27         private void btnPutMarker_Click(object sender, EventArgs e)
    28         {
    29             webBrowser1.Document.InvokeScript("PUTANDSEND");
    30         }
    31         //得到Radiobutton的值
    32         public string setWhichCar()
    33         {
    34             if (radioButton1.Checked)
    35                 return "1";
    36             if (radioButton2.Checked)
    37                 return "2";
    38             if (radioButton3.Checked)
    39                 return "3";
    40             return "Erro";
    41         }
    42         public void PutIntotextBox(object markerIndex, object carNumber, object JSlng, object JSlat)
    43         {
    44             text_index.Text = markerIndex.ToString();
    45             text_num.Text = (string)carNumber;
    46             text_lng.Text = JSlng.ToString();
    47             text_lat.Text = JSlat.ToString();
    48             string sql = "insert into 汽车轨迹数据 values ('" + text_num.Text + "','" + text_index.Text + "','" + text_lng.Text + "','" + text_lat.Text + "','" + DateTime.Now.ToString() + "')";
    49             //MessageBox.Show(sql);
    50 
    51         }
    52     }
    53 }

    执行结果如图所示

     

  • 相关阅读:
    Excel透视表进阶之计算字段、计算项、切片器、页面布局
    Excel透视表进阶之排序、筛选、分组、总计与分类汇总
    Excel透视表基础之字段布局与重命名、更新、数字格式设置、空值与错误值、
    Excel透视表基础之数据源、创建、基本术语、基本操作
    NumPy基础
    Python读写Excel
    逻辑判断
    打印 PRINT
    面向对象
    循环控制
  • 原文地址:https://www.cnblogs.com/nygfcn1234/p/3040275.html
Copyright © 2011-2022 走看看