单击时,如何使用 C# 编写的 ActiveX 控件在 JavaS

How can I make an ActiveX control written with C# raise events in JavaScript when clicked?(单击时,如何使用 C# 编写的 ActiveX 控件在 JavaScript 中引发事件?)
本文介绍了单击时,如何使用 C# 编写的 ActiveX 控件在 JavaScript 中引发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 SO 上看到了一些与此相关的问题,但我认为我的问题完全不同,不会被视为重复(如果我错了,请告诉我).

I'm seeing a few questions related to this on SO already, but I think mine is sufficiently different to not be considered a duplicate (if I'm wrong let me know).

我有一个用 C# 编写的 ActiveX 控件,虽然我大部分时间都在工作,但我想在单击它时在 JavaScript 中引发一个事件(它显示一个图像,因此它是页面上的一个可视元素).

I have an ActiveX control I've written in C# and while I have it mostly working, I want to raise an event in JavaScript when it's clicked (it displays an image so it's a visual element on the page).

我想要完成的最终目标与如果它是一个 <span> 标记并且它有一个 onclick 事件来引发一个单击标记区域时的 JavaScript 函数.

The end goal of what I'm looking to accomplish is no different than if it were a <span> tag and it had an onclick event to raise a JavaScript function when the area of the tag were clicked.

我读过的大部分内容 上详细介绍了如何处理 ActiveX 控件中的事件和来回发送信息,这很好,但似乎过于复杂.我不想与 ActiveX 控件进行通信,我只需要一个 JavaScript 函数在我单击它时触发它,其方式类似于 <span><div> 标签.我可以在 JavaScript 中处理所有其他事情.简单地将控件包装在 <span><div>onclick 事件没有任何效果 - ActiveX 控件几乎覆盖它.

Most of the stuff I've read on it goes into very fine detail on how to handle events in an ActiveX control and send info back/forth, and that's fine, but it seems overly complicated. I'm not looking to communicate with the ActiveX control, I just need a JavaScript function to fire off when I click it, in a way similar to a <span> or <div> tag. I can handle everything else in JavaScript. Simply wrapping the control in a <span> or <div> with an onclick event has no effect - the ActiveX control pretty much overrides it.

对于用 C# 编写的 ActiveX 控件,是否有一种简单的方法来处理这个问题?

Is there a simple way to handle this for an ActiveX control written in C#?

我想另一种说法是 - 我正在使用第三方控件,我们必须使用类似于以下的代码来让它通过 JavaScript 与我们的 HTML 页面进行通信

I guess another way of putting it is - I'm working with a third party control and we have to use code similar to the following to get it to communicate with our HTML page via JavaScript

<script type="text/javascript" event="OnMouseClick(index)" for="AXObjectName"> 
        <!--
         AXObjectName_OnMouseClick(index);
        //-->
</script>

其中 AXObjectName 是控件的名称/ID,AXObjectName_OnMouseClick 是它将在我的代码中触发的 JavaScript 函数的名称,传递一个 index 参数.但是,要在控件中设置像 OnMouseClick 这样的方法,我需要做些什么?如果我不想传递任何实际信息(即没有 index),我什至必须走这么远吗?

Where AXObjectName is the name/id of the control and AXObjectName_OnMouseClick is the name of the JavaScript function it will fire in my code, passing an index parameter. However, what all do I have to do to set up a method like OnMouseClick in the control? And if I don't want to pass any actual information (i.e., no index) do I even have to go this far?

推荐答案

ActiveX 事件是通过 COM 处理的,所以你需要深入研究一下不幸的是.

ActiveX events are handled via COM, so you need to delve in there a bit unfortunately.

使用 COM 要记住的是,一切都是通过接口处理的,因此您通常需要创建两个接口,一个用于任何属性,一个用于您的事件.

The thing to remember with COM is that everything is handled via interfaces, so you normally need to create two interfaces, one for any properties and one for your events.

事件的关键是使用 ComSourceInterfaces 属性标记您的类,MSDN 将其描述为标识作为属性类的 COM 事件源公开的接口列表."

The key for events is marking your class with the ComSourceInterfaces attribute, which is described by MSDN as "Identifies a list of interfaces that are exposed as COM event sources for the attributed class."

这个简单的类结构应该适合你(过去也适合我).

This simple class structure should work for you (it has for me in the past).

namespace MyActiveX
{
    [Guid("Your-GUID") ,InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IActiveXEvents
    {
        [DispId(1)]
        void OnMouseClick(int index);
    }

    [Guid("Another-GUID"),InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IActiveX
    {       
        //[DispId(1)]
        // Any properties you want here like this
        // string aProperty { get; set; }        
    }

    [ComVisible(true)]
    [Guid("Yet-Another-GUID"), ClassInterface(ClassInterfaceType.None)]
    [ProgId("MyActiveX")]
    [ComSourceInterfaces(typeof(IActiveXEvents))]
    public partial class MyActiveX : UserControl, IActiveX
    {
        public delegate void OnMouseClickHandler(int index);

        public event OnMouseClickHandler OnMouseClick;

        // Dummy Method to use when firing the event
        private void MyActiveX_nMouseClick(int index)
        {

        }

        public MyActiveX()
        {
            InitializeComponent();

            // Bind event
            this.OnMouseClick = new OnMouseClickHandler(this.MyActiveX_MouseClick)
        }

        public void FireTheEvent()
        {
            int index = -1;
            this.OnMouseClick(index);
        }       
    }
}

如果您不需要任何属性,您可以排除 IActiveX 接口.此外,如果您要使用 Click 事件,则需要将其标记为 new 以将其传递给 COM.

If you don't need any properties you can just exclude the IActiveX interface. Also if you are going to use the Click event, you will need to mark it as new to pass it to COM.

这篇关于单击时,如何使用 C# 编写的 ActiveX 控件在 JavaScript 中引发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)