NET中winform與webform互相通訊執行個體 轉

來源:互聯網
上載者:User
NET中winform與webform互相通訊執行個體2009-12-17    文章來源:    瀏覽次數:186

現在C#越來越多朋友使用了,大家都知道.NET可以開發winform與webform頁面,有時候在開發項目過程中要結合BS+CS這樣來應用,那麼本站做一個執行個體來測試winform與webform互相通訊的執行個體,下面先看下效果:

winform調用bs頁面的js函數

webform頁面發送資訊到winform

好了,看完上面的效果,下面我們看下如何?吧。

第一、開啟VS2008建立winform項目,之後在MainForm拖入瀏覽器控制項,並命令這個瀏覽器控制項名為:WebContainer

下面是全部CS端代碼:

 Code [http://www.xueit.com]
/*
*
* 名稱:CS與BS互相通訊
* 作者:dodo
* 官方:www.xueit.com
*
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestJSWin
{
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
this.WebContainer.ObjectForScripting = this; //這句很關鍵,主要和頁面的JS互相操作 Uri uriSale = new System.Uri("http://localhost:8012/index.htm"); //瀏覽器控制項預設開啟頁面 WebContainer.Url = uriSale;
}

/// <summary>
/// 菜單點擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param> private void jsEventToolStripMenuItem_Click(object sender, EventArgs e)
{
WebContainer.Navigate("javascript:fn_test();void(0);");
}

/// <summary>
/// BS調用方法
/// </summary>
/// <param name="strShow"></param> public void JavascriptCall(string strShow)
{
MessageBox.Show(strShow);
}
}
}

好,做完winform,下面是http://localhost:8012/index.htm頁面的做法。

第2、webform的頁面,源碼很簡單,你可以直接複製原始碼到本地測試就可以了。下面是HTML頁面原始碼:

 Code [http://www.xueit.com]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>
<head>
<title>Test js event</title>
<script language="javascript" type="text/javascript">
<!--
function fn_test() {
alert("Hello, cs調用JS成功-學it網www.xueit.com歡迎你");
}

function fn_call() {
window.external.JavascriptCall("bs發送資訊到winform成功");
}
-->
</script>
</head>
<body>
NET中winform與webform互相通訊執行個體-www.xueit.com(學IT網歡迎你訪問)
<input type="button" value="Call Winform Methed" onclick="fn_call()" />
</body></html>

嗯!到現在為此,所有操作都可以了,非常簡單,如果你有興趣來按上面的代碼來測試下!

作者:dodo
出處:http://www.xueit.com/html/2009-12-11/21-1046653925703.html
本文著作權歸作者和學IT網共有,歡迎轉載,但未經作者同意必須保留此段聲明,並且保留原文連結。否則保留追究法律責任的權利。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.