110,533
社区成员
发帖
与我相关
我的任务
分享
using System.Reflection;
public class Form1
{
private System.Windows.Forms.ToolStrip TestToolStrip;
private void Form1_Load(object sender, System.EventArgs e)
{
this.TestToolStrip = new ToolStrip();
this.TestToolStrip.Items.Add(new ToolStripButton("TestButton"));
this.TestToolStrip.Items.Add(new ToolStripComboBox("TestComboBox"));
this.TestToolStrip.Items.Add(new ToolStripDropDownButton("TestDropDownButton"));
this.TestToolStrip.Dock = DockStyle.Top;
this.Controls.Add(this.TestToolStrip);
string Path = Application.StartupPath + "\\Extended";
string FileName = FileSystem.Dir(Path + "\\*.dll");
while (FileName != "") {
AddExtendItem(Path + "\\" + FileName);
FileName = FileSystem.Dir();
}
}
private void AddExtendItem(string dllFilePath)
{
try {
Assembly a = Assembly.LoadFrom(dllFilePath);
Type[] MyTypes = a.GetTypes();
foreach (Type t in MyTypes) {
if (t.Name == "ExtendedItem") {
object[] Parameters = {this.TestToolStrip};
object obj = Activator.CreateInstance(t, Parameters);
break; // TODO: might not be correct. Was : Exit For
}
}
}
catch (Exception ex) {
Interaction.MsgBox(ex.Message, MsgBoxStyle.Critical, "Error");
}
}
}
using System.Windows.Forms;
public class ExtendedItem
{
ToolStripDropDownButton Testbtn;
private Threading.Thread ListenThread;
private Net.Sockets.UdpClient listen;
public ExtendedItem(ToolStrip _ToolStrip)
{
this.Testbtn = new ToolStripDropDownButton("TestDllDropDownButton");
{
this.Testbtn.DropDownItems.Add("TestItem1");
this.Testbtn.DropDownItems.Add("TestItem2");
this.Testbtn.DropDownItems.Add("CloseUdpClient", null, Close);
}
_ToolStrip.Items.Add(this.Testbtn);
try {
listen = new Net.Sockets.UdpClient(2525);
ListenThread = new Threading.Thread(ListenPort);
ListenThread.Start();
}
catch (Exception ex) {
Interaction.MsgBox(ex.Message, MsgBoxStyle.Critical, "Error");
}
}
//侦听指定端口的广播地址UDP包
private void ListenPort()
{
Net.IPEndPoint tempEnd = new Net.IPEndPoint(Net.IPAddress.Any, 2525);
while (true) {
Application.DoEvents();
try {
byte[] recb = listen.Receive(tempEnd);
CheckMessage(recb);
}
catch (Exception e) {
break; // TODO: might not be correct. Was : Exit Try
}
}
}
private void CheckMessage(byte[] bytes)
{
//省略
}
private void Close(object sender, System.EventArgs e)
{
if (ListenThread != null)
ListenThread.Abort();
if (listen != null)
listen.Close();
}
}
Imports System.Reflection
Public Class Form1
Private TestToolStrip As System.Windows.Forms.ToolStrip
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TestToolStrip = New ToolStrip
Me.TestToolStrip.Items.Add(New ToolStripButton("TestButton"))
Me.TestToolStrip.Items.Add(New ToolStripComboBox("TestComboBox"))
Me.TestToolStrip.Items.Add(New ToolStripDropDownButton("TestDropDownButton"))
Me.TestToolStrip.Dock = DockStyle.Top
Me.Controls.Add(Me.TestToolStrip)
Dim Path As String = Application.StartupPath + "\Extended"
Dim FileName As String = Dir(Path + "\*.dll")
While FileName <> ""
Call AddExtendItem(Path + "\" + FileName)
FileName = Dir()
End While
End Sub
Private Sub AddExtendItem(ByVal dllFilePath As String)
Try
Dim a As [Assembly] = [Assembly].LoadFrom(dllFilePath)
Dim MyTypes As Type() = a.GetTypes()
For Each t As Type In MyTypes
If t.Name = "ExtendedItem" Then
Dim Parameters() As Object = {Me.TestToolStrip}
Dim obj As [Object] = Activator.CreateInstance(t, Parameters)
Exit For
End If
Next
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
End Class
Imports System.Windows.Forms
Public Class ExtendedItem
Dim Testbtn As ToolStripDropDownButton
Private ListenThread As Threading.Thread
Private listen As Net.Sockets.UdpClient
Public Sub New(ByVal _ToolStrip As ToolStrip)
Me.Testbtn = New ToolStripDropDownButton("TestDllDropDownButton")
With Me.Testbtn.DropDownItems
.Add("TestItem1")
.Add("TestItem2")
.Add("CloseUdpClient", Nothing, AddressOf Close)
End With
_ToolStrip.Items.Add(Me.Testbtn)
Try
listen = New Net.Sockets.UdpClient(2525)
ListenThread = New Threading.Thread(AddressOf ListenPort)
ListenThread.Start()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
'侦听指定端口的广播地址UDP包
Private Sub ListenPort()
Dim tempEnd As New Net.IPEndPoint(Net.IPAddress.Any, 2525)
While True
Application.DoEvents()
Try
Dim recb As Byte() = listen.Receive(tempEnd)
Call CheckMessage(recb)
Catch e As Exception
Exit Try
End Try
End While
End Sub
Private Sub CheckMessage(ByVal bytes() As Byte)
'省略
End Sub
Private Sub Close(ByVal sender As Object, ByVal e As System.EventArgs)
If ListenThread IsNot Nothing Then ListenThread.Abort()
If listen IsNot Nothing Then listen.Close()
End Sub
End Class
internal class UdpClient
{
public UdpClient(){}//初始化,构造函数
~UdpClient(){}//显示声明析构函数,在此做清理工作
}
using System.Windows.Forms;
public class ExtendedItem
{
//将dll修改,增加析构函数。不行吗?
~ExtendedItem()
{
Close(null,null);
}
private void Close(object sender, System.EventArgs e)
{
if (ListenThread != null)
ListenThread.Abort();
if (listen != null)
listen.Close();
}
}