問題描述
我已經在 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 中引發事件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!