一、新建WinForm项目,WindowsFormsApp_ProcessA
界面,窗口添加了Load事件
代码
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;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace WindowsFormsApp_ProcessA
{
public partial class Form1 : Form
{
ProcessStartInfo info = new ProcessStartInfo();
Process pro = new Process();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
info.FileName = "E:\\projects_vs2019\\WindowsFormsApp\\WindowsFormsApp_ProcessA\\bin\\Debug\\WindowsFormsApp_ProcessB.exe";
pro.StartInfo = info;
}
/// <summary>
/// 启动被控进程ProcessB
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
pro.Start();
}
/// <summary>
/// 退出被控进程ProcessB
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
pro.Kill();
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="hWnd">窗口句柄</param>
/// <param name="Msg">消息值</param>
/// <param name="wParam">参数一</param>
/// <param name="lParam">参数二</param>
/// <returns></returns>
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wP, IntPtr lP);
/// <summary>
/// 向被控进程ProcessB发送消息按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
try
{
IntPtr hWnd = pro.MainWindowHandle; //获取Form1.exe主窗口句柄
String s = this.textBox1.Text;//消息内容
if (s == String.Empty) s = "0";
int data = Convert.ToInt32(s); //获取文本框数据
SendMessage(hWnd, 0x0100, (IntPtr)data, (IntPtr)0); //发送WM_KEYDOWN消息
}
catch (Exception ex)
{
MessageBox.Show("只能给<启动被控进程ProcessB>按钮创建的窗口发送消息");
}
//点击该按钮,以文本框数据为参数,向Form1发送WM_KEYDOWN消息
}
//----------------------------------------------------------------------------------------
/// <summary>
/// 定义结构体
/// </summary>
public struct COPYDATASTRUCT
{
public IntPtr dwData; //可以是任意值
public int cbData; //指定lpData内存区域的字节数
[MarshalAs(UnmanagedType.LPStr)]
public string lpData; //发送给目录窗口所在进程的数据
}
/// <summary>
/// 查找窗口
/// </summary>
/// <param name="lpClassName">窗口类名</param>
/// <param name="lpWindowName">窗口标题</param>
/// <returns></returns>
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);
/// <summary>
/// 发送消息
/// </summary>
/// <param name="hWnd">窗口句柄</param>
/// <param name="Msg">消息值</param>
/// <param name="wParam">参数一</param>
/// <param name="lParam">参数二</param>
/// <returns></returns>
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);
/// <summary>
/// 定义消息值
/// </summary>
private const int WM_COPYDATA = 0x004A; //必须是这个数值,不能更改
/// <summary>
/// 查找窗口发送消息按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
int hWnd = FindWindow(null, this.textBox2.Text);//窗口标题
if (hWnd == 0)
{
MessageBox.Show("未找到标题为 "+this.textBox2.Text+" 的窗口");
}
else
{
string sendString = textBox1.Text; //发送内容
byte[] sarr = System.Text.Encoding.Default.GetBytes(sendString);
int len = sarr.Length;
COPYDATASTRUCT cds;
cds.dwData = (IntPtr)0;
cds.cbData = len + 1;
cds.lpData = sendString;
SendMessage(hWnd, WM_COPYDATA, 0, ref cds);
}
}
}
}
二、新建WinForm项目,WindowsFormsApp_ProcessB
窗口控件只有一个label,窗口添加了 KeyDown事件
代码
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 WindowsFormsApp_ProcessB
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
this.label1.Text = Convert.ToString(e.KeyValue);
}
//-------------------------------------------------------------------------
/// <summary>
/// 定义结构体
/// </summary>
public struct COPYDATASTRUCT
{
public IntPtr dwData; //可以是任意值
public int cbData; //指定lpData内存区域的字节数
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public string lpData; //发送给目录窗口所在进程的数据
}
/// <summary>
/// 定义消息值
/// </summary>
private static int WMA_InterPro = 0x004A;
protected override void DefWndProc(ref Message m)
{
if (m.Msg == WMA_InterPro)
{
COPYDATASTRUCT cds = new COPYDATASTRUCT();
Type t = cds.GetType();
cds = (COPYDATASTRUCT)m.GetLParam(t);
string receiveInfo = cds.lpData; //接收内容
label1.Text = receiveInfo;
}
else
{
base.DefWndProc(ref m);
}
}
}
}