我利用很简单的办法创建了类似于ActiveX的控件,步骤如下:
1. 创建Windows Control Libary,实现控件的属性、方法和事件。并在C#的Windows Application中测试该组件,一切正常。
2. 通过工具Creat Guid,创建GUID,设置到类的GUID属性。设置项目属性。Make assembly COM-Visible为True,Register for COM Interop为True。
3. 实现IObjectSafety接口,在Assembly.cs文件中添加[assembly: AllowPartiallyTrustedCallers()] 
4. 通过在HTML文件中用<object id="MyAx" name="MyAx" classid="clsid:15C969FD-5CA4-494D-AF9B-A0C9CCE576C0">的方式,ActiveX控件可以正常显示。利用js调用ActiveX控件的属性和方法一切正常。但是无法响应ActiveX触发的事件。跟踪发现,ActiveX控件中的点燃函数无法执行。html中的事件响应代码如下:
<script language='javascript' for=MyAxevent='OnClickButton()'>
    alert("ss");
</script>ActiveX控件中的事件点燃代码为:
if (ClickEvent != null)
{
    Invoke(ClickEvent);
}
ClickEvent始终为null,如果不进行判断强行执行点燃函数,后果是IE进程失去响应。因此我判断,利用该方式创建的类似ActiveX控件存在某些兼容性问题。无法在IE中触发事件。以上的代码我参考自http://homer.cnblogs.com/archive/2005/01/04/86473.aspx不知道有人遇到过没有。有什么办法可以解决这个问题。多谢了。

解决方案 »

  1.   

    刚才的代码粘贴出现了问题,html中响应事件的代码应该如下:
    <script language='javascript' for='MyAx' event='OnClickButton()'>
        alert("ss");
    </script>
      

  2.   

    学习,不过这样在非.net的client应该不能用吧
      

  3.   

    I pay attention on this thread,I have read your referrence,but  I do not why too.
    can you share you source? thank you.
      

  4.   

    开发工具:VS.NET 2005 pro en
    源程序(1)-- SoftPhone.cs 自定义控件的源程序using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.IO;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace SoftPhoneDotNetControl
    {
        /// <summary>
        /// 可以将Com对象声明为安全的。可以在IE中同js交互时避免弹出安全警告框。这里的GUID不能修改。
        /// </summary>
        [Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IObjectSafety
        {
            // methods 
            void GetInterfacceSafyOptions(
                System.Int32 riid,
                out System.Int32 pdwSupportedOptions,
                out System.Int32 pdwEnabledOptions);
            void SetInterfaceSafetyOptions(
                System.Int32 riid,
                System.Int32 dwOptionsSetMask,
                System.Int32 dwEnabledOptions);
        }    public delegate void ClickHandler();    [Guid("15C969FD-5CA4-494d-AF9B-A0C9CCE576C0")]
        public partial class SoftPhone : UserControl, IObjectSafety
        {
            private ClickHandler ClickEvent;
            public SoftPhone()
            {
                InitializeComponent();
            }        [
            Category("SoftphoneControls"),
            Description("")
            ]
            public event ClickHandler OnClickButton
            {
                add
                {
                    ClickEvent += value;
                }
                remove
                {
                    ClickEvent -= value;
                }
            }        [
            Category("SoftphoneControls"),
            Description("设置按钮控件的文本内容")
            ]
            public string ButtonCaption
            {
                get
                {
                    return this.btn_SignIn.Text;
                }
                set
                {
                    this.btn_SignIn.Text = value;
                }
            }        private void btn_SignIn_Click(object sender, EventArgs e)
            {
                // 由于安全限制,如果是.NET组件发布,该代码无法在Web中使用。
                // 如果是按照Com组件发布,该代码可以执行
                StreamWriter bplStreamWriter = new StreamWriter("c:\\a.txt");
                bplStreamWriter.WriteLine("Inserted Text by .Net WinFormControl.");
                bplStreamWriter.Close();            if (ClickEvent != null)
                {
                    Invoke(ClickEvent);
                }
            }
            public void ShowMessage(String Msg)
            {
                System.Windows.Forms.MessageBox.Show(Msg);
            }        #region IObjectSafety 成员
            public void GetInterfacceSafyOptions(Int32 riid, out Int32 pdwSupportedOptions, out Int32 pdwEnabledOptions)
            {
                // TODO:  添加 WebCamControl.GetInterfacceSafyOptions 实现 
                pdwSupportedOptions = 1;
                pdwEnabledOptions = 2;
            }        public void SetInterfaceSafetyOptions(Int32 riid, Int32 dwOptionsSetMask, Int32 dwEnabledOptions)
            {
                // TODO:  添加 WebCamControl.SetInterfaceSafetyOptions 实现             
            }
            #endregion 
        }
    }
      

  5.   

    http://homer.cnblogs.com/archive/2005/01/04/86473.aspx
      

  6.   

    静态的HTML测试文件。
    <HTML>
    <HEAD>
      <meta http-equiv="Content-Type" content="text/html; charset=GBK">
    </HEAD>
    <script language='javascript' for='Softphone' event='OnClickButton()'>
    alert("ss");
      window.defaultStatus = "OnClickButton()";
    </script><body> 
    <object id="Softphone" name="Softphone" classid="clsid:15C969FD-5CA4-494D-AF9B-A0C9CCE576C0">
    </object>
    <br><br><br><br>
    <input type='button' value=' 确定 ' onclick='Softphone.ShowMessage("Hello World!");'></HTML>
      

  7.   

    http://www.youren.com/Article/programme/aspx/ocx/200503/3347.html
      

  8.   

    to lovevsnet(编程一把手) 
    这篇文章我看过了,但没有介绍ActiveX事件的。
      

  9.   

    你看看下面的链接,或许对你有用,我搜索了一天,翻了不知多少页才找到这个,我E文不太行,你自己好好看看
    http://community.sgdotnet.org/forums/15856/ShowPost.aspx
    http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=388
    看懂后请翻译一下吧
      

  10.   

    经过多种方式的尝试,特别是借鉴了http://community.sgdotnet.org/forums/15856/ShowPost.aspx,我找到了解决办法。目前已经可以将WinForm的.NET控件转换为在IE中使用的ActiveX,并与js进行互动,包括属性、方法和事件。出去需要客户端要安装FrameWork外,应该同其他语言写的ActiveX没有差别。感谢大家的热情帮助。