C#实现不同窗口间的消息通信


有时候我们需要在一个程序中向另外一个窗口传递数据,以实现不同窗口间的数据通信,下面介绍两种通信方式,一种是使用窗体的消息处理机制,另一种是使用委托实现消息通信。

一、重写窗体消息发送机制来发送自定义消息


首先创建一个窗口程序A,添加数据发送以及打开窗口B的按钮,并重写消息发送函数,如图:

窗口程序A代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WinFormTest
{
    public struct CustomMsg
    {
        public string str;
    }
 
    public partial class Form1 : Form
    {
        //自定义消息
        public const int USER = 0x800;
        public const int MYMESSAGE = USER + 1;
 
        /// <summary>
        /// 重写消息发送函数
        /// </summary>
        /// <param name="hWnd">信息发往的窗口的句柄</param>
        /// <param name="Msg">消息ID</param>
        /// <param name="wParam">参数1</param>
        /// <param name="lParam">自定义结构</param>
        /// <returns></returns>
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, ref CustomMsg lParam);
 
        /// <summary>
        /// 获取窗口句柄
        /// </summary>
        /// <param name="lpClassName"></param>
        /// <param name="lpWindowName">窗口名称</param>
        /// <returns></returns>
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
 
        private void UnitySendMessage(CustomMsg msg)
        {
            IntPtr windows = FindWindow(null, "FormB");
            if (windows != IntPtr.Zero)
            {
                SendMessage(windows, MYMESSAGE, 0, ref msg);
            }
        }
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            FormB formB = new FormB();
            formB.Show();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            CustomMsg customMsg = new CustomMsg();
            customMsg.str = textBox2.Text;
            UnitySendMessage(customMsg);
        }
    }
}


窗口程序B接收代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WinFormTest
{
    public partial class FormB : Form
    {
        //自定义消息
        public const int USER = 0x800;
        public const int MYMESSAGE = USER + 1;
 
        public FormB()
        {
            InitializeComponent();
        }
 
        //重写窗体的消息处理函数DefWndProc,从中加入自定义消息 MYMESSAGE 的检测处理入口
        protected override void DefWndProc(ref Message m)
        {
            switch (m.Msg)
            {
                //接收自定义消息MYMESSAGE,并显示其参数
                case MYMESSAGE:
                    CustomMsg msg = new CustomMsg();
                    Type t = msg.GetType();
                    msg = (CustomMsg)m.GetLParam(t);
                    ShowMessage(msg);
                    break;
                default:
                    //调用基类函数,以便系统处理其他消息
                    base.DefWndProc(ref m);
                    break;
            }
        }
 
        public delegate void ShowMessageCallback(CustomMsg msg);
        public void ShowMessage(CustomMsg msg)
        {
            if (this.InvokeRequired)
            {
                ShowMessageCallback d = new ShowMessageCallback(ShowMessage);
                this.Invoke(d, new object[] { msg });
            }
            else
            {
                textBox1.Text = msg.str;
            }
        }
    }
}


最后运行程序进行测试,测试结果如图:

二、使用委托进行进程间或窗体间通信


首先在类A或窗体A中创建委托事件并且发布事件,如:

#region 调试
public delegate void DebugHandler(SerialMsg msg);
/// <summary>
/// 调试使用
/// </summary>
public event DebugHandler DebugEvent;
/// <summary>
/// 直接输出接收到的数据
/// </summary>
/// <param name="msg"></param>
public void SerialToolsDebug(SerialMsg msg)
{
DebugEvent?.Invoke(msg);
}
#endregion


A发布了事件DebugEvent,在B中进行事件订阅,如:

Class_A.DebugEvent += GetDataFromDebugEvent;
 
private void GetDataFromDebugEvent(SerialMsg msg)
{
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, (Action)(() =>
{
//处理msg消息
}));
}


实际使用时,当A中调用SerialToolsDebug(msg)发布消息后,B中就可以接收到A发布的消息,使用函数GetDataFromDebugEvent来处理,B中处理消息时使用匿名的委托函数来进行后台更新数据,可以避免频繁刷新UI界面时造成卡顿的情况。

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以使用C#中的Socket类来实现LAN口通信,并将其与窗口程序结合起来。以下是一个简单的示例代码: ```csharp using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Windows.Forms; public class Form1 : Form { private Socket _socket; private byte[] _buffer; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // 创建Socket对象 _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 设置本机IP和端口号 IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); int port = 8888; IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, port); // 连接到服务器 _socket.Connect(ipEndPoint); // 接收数据 _buffer = new byte[1024]; _socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null); } private void ReceiveCallback(IAsyncResult ar) { int bytesRead = _socket.EndReceive(ar); if (bytesRead > 0) { string message = Encoding.ASCII.GetString(_buffer, 0, bytesRead); // 处理接收到的数据 MessageBox.Show(message); } // 继续接收数据 _buffer = new byte[1024]; _socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null); } } ``` 在这个示例中,我们创建了一个Socket对象并连接到本机IP地址为127.0.0.1、端口号为8888的服务器。然后我们使用BeginReceive方法异步接收数据,并在接收到数据后处理它。 您可以将此示例代码放入一个Windows窗体程序中,并在Form1_Load事件中调用它,以在程序启动时自动连接到服务器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值