static void Main(string[] args) { string CurrentDirectory = System.Environment.CurrentDirectory.Replace("MessageCommunicationA", _messageTo); bool isStart = false; foreach (Process p in Process.GetProcessesByName(_messageTo)) { isStart = true; WinMessageHelper.Send(p.MainWindowHandle.ToInt32(), _messageStr); } if (!isStart) { System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo.FileName = _messageTo + ".exe"; process.StartInfo.WorkingDirectory = CurrentDirectory; process.StartInfo.CreateNoWindow = false; process.Start(); process.WaitForInputIdle(); System.Threading.Thread.Sleep(2000); //增加了延时2秒,这样就可以获取计算器的窗口句柄 foreach (Process p in Process.GetProcessesByName(_messageTo)) { WinMessageHelper.Send(p.MainWindowHandle.ToInt32(), _messageStr); } } }
protected override void DefWndProc(ref System.Windows.Forms.Message m) { switch (m.Msg) { case WinMessageHelper.WM_COPYDATA: string str = WinMessageHelper.Receive(ref m); if (str == "close") { Application.Exit(); Environment.Exit(0); } else { MessageBox.Show(str); MessageBox.Show(m.Msg.ToString()); } break; default: base.DefWndProc(ref m); break; } }