软件编程
位置:首页>> 软件编程>> C#编程>> c# 实现控件(ocx)中的事件详解

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

作者:richerg85  发布时间:2022-12-29 04:20:50 

标签: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
   }
 }
}

来源:https://blog.csdn.net/richerg85/article/details/7399081

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com