- //1.新建一个解决方案,是程序A,窗体后台代码重写DefWndProc
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- protected override void DefWndProc(ref Message m)
- {
- if (m.Msg == 0x104)
- {
- m.Result = (IntPtr)333;
- return;
- }
- else
- {
- }
- base.DefWndProc(ref m);
- }
- }
- //2.新建一个解决方案,程序B,
- //点击button2的时候,会向A发送一个消息,消息类型是104,两个参数1和2,A能捕获到,设置结果为333,那最后在B中的ip的值就是333
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- [DllImport("User32.dll", EntryPoint = "SendMessage")]
- private static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
- private void button2_Click(object sender, EventArgs e)
- {
- Process[] arrPro = Process.GetProcessesByName("WindowsFormsApplication1.vshost");
- IntPtr ip = SendMessage(arrPro[0].MainWindowHandle, 0x104, 1, 2);
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/040720134430.html
来源: http://www.codesnippet.cn/detail/040720134430.html