c# Activex開發之HelloWorld

來源:互聯網
上載者:User

標籤:ice   logs   ring   ext   text   console   一點   雙擊   3.1   

最近需要在Web上使用WinFrom程式,所以要用到Activex技術將WinFrom程式變成外掛程式在Web運行

 

一、建立使用者控制項

1.1 建立使用者控制項項目

 

1.2 在介面上拉一個label,Text賦值為“HelloWorld”

 

1.3 加上guid

using System;using System.Windows.Forms;using System.Runtime.InteropServices;using System.Diagnostics;namespace HelloWorld{    [Guid("ce85468b-7742-4279-b081-2eca51a2afbc")]    public partial class Demo : UserControl, IObjectSafety    {        public Demo()        {            InitializeComponent();        }    }}

  

二. 建立安裝項目

2.1 命名:HelloWorld.Setup

2.2 將HelloWorld.dll加進來

2.3 雙擊HelloWorld.Setup.msi安裝即可

 

 

三、 建立Index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></title>    <script type="text/javascript" src="jquery-1.8.2.min.js"></script></head><body>    <div>        <object id="demo" classid="clsid:ce85468b-7742-4279-b081-2eca51a2afbc" width="300" height="200" />    </div></body></html>

3.1 注意紅色部分,這個clsid即是使用者控制項的guid

3.2 運行Index.html

四、添加自訂方法和按鈕功能

4.1 上邊的功能太簡單,我們稍微深入一點,添加一個方法給js代碼調用,在Demo.cs中加上下邊代碼

        public string SayHello()        {            return "Hello World";        }

4.2 新增一個按鈕,開啟文字框

4.3 按鈕單擊事件

        private void button1_Click(object sender, EventArgs e)        {            Process.Start("notepad.exe");        }

4.4 再重新編譯運行程式,然後Index.html,雙擊按鈕

 

4.5 修改Index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></title>    <script type="text/javascript" src="jquery-1.8.2.min.js"></script>    <script type="text/javascript">        $(document).ready(function () {            $("#btn1").click(function () {                var demo = document.getElementById("demo");                console.log(demo.SayHello());            });        });    </script></head><body>    <div>        <input id="btn1" type="button" value="調用後台SayHello方法" />        <object id="demo" classid="clsid:ce85468b-7742-4279-b081-2eca51a2afbc" width="300" height="200" />    </div></body></html>

4.6 點擊“調用後台SayHello方法”按鈕

 

OK,暫時就到這裡!

 

c# Activex開發之HelloWorld

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.