如何使在JavaScript中C#引发事件写了一个ActiveX控件,点击时? [英] How can I make an ActiveX control written with C# raise events in JavaScript when clicked?

查看:164
本文介绍了如何使在JavaScript中C#引发事件写了一个ActiveX控件,点击时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到与此相关的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).

的什么,我希望实现的最终目标是没有什么不同,如果它是一个<跨度> 标记,它有一个的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.

大部分的<一个href=\"http://stackoverflow.com/questions/150814/how-to-handle-an-activex-event-in-javascript/379872#379872\">stuff我读过它进入上如何处理ActiveX控件的事件和发送信息回/来回非常精细的细节,这很好,但似乎过于复杂。我不是在寻找与ActiveX控件进行沟通,我只是需要一个JavaScript函数来火了,当我点击它,在类似于℃的途径;跨度&GT; &LT; D​​IV&GT; 标记。我可以处理一切其他的JavaScript。简单地包裹在&LT控制;跨度&GT; &LT; D​​IV&GT; 的onclick 事件有没有影响 - ActiveX控件pretty多重写它

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#?

我猜把它的另一种方式 - 我与第三方控制工作,我们必须使用类似下面的code让它通过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函数的名称,它会火我的code,传递一个首页参数。然而,什么都做我必须做的设置在控制如 OnMouseClick 的方法?如果我不想传递任何实际信息(即无首页)我连这样走多远?

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事件,您需要将其标记为将它传递给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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆