如何以 c#.net windows 形式创建 activex 控件以及如何部署

How to create activex control in c#.net windows form and how to deploy

我创建了一个 activeX 控件,但在 Internet Explorer 中部署它时遇到问题。浏览器 (IE 11) 无法下载 activeX 控件。我不确定哪里出了问题或哪段代码可能无法正常工作。我正在使用 .net 2010,框架 4.0

这是我试过的代码。

[ProgId("Newcomp.UserControl1")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("5FE8E181-7D6D-4CE2-AB83-BAEB9906EF48")]
[ComVisible(true)]
public partial class UserControl1: UserControl
{

    public UserControl1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.BackColor = Color.YellowGreen;
    }
    /// 
    /// Register the class as a control and set it's CodeBase entry
    /// 
    /// The registry key of the control
    [ComRegisterFunction()]
    public static void RegisterClass(string key)
    {
        // Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need it
        StringBuilder sb = new StringBuilder;
        sb.Replace(@"HKEY_CLASSES_ROOT\", "");

        // Open the CLSID\{guid} key for write access
        RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

        // And create the 'Control' key - this allows it to show up in
        // the ActiveX control container
        RegistryKey ctrl = k.CreateSubKey("Control");
        ctrl.Close();

        // Next create the CodeBase entry - needed if not string named and GACced.
        RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
        inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
        inprocServer32.Close();

        // Finally close the main key
        k.Close();

    }

    /// 
    /// Called to unregister the control
    /// 
    /// Tke registry key
    [ComUnregisterFunction()]
    public static void UnregisterClass(string key)
    {
        StringBuilder sb = new StringBuilder;
        sb.Replace(@"HKEY_CLASSES_ROOT\", "");

        // Open HKCR\CLSID\{guid} for write access
        RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

        // Delete the 'Control' key, but don't throw an exception if it does not exist
        k.DeleteSubKey("Control", false);

        // Next open up InprocServer32
        RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);

        // And delete the CodeBase key, again not throwing if missing
        k.DeleteSubKey("CodeBase", false);

        // Finally close the main key
        k.Close();
    }
}

我也关注了this

您遇到的问题是由于默认情况下,IE10(及更高版本)不允许您使用 download/run Active X 控件,在默认情况下配置。这样做是出于安全考虑。换句话说,ActiveX 控制在哪里被恶意使用。

不幸的是,您使用的“操作方法”文章(虽然准确):https://blogs.msdn.microsoft.com/asiatech/2011/12/05/how-to-develop-and-deploy-activex-control-in-c/, 早于此默认限制,因此忽略了警告用户。

Microsoft 提供了一种在 IE10IE11 中禁用 AciveX 过滤的方法: https://support.microsoft.com/en-us/help/17469/windows-internet-explorer-use-activex-controls

但是,此解决方案需要最终用户采取行动,程序员无法强制执行此选项,只能向最终用户提出建议。这适用于受众较少的 Intranet 应用程序,但对于更广泛的受众来说变得难以管理。