目录
程序1
界面:
代码:
-
using System;
-
using System.Runtime.InteropServices;
-
using System.Windows.Forms;
-
-
namespace
SendDemo1st
-
{
-
public
partial
class
Form1 :
Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
public
struct CopyDataStruct
-
{
-
public IntPtr dwData;
-
public
int cbData;
-
[
MarshalAs(UnmanagedType.LPStr)]
-
public
string lpData;
-
}
-
-
public
const
int WM_COPYDATA =
0x004A;
-
-
//通过窗口标题来查找窗口句柄
-
[
DllImport("User32.dll", EntryPoint = "FindWindow")]
-
private static extern int FindWindow(string lpClassName, string lpWindowName);
-
-
//在DLL库中的发送消息函数
-
[
DllImport("User32.dll", EntryPoint = "SendMessage")]
-
private static extern int SendMessage
-
(
-
int hWnd, // 目标窗口的句柄
-
int Msg, // 在这里是WM_COPYDATA
-
int wParam, // 第一个消息参数
-
ref CopyDataStruct lParam // 第二个消息参数
-
);
-
-
//发送消息
-
private void button1_Click(object sender, EventArgs e)
-
{
-
string strURL = textBox1.Text;
-
CopyDataStruct cds;
-
cds.dwData = (IntPtr)
1;
//这里可以传入一些自定义的数据,但只能是4字节整数
-
cds.lpData = strURL;
//消息字符串
-
cds.cbData = System.Text.Encoding.Default.GetBytes(strURL).Length +
1;
//注意,这里的长度是按字节来算的
-
SendMessage(FindWindow(
null,
"Form2"), WM_COPYDATA,
0,
ref cds);
// 窗口标题
-
}
-
-
//接收消息方法
-
protected override void WndProc(ref System.Windows.Forms.Message e)
-
{
-
if (e.Msg == WM_COPYDATA)
-
{
-
CopyDataStruct cds = (CopyDataStruct)e.GetLParam(
typeof(CopyDataStruct));
-
textBox2.Text = cds.lpData.ToString();
-
}
-
base.WndProc(
ref e);
-
}
-
}
-
}
程序2
界面:
代码:
-
using System;
-
using System.Runtime.InteropServices;
-
using System.Windows.Forms;
-
-
namespace
SendDemo2nd
-
{
-
public
partial
class
Form2 :
Form
-
{
-
public Form2()
-
{
-
InitializeComponent();
-
}
-
-
public
struct CopyDataStruct
-
{
-
public IntPtr dwData;
-
public
int cbData;
-
-
[
MarshalAs(UnmanagedType.LPStr)]
-
-
public
string lpData;
-
}
-
-
public
const
int WM_COPYDATA =
0x004A;
-
-
//通过窗口标题来查找窗口句柄
-
[
DllImport("User32.dll", EntryPoint = "FindWindow")]
-
private static extern int FindWindow(string lpClassName, string lpWindowName);
-
-
//在DLL库中的发送消息函数
-
[
DllImport("User32.dll", EntryPoint = "SendMessage")]
-
private static extern int SendMessage
-
(
-
int hWnd, // 目标窗口的句柄
-
int Msg, // 在这里是WM_COPYDATA
-
int wParam, // 第一个消息参数
-
ref CopyDataStruct lParam // 第二个消息参数
-
);
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
string strURL = textBox1.Text;
-
CopyDataStruct cds;
-
cds.dwData = (IntPtr)
1;
//这里可以传入一些自定义的数据,但只能是4字节整数
-
cds.lpData = strURL;
//消息字符串
-
cds.cbData = System.Text.Encoding.Default.GetBytes(strURL).Length +
1;
//注意,这里的长度是按字节来算的
-
SendMessage(FindWindow(
null,
"Form1"), WM_COPYDATA,
0,
ref cds);
// 窗口标题
-
}
-
-
-
//接收消息方法
-
protected override void WndProc(ref System.Windows.Forms.Message e)
-
{
-
if (e.Msg == WM_COPYDATA)
-
{
-
CopyDataStruct cds = (CopyDataStruct)e.GetLParam(
typeof(CopyDataStruct));
-
textBox2.Text = cds.lpData.ToString();
-
}
-
base.WndProc(
ref e);
-
}
-
}
-
}
测试
可以看到,程序1和程序2代码没什么分别,下面测试下效果:
如果你这个帖子对你有用,欢迎给我点赞 + 留言,谢谢
end
转载:https://blog.csdn.net/qq_38693757/article/details/117379292
查看评论