`
qingtian881223
  • 浏览: 35237 次
  • 性别: Icon_minigender_1
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

javascript绑定C#事件

阅读更多
C#控件
最终界面




c#代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
namespace WindowsControlLibrary1
{

    public delegate void DelegateClassHandle();



    //用于javascript访问
    public interface IMove
    {
        void say();
    }

    //实现此接口不会弹出不允许访问的框框
    [Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IObjectSafety
    {
        // methods 
        void GetInterfacceSafyOptions(
            System.Int32 riid,
            out System.Int32 pdwSupportedOptions,
            out System.Int32 pdwEnabledOptions);
        void SetInterfaceSafetyOptions(
            System.Int32 riid,
            System.Int32 dwOptionsSetMask,
            System.Int32 dwEnabledOptions);
    }

    [Guid("7BD20046-DF8C-44A6-8F6B-687FAA288A77"),
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IPlayHandler
    {
        [DispId(0x60020000)]
        void PlayGame();//提供给javascript绑定
    } 

   
    //[ClassInterface(ClassInterfaceType.None)]
    [Guid("1EA4DBF0-3C3B-11CF-810C-00AA00389B73")]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]//可以实现javascript调用控件的say方法;
    [ComSourceInterfaces(typeof(IPlayHandler))]//暴露事件给com,让javaxcript可以绑定
    public partial class UserControl1 : UserControl, IObjectSafety, IMove
    {

        public event DelegateClassHandle PlayGame;

        public void GetInterfacceSafyOptions(Int32 riid, out Int32 pdwSupportedOptions, out Int32 pdwEnabledOptions)
        {
            // TODO:  添加 WebCamControl.GetInterfacceSafyOptions 实现 
            pdwSupportedOptions = 1;
            pdwEnabledOptions = 2;
        }

        public void SetInterfaceSafetyOptions(Int32 riid, Int32 dwOptionsSetMask, Int32 dwEnabledOptions)
        {
            // TODO:  添加 WebCamControl.SetInterfaceSafetyOptions 实现             
        } 

        public UserControl1()
        {
            InitializeComponent();
        }


        public void say()
        {
            MessageBox.Show("i am C# say");
        }

        
        private void button1_Click(object sender, EventArgs e)
        {
            PlayGame();
        }

    }
}

html最终界面:



html代码:
<body> 

	<form>	
		<object id="my" 
			classid="clsid:1EA4DBF0-3C3B-11CF-810C-00AA00389B73" Width="900" Height="200"> 
		</object>
	</form>
<script type="text/javascript">
		function test()
{
	document.write("Hello World!");
}
window.onload=function()
{

	var obj=document.getElementById("my");
         //start 相应C#控件,非常重要的部分
	function fn()
	{
		function obj::PlayGame()
		{
			alert("aaaa");
		}
	}
	fn();
         //end 好像必须这样写
	alert("aaaaaaaaaa");
	//alert(my);
	alert(obj);
}

</script>

<input type='button' onclick='my.say()' value='Click'> 
<input type='button' onclick='test()' value='Click'> 
</body


  • 大小: 39.5 KB
  • 大小: 94.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics