0

COM-visibleを使用してC#でActiveXを作成しようとしています。これはWindowsフォームです。dll ActiveXとして正常にビルドし、それを呼び出すためのVBScriptコードを記述しました。からが現れたが、その直後に消えた。なぜ@@なのかわかりません。私のコードは次のとおりです。

C#コード

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Reflection;

namespace ActiveXTestLibrary
{
    [ProgId("ActiveXTestLibrary.UserControl")]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    [ComVisible(true)] 
    public partial class UserControl1 : Form
    {
        public UserControl1()
        {
            InitializeComponent();
        }


        [ComVisible(true)]
        public void Hello()
        {
            this.Show();
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            this.lblResult.Text = "I am a .NET user control happily living \ninside an ActiveX container. Cheers.";
        }

        [ComRegisterFunction()]
        public static void RegisterClass(string key)
        {
            StringBuilder sb = new StringBuilder(key);
            sb.Replace(@"HKEY_CLASSES_ROOT\", "");

            RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

            RegistryKey ctrl = k.CreateSubKey("Control");
            ctrl.Close();

            RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
            inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
            inprocServer32.Close();

            k.Close();
        }

        [ComUnregisterFunction()]
        public static void UnregisterClass(string key)
        {
            StringBuilder sb = new StringBuilder(key);
            sb.Replace(@"HKEY_CLASSES_ROOT\", "");

            RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

            if (k == null)
            {
                return;
            }
            k.DeleteSubKey("Control", false);

            RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);

            inprocServer32.DeleteSubKey("CodeBase", false);

            inprocServer32.Close();
        }
    }
}

およびVBScript:

Sub main
    set objTest = CreateObject("ActiveXTestLibrary.UserControl")
    objTest.Hello
end sub

call main
4

1 に答える 1

0

フォームを表示するには、 Application.Runを呼び出してメッセージループを開始する必要があります。

[ComVisible(true)]
public void Hello()
{
    Applicaiton.Run(this);
}

Show()関数はフォームを表示し、すぐに終了します。 Application.Run()フォームが閉じられるまで終了しません。

于 2012-06-08T05:00:54.370 に答える