c# vs2010 创建一个active控件(2)--通过js与web交互
将上一章做好的控件,添加些内容。
效果图附上,点击开始按钮,先弹出一个对话框(failed),然后又弹出一个对话框。----实现将winform控件里的事件传递到web页面。
-----------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
控件全部代码如下:
[Guid("18824A0C-B7B1-464D-8F81-80DA145836DF")] [ClassInterface(ClassInterfaceType.None), ComSourceInterfaces(typeof(ControlEvents))] public partial class ActiveXControl : UserControl, IObjectSafety1 { event ErrorEventHandler ErrorEvent; private static WindowsMediaPlayer Player; public ActiveXControl() { InitializeComponent(); } private void ActiveXControl_Load(object sender, EventArgs e) { } void Player_MediaError(object pMediaObject) { //display a message MessageBox.Show("Failed"); // notify all subscribers including IE sinks if (ErrorEvent != null) { ErrorEvent(new ErrObject("Invalid file name")); } } private void button1_Click(object sender, EventArgs e) { Player = new WMPLib.WindowsMediaPlayer(); Player.MediaError += new WMPLib._WMPOCXEvents_MediaErrorEventHandler(Player_MediaError); Player.URL = "invalidFile.wma"; Player.controls.play(); Player.uiMode = "mini"; } #region IObjectSafety 成员 private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}"; private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}"; private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}"; private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}"; private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}"; private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001; private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002; private const int S_OK = 0; private const int E_FAIL = unchecked((int)0x80004005); private const int E_NOINTERFACE = unchecked((int)0x80004002); private bool _fSafeForScripting = true; private bool _fSafeForInitializing = true; public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions) { int Rslt = E_FAIL; string strGUID = riid.ToString("B"); pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA; switch (strGUID) { case _IID_IDispatch: case _IID_IDispatchEx: Rslt = S_OK; pdwEnabledOptions = 0; if (_fSafeForScripting == true) pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER; break; case _IID_IPersistStorage: case _IID_IPersistStream: case _IID_IPersistPropertyBag: Rslt = S_OK; pdwEnabledOptions = 0; if (_fSafeForInitializing == true) pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA; break; default: Rslt = E_NOINTERFACE; break; } return Rslt; } public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions) { int Rslt = E_FAIL; string strGUID = riid.ToString("B"); switch (strGUID) { case _IID_IDispatch: case _IID_IDispatchEx: if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true)) Rslt = S_OK; break; case _IID_IPersistStorage: case _IID_IPersistStream: case _IID_IPersistPropertyBag: if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true)) Rslt = S_OK; break; default: Rslt = E_NOINTERFACE; break; } return Rslt; } #endregion } //this class is used to make error messages available on the client public class ErrObject { private string _msg; public ErrObject(string str) { _msg = str; } //public properties enable javascript to access the internal class members on the client public string Message { get { return _msg; } set { _msg = value; } } } //delegate to handle error condition public delegate void ErrorEventHandler(object e); [GuidAttribute("9819EE05-86A4-43a8-AF62-A7AFFA69AB1B")] [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)] public interface ControlEvents { //Add a DispIdAttribute to any members in the source interface to specify the COM DispId. [DispIdAttribute(0x60020000)] void ErrorEvent(object e); }
在web页面
<div> <object id="myControl1" name="myControl1" classid="clsid:18824A0C-B7B1-464D-8F81-80DA145836DF" width="300" height="150"> </object> <script for="myControl1" event="ErrorEvent(e)" language="javascript"> function myControl1::ErrorEvent(e) { alert('Windows media reports an error: ' + e.Message); } </script> </div>
其中,clsid是为控件生成 的GUID,同时也是控件的注册的clsid。这样可以将activex里面的事件传递给js。
当然反之也可以通过js调用winfrom控件的方法和属性。
在控件UserControl1中有一个方法SayHello如下:
public string SayHello()
{
return "Hello!";
}
在web页面可以直接调用方法如下:
<div class="divBG"> <object id="ActiveX1" name="ActiveX1" classid="clsid:291A5EC0-E87B-4B30-93DE-DCBA44284AD1" width="300" height="150"> </object> <input id="Button1" type="button" onclick="alert(ActiveX1.SayHello());" value="点击事件" /> </div>
当然,也可以获取或者设置activex控件的属性值,在web页面添加param参数。
//js赋值
<script language="javascript" type="text/javascript">
function getvalue() {
var obj = document.getElementById("ActiveX1");
obj.TestValue = "hello";
alert(obj.TestValue);
}
</script>
//body 中添加事件onload="getvalue"
//在body中div中添加如下代码
<object id="ActiveX1" name="ActiveX1" classid="clsid:291A5EC0-E87B-4B30-93DE-DCBA44284AD1" width="300" height="150"> <param name="TestValue" value="" /> </object>
在控件中的事件DownLoadLists_Load中调用属性值,即可获取到。
提示:在IE11和IE8中效果不同,在IE8中可直接将脚本放在ActiveX1的下面执行,即可在控件初始化中赋值。
作者:造梦者2013
出处:http://www.cnblogs.com/lipanpan/
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。