private void frmInteropCtrl_Load(object sender, EventArgs e) { // 允许在不同线程中使用控件 System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; // 填写事件 DoAMouse += new DoAMouseEventHandler(OnDoAMouse); DoGesture += new DoAMouseEventHandler(OnDoGesture); // 加载键盘驱动 m_myIoSys.InitSuperKeys(); // 初始化串口 InitSerialPort(); // 准备与传感器控制程序通信 m_UdpSensorNet = new UdpClient(); m_UdpSensorNet.Client.ReceiveTimeout = 1000; try { m_udpReciver = new UdpClient(LOCAL_ADDRESS); // m_tcpSender = new TcpClient(ROBOT_ADDRESS.Address.ToString(), ROBOT_ADDRESS.Port); // m_tcpSender.NoDelay = false; // m_tcpSender.ReceiveTimeout = 5000; m_nsRobotControl = m_tcpSender.GetStream(); } catch(Exception ex) { WriteLine(ex.Message); } if (null != m_udpReciver) m_udpReciver.BeginReceive(UdpCallBack, m_udpReciver); // 打开串口 try { if (!spGlove.IsOpen) spGlove.Open(); } catch (Exception ex) { WriteLine(ex.Message); } }
/// <summary> /// 游戏退出时执行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void m_prcsGame_Exited(object sender, EventArgs e) { DoAMouse += new DoAMouseEventHandler(OnDoAMouse); m_prcsGame = null; m_bLeftDown = false; m_bRightDown = false; if (null != m_tmrChangeMode) m_tmrChangeMode.Dispose(); }
private void BtnGameClick(object sender, EventArgs e) { // 问题:如何得知游戏已经退出~ // 解答:Process 类的 Exited 事件 // 改变处理手套数据的方式 DoAMouse += new DoAMouseEventHandler(OnDoGameCtrl); // 清理标志位 m_bGameMode = false; m_byLastGesture = (byte)AMouseAction.Init; // 设为初始状态 m_byLastDirection = (byte)GameCtrl.Init; // 设为初始方向 m_tmrChangeMode = new System.Threading.Timer( TmrGameTick, "", Timeout.Infinite, Timeout.Infinite); m_bTiming = false; // 游戏路径 string gamePath = @"D:\game\nfs6"; string fileName = gamePath + "\\RunNfs.bat"; try { // 若游戏已经打开,则将其激活 if (null != m_prcsGame && !m_prcsGame.HasExited) { SetForegroundWindow(m_prcsGame.MainWindowHandle); return; } // end of if // 运行 .bat 问件,打开游戏 Process tempPrcs = new Process(); tempPrcs.StartInfo.FileName = fileName; // 不显示窗口 tempPrcs.StartInfo.CreateNoWindow = true; tempPrcs.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; // 启动进程 tempPrcs.Start(); // 等待游戏启动 Thread.Sleep(300); // 获得游戏进程 m_prcsGame = (Process.GetProcessesByName("NFSHP2"))[0]; // 设置该属性以使 Exited 事件能够被触发 m_prcsGame.EnableRaisingEvents = true; m_prcsGame.Exited += new EventHandler(m_prcsGame_Exited); Thread.Sleep(14000); // 等待14秒开始自动进入游戏 //SetForegroundWindow(m_prcsGame.MainWindowHandle); WriteLine(m_prcsGame.MainWindowHandle.ToString()); int i; WriteLine("sending enter"); for (i = 0; i < 4; i++ ) { m_myIoSys.KeyPressEx(WinIoSys.Key.VK_RETURN, 100); Thread.Sleep(500); } Thread.Sleep(800); WriteLine("sending enter2"); for (i = 0; i < 3; i++ ) { m_myIoSys.KeyPressEx(WinIoSys.Key.VK_RETURN, 100); Thread.Sleep(500); } Thread.Sleep(2000); WriteLine("sending tab"); for(i = 0; i < 5; i++) { m_myIoSys.KeyPress(WinIoSys.Key.VK_TAB, 200); Thread.Sleep(500); } Thread.Sleep(500); WriteLine("sending enter"); m_myIoSys.KeyPressEx(WinIoSys.Key.VK_RETURN, 100); } catch (Exception ex) { try { if (null != m_prcsGame && !m_prcsGame.HasExited) m_prcsGame.Kill(); m_prcsGame = null; } catch { } WriteLine(ex.Message); } }