一、發送端
// 窗體:發送端
public partial class SendForm : Form
{public SendForm(){InitializeComponent();}// 按鈕:發送private void btnSend_Click(object sender, System.EventArgs e){IntPtr hwnd = User32Helper.FindWindow(null, "接收端");int msg = 0x0401; // 自定義消息IntPtr param = new IntPtr(10); //自定義參數User32Helper.SendMessage(hwnd, msg, param, IntPtr.Zero);}
}
二、接收端
?
// 窗體:接收端
public partial class ReceiveForm : Form
{public ReceiveForm(){InitializeComponent();}// 重寫方法:窗體消息protected override void WndProc(ref Message m){switch (m.Msg){case 0x0401: // 自定義消息textBox1.Text = m.WParam.ToString(); //自定義參數break;}base.WndProc(ref m);}
}
三、幫助類
internal class User32Helper
{[DllImport("User32.dll", EntryPoint = "FindWindow")]public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);[DllImport("User32.dll", EntryPoint = "SendMessage")]public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
}