C#句柄实现窗体应用程序间的通信

发送端:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace WindowsFormsApp1
{

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        [DllImport("user32.dll")]
        private static extern Int32 FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        private static extern long SendMessage(Int32 hwnd, Int32 msg, Int32 hwndFrom, ref COPYDATASTRUCT cd);

        [StructLayout(LayoutKind.Sequential)]
        public struct COPYDATASTRUCT
        {
            public IntPtr dwData;
            public int cbData;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpData;
        }

        const int WM_COPYDATA = 0x004A;

        private void button1_Click(object sender, EventArgs e)
        {
//第一个参数lpClassName和第二个参数lpWindowName分别表示需要查找窗口的类名和窗口名。
//如果分别传入空字符串,则可以查找所有窗口。
//FindWindow函数返回一个窗口句柄windowHandle,该句柄可用于后续对该窗口的操作。
            int psW = FindWindow(null, "叫号");//这里是接收端窗体的标题

            if (psW != 0)
            {
                byte[] sarr = System.Text.Encoding.Default.GetBytes(textBox1.Text);
                int len = sarr.Length;

                COPYDATASTRUCT cpData = new COPYDATASTRUCT(); //包含要通过 WM_COPYDATA 消息传递到另一个应用程序的数据。
                cpData.dwData = (IntPtr)0;//要传递给接收应用程序的数据类型。 接收应用程序定义有效类型。
                cpData.cbData = len + 1;//成员指向的数据的大小(以字节为单位)
                cpData.lpData = textBox1.Text;//要传递给接收应用程序的数据。 此成员可以为 NULL。
                int fsW = 0;
                SendMessage(psW, WM_COPYDATA, fsW, ref cpData);
            }
        }
    }
}

接收端:

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 WindowsFormsApp2
{
    public partial class 叫号1 : Form
    {
        public 叫号1()
        {
            InitializeComponent();
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct COPYDATASTRUCT
        {
            public IntPtr dwData;
            public int cbData;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpData;
        }

        protected override void DefWndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x004A://处理消息                    
                    COPYDATASTRUCT mystr = new COPYDATASTRUCT();
                    Type mytype = mystr.GetType();
                    mystr = (COPYDATASTRUCT)m.GetLParam(mytype);
                    this.textBox1.Text = mystr.lpData;
                    break;
                default:
                    base.DefWndProc(ref m);//调用基类函数处理非自定义消息。 
                    break;
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值