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
        }


    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值