欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# 实现控件(ocx)中的事件详解

程序员文章站 2022-06-24 09:58:58
c#控件实现类似c++中ocx控件功能c++中ocx控件1、控件方法2、控件事件c#很容易实现c++中ocx中控件方法的功能,但是实现类似c++中ocx的控件事件,则需要一定的周折。下面就用实例简单的...

c#控件实现类似c++中ocx控件功能

c++中ocx控件

1、控件方法

2、控件事件

c#很容易实现c++中ocx中控件方法的功能,但是实现类似c++中ocx的控件事件,则需要一定的周折。

下面就用实例简单的介绍c#如何实现

c#中activex(ocx)实现实例(vs2008环境下):

using system;
using system.collections.generic;
using system.componentmodel;
using system.drawing;
using system.data;
using system.linq;
using system.text;
using system.windows.forms;
using system.runtime.interopservices;
using system.threading;
namespace simocx
{
//代理
  public delegate void recvmsghandler(string msg);
  public delegate void simplehandler();
  public delegate void changehandler(int value);
  [guidattribute("1a585c4d-3371-48dc-af8a-affecc1b0967")]
  [interfacetypeattribute(cominterfacetype.interfaceisidispatch)]
  public interface controlevents
  {
    
    [dispidattribute(0x001)]
    void onrecvmsg(string msg);
    [dispidattribute(0x002)]
    void onrecordstopped();
    [dispidattribute(0x003)]
    void onrecordvolumechanged(int value);
  }
  //控件的uuid,这个uuid需要手动添加,添加方法为(vs2008):工具--->创建guid,如果找不到,在外部对象中查找一下。
  [guid("c170dcdb-43c5-4a90-a984-95d9719eda11")]
  //指示应用该属性的对象对com可见
  [comvisible(true)]
  //将事件接收接口连接到托管类
  [comsourceinterfacesattribute(typeof(controlevents))]
  public partial class simocx : usercontrol
  {
//定义事件
    public event recvmsghandler onrecvmsg;
    public event simplehandler onrecordstopped;
    public event changehandler onrecordvolumechanged;
    public simocx()
    {
      initializecomponent();
    }
    //方法
    public void setvalue(string value)
    {
      //事件调用
      onrecordstopped(); 
      onrecvmsg(value);
      onrecordvolumechanged(20);
    }
  }
}

以上程序集编辑生成后,生成*.dll。下面是使用两种方式调用此dll:

一种,网页调用,此控件即为activex控件:

htm中代码:

<html>
<head>
<title></title> 
<object id="notify" classid="clsid:c170dcdb-43c5-4a90-a984-95d9719eda11"></object>
//以下是调用控件的事件
<script language="javascript" for="notify" event="onrecordvolumechanged(value)">
 mydiv.innerhtml= 'in javascript: get volume:'+value;
 </script>
<script language="javascript" for="notify" event="onrecvmsg()">
alert("started");
 mydiv.innerhtml= 'in javascript: onrecvmsg';
 </script>
 
 <script language="javascript" for="notify" event="onrecordstopped()">
 alert("stopped");
 mydiv.innerhtml= 'in javascript: onrecordstopped';
 </script>
</head>
<body>
 
<form>
 <script language="javascript" type="text/jscript">
      function button1_onclick() {
        notify.setvalue("value");//调用控件方法
      }
</script>
 <div id="mydiv">nothing happened</div>
 <input id="button1"type="button"value="start"οnclick="button1_onclick()"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 <input id="button2"type="button"value="stop"οnclick="button2_onclick()"/></p>
</form>
</body>
</html>

再一种,c#winform程序调用,此控件即类似为ocx控件:

c#winform部分代码:

public partial class form1 : form
  {
    public form1()
    {
      initializecomponent();
    }
    private void btn_click(object sender, eventargs e)
    {
      if (!this.tbsendmsg.text.trim().equals(""))
      {
        simocx.setvalue(this.tbsendmsg.text.trim());//调用控件方法
      }
    }
    private void simocx_onrecvmsg(string msg)//调用控件事件
    {
      messagebox.show(msg);
    }
    private void simocx_onrecordstopped()//调用控件事件
    {
      messagebox.show("testttt");
    }
  }

c# 实现控件(ocx)中的事件详解

c# 实现控件(ocx)中的事件详解

补充知识:c#如何绑定dll或者ocx组件中的事件

主要流程

1.注册(regsvr32)将要引用的组件(dll或者ocx)

2.引用将要调用的组件(dll或者ocx)

3.查看组件中事件的函数的声明原型(并在项目中定义响应的函数原型与之对应,便于后面进行事件绑定)

4.代码如下(分为两种方式,一种是通过tlbimp.exe工具导出成公共语言运行库程序集(可能我描述的不正确,见谅,如何导出请自行搜索方法),另一种不经过转换,使用原文件)

#define is_tlbimp 
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
 
namespace windowsapplication1
{  
  public partial class form1 : form
  {
    public delegate void recvdataevent(short datalen, short slaveaddress, ref float[] data); 
    public void recvdata03event(short datalen, short slaveaddress, ref float[] data)
    {
 
      console.writeline(datalen);
    }
 
    public form1()
    {
      initializecomponent();
    }
 
    private void button1_click(object sender, eventargs e)
    {
 
#if is_tlbimp
      test_modbus_csharp.modbusclass modbus = new test_modbus_csharp.modbusclass();
      modbus.initmscom(12, "9600,n,8,1");
      modbus.recdata03debug += new test_modbus_csharp.__modbus_recdata03debugeventhandler(recvdata03event);
      //modbus.recdata03debug += new test_modbus_csharp.__modbus_recdata03debugeventhandler(new recvdataevent(recvdata03event));
      modbus.debug_info();      
      modbus.closeport();
#else
      test_modbus.modbusclass modbus = new test_modbus.modbusclass();
      modbus.initmscom(12, "9600,n,8,1");
      modbus.recdata03debug += new test_modbus.__modbus_recdata03debugeventhandler(recvdata03event);
      //modbus.recdata03debug += new test_modbus.__modbus_recdata03debugeventhandler(new recvdataevent(recvdata03event));
      modbus.debug_info();      
      modbus.closeport();
#endif
    } 
  }
}

以上这篇c# 实现控件(ocx)中的事件详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

相关标签: c# 控件 ocx