【问题标题】:I am using ActiveX control and installing it by msi. The method written in ActiveX user control class are firing but UI is not visible我正在使用 ActiveX 控件并通过 msi 安装它。在 ActiveX 用户控件类中编写的方法正在触发但 UI 不可见
【发布时间】:2015-06-08 06:10:45
【问题描述】:
<object name="Form" id='Form' classid='2F76566A-964F-4547-BD48-EE498AE1A7A2' 
       codebase='ActiveXControl.cab#version=1,0,0,0'
       width="500px" height="500px" style="background-color:Blue">
</object>

<script type="text/javascript" language="javascript">
    var x = new ActiveXObject("ActiveXControl.ControlClass");
    x.UserTxt = "Aashish";
    x.password = "Rockstar";
    x.getmethod();
    alert(x.Data());
</script>

我在 Htm 文件中使用了对象标签,并为我的代码提供了类 ID 和代码库。我的方法 Data() 调用成功,但我的 ActiveX 控件的视图不可见。我不想使用 Caspol.exe 来修复我的查询

【问题讨论】:

  • 您确定您的 ControlClass 在调用其 Data 方法后尝试打开表单吗?
  • 您创建的唯一可见对象是“表单”。 javascript 代码创建了一个单独的“ControlClass”实例,它不是表单的子控件,因此不可见。你必须重新考虑这一点。
  • 不,我的想法是,当创建 ActiveXObject 时,它应该调用我的 ControlClass 的构造函数,在该构造函数中,窗体正在初始化,并且该对象进一步调用此类中存在的方法。

标签: c# cas activexobject caspol


【解决方案1】:
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 ActiveXControl
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    [Guid("2F76566A-964F-4547-BD48-EE498AE1A7A2")]
    [ProgId("ActiveXControl.ControlClass")]
    [ComDefaultInterface(typeof(Intermediate))]
    public partial class ControlClass : UserControl, Intermediate, IObjectSafety
    {
        public ControlClass()
        {
            InitializeComponent();
        }
        public string UserTxt { get; set; }
        public string password { get; set; }

        public void getmethod()
        {
            textBox1.Text = UserTxt;
            textBox2.Text = password;
        }
        public string Data()
        {
            return textBox1.Text +" " + textBox2.Text;
        }
        public enum ObjectSafetyOptions
        {
            INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001,
            INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002,
            INTERFACE_USES_DISPEX = 0x00000004,
            INTERFACE_USES_SECURITY_MANAGER = 0x00000008
        };

        public int GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions)
        {
            ObjectSafetyOptions m_options = ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_CALLER | ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_DATA;
            pdwSupportedOptions = (int)m_options;
            pdwEnabledOptions = (int)m_options;
            return 0;
        }

        public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
        {
            return 0;
        }
    }
}

这段代码写在我的 ActiveX 类中,可以清楚地看到构造函数正在调用方法来初始化表单对象。

【讨论】:

    猜你喜欢
    • 2012-03-05
    • 2011-11-20
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多