Form2,向Form1发送消息 0x0444
为自定义进程间通信代码。
private IntPtr ip = IntPtr.Zero; [DllImport("user32")] private static extern bool SendMessage(IntPtr a, int b, int c, string d); [DllImport("User32.dll", EntryPoint = "FindWindow")] private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
ip = FindWindow(null, "Form1"); if (ip == IntPtr.Zero) { MessageBox.Show("Form1未运行。"); return; } else { SendMessage(this.ip, 0x0444, 100, "ssss"); }
Form1,等待接收消息并进行相应处理
protected override void DefWndProc(ref System.Windows.Forms.Message m) { switch (m.Msg) { case 0x0444: break; default: base.DefWndProc(ref m); break; } }
|
文章评论