Create GUI in C# and call in VBScript

為{幸葍}努か 提交于 2019-12-23 04:42:18

问题


I try to make an ActiveX by C# with COM-visible. It is a Windows Form. I built it successfully as a dll ActiveX, then I wrote a VBScript code to call it. The from appeared but disappeared right after that. I don't know why @@ Here is my code:

C# code

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();
        }
    }
}

and VBScript:

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

call main

回答1:


To show a form you need to start a message loop by calling Application.Run:

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

The Show() function will show the form and immediately exit. Application.Run() will not exit until the form is closed.



来源:https://stackoverflow.com/questions/10943253/create-gui-in-c-sharp-and-call-in-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!