C#BS通过ActiveX控件及制作CAB包来调CS

1) 创建Visual Studio解决方案 "ActiveXSolution"。
2)在解决方案ActiveXSolution中添加 Windows窗体应用程序 “CSDemo”。
1. 在Windows窗体Form1添加代码如下:

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 CSDemo
{
    public partial class Form1 : Form
    {
        //返回给web的值
        public string dialogResult = string.Empty;
        //获取从web中传过来的值
        private string caption = string.Empty;
        private string text = string.Empty;
        public Form1()
        {
            InitializeComponent();
        }
        public Form1(string caption, string text)
        {
            InitializeComponent();
            this.caption = caption;
            this.text = text;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Button btnTest = new Button();
            btnTest.Text = "测试";
            btnTest.Location = new Point(10, 10);
            btnTest.Click+=new EventHandler(btnTest_Click);
            this.Controls.Add(btnTest);
        }
        /// <summary>
        /// 显示从web传来的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTest_Click(object sender, EventArgs e)
        {
            dialogResult = MessageBox.Show(text,
                                                              caption,
                                                              MessageBoxButtons.OKCancel,
                                                              MessageBoxIcon.Information,
                                                              MessageBoxDefaultButton.Button1).ToString();
            
            this.Close();
        }
    }
}

3) 在解决方案ActiveXSolution中添加 Windows窗体控件库“ActiveXDemo”。
1. 在“ActiveXDemo”中添加接口“IObjectSafety.cs”代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ActiveXDemo
{
    [ComImport, Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IObjectSafety
    {
        [PreserveSig]
        void GetInterfacceSafyOptions(
            int riid,
            out int pdwSupportedOptions,
            out int pdwEnabledOptions);
        [PreserveSig]
        void SetInterfaceSafetyOptions(
            int riid,
            int dwOptionsSetMask,
            int dwEnabledOptions);
    }
}

2. 在“ActiveXDemo”中添加 用户控件“UserControl1.cs”代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ActiveXDemo
{
    [Guid("413f087e-95b4-4eb4-9941-da0610f71fef")]
    public partial class UserControl1 : UserControl, IObjectSafety
    {
        public UserControl1()
        {
            InitializeComponent();
        }
        public void GetInterfacceSafyOptions(int riid, out int pdwSupportedOptions, out int pdwEnabledOptions)
        {
            pdwSupportedOptions = 1;
            pdwEnabledOptions = 2;
        }
        public void SetInterfaceSafetyOptions(int riid, int dwOptionsSetMask, int dwEnabledOptions)
        {
            throw new NotImplementedException();
        }
        public string ShowForm(string caption, string text)
        {
            string dialogResult = string.Empty;
            //将web传过来的值传给winform
            CSDemo.Form1 frm = new CSDemo.Form1(caption, text);
            frm.ShowDialog();
            //将winform传过来的值传给web
            dialogResult = frm.dialogResult;
            return dialogResult;
        }
    }
}

3.右击“ActiveXDemo”—属性—生成—勾上为COM互操作注册。
4.打开“ActiveXDemo”—Properties—AssemblyInfo.cs将[assembly: ComVisible(false)]改成[assembly: ComVisible(true)]


4)在解决方案ActiveXSolution中创建安装项目“SetupDemo”
1. 右击安装项目“SetupDemo”—添加—项目输出—主输出“ActiveXDemo”
2. 安装“SetupDemo”
3. 单击:开始—所有程序—Microsoft Windows SDK v6.0A—Tools—OLE-COM Object Viewer,打开.NET Category可以看到ActiveXDemo.UserControl1

5)在解决方案ActiveXSolution中添加 ASP.NET Web应用程序 “BSDemo”

1.Default.aspx代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="BSDemo._Default" %>
<!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 runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        function fun(caption, text) {
            //将参数传给winform,并获取winform返回的值
            var dialogResult = document.getElementById('controlbyid').ShowForm(caption, text);
            alert(dialogResult);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <object id="controlbyid" classid="clsid:413f087e-95b4-4eb4-9941-da0610f71fef" codebase="Resource/SetupDemo.cab"></object>
        <asp:Button ID="btnTest" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>

2.Default.aspx.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace BSDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string caption = "a1";
                string text = "b1";
                this.btnTest.Attributes.Add("onclick", "fun('"+caption+"','"+text+"');");
            }
        }
    }
}

6)生成SetupDemo.cab
1.网上下载cabsdk.exe
2.解压csbsdk.exe,将SetupDemo.msi放到解压后文件的BIN下面。
3.在BIN中创建install.inf
代码如下:
[version]
signature="$CHICAGO$"
AdvancedINF=2.0
[Setup Hooks]
hook1=hook1  
[hook1]
run=msiexec.exe /i "%EXTRACT_DIR%\SetupDemo.msi" /qn
4.开始-运行,输入“CMD”,在打开的命令提示符界面中,进入解压后文件的BIN目录,
输入:cabarc n SetupDemo.cab SetupDemo.msi install.inf
5.cab生成成功,将SetupDemo.cab放到“BSDemo”下面的Resource文件夹下。
6.启动网站,一般情况下,网页会弹出提示,是否安装ActiveX控件等,如果提示了也不能安装,需要将网站添加到信任站点,并允许下载不安全和未签名控件。关于控件的签名和认证,本文不做说明,需要的请自行搜索。

7)用VS创建证书。

1.开始—所有程序—Microsoft Visual Studio 2008—Visual Studio Tools—Visual Studio 2008 命令提示。
2.makecert -r -pe -n "CN=zhcao-demo" -ss My -sky exchange
3.查看证书:
打开ie—工具—Internet选项—内容—证书—我们将在个人中看到我们刚才创建的证书“zhcao-demo”。

8)给cab包添加证书
1.开始—所有程序—Microsoft Visual Studio 2008—Visual Studio Tools—Visual Studio 2008 命令提。
2.signtool signwizard—下一步—浏览获取我们前面创建的SetupDemo.cab—下一步—典型—从存储区选择—选择我们前面创建的证书“zhcao-demo”—确定—下一步—下一步—下一步—完成。

 

 

你可能感兴趣的:(Web,C#,ActiveX,WinForm)