Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

How can I make an ActiveX control written with C# raise events in JavaScript when clicked?

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).

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).

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.

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.

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

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>

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...