/// <summary> /// 点击按钮,先按下按钮不松开,然后观察按钮是否发生样式变化;如果 /// 没有发生变化,则一段时间后再次检查,直到发生变化后才松开按钮,形成一次 /// 点击事件。如果按下很长时间都没有发生变化,则认为手机模拟器卡死。 /// </summary> /// <param name="ClickRange">观察范围</param> public static void ClickBtn(Rectangle ClickRange) { Bitmap Before = ImageHandleHelper.CopyScreen(ClickRange); Bitmap After; for (int i = 0; i < 200; i++) { if (OpencvImageHelper.CheckChange(ClickRange, 4)) { if (i % 10 == 0) { MouseClick(MouseClickType.LeftPress); } if (OpencvImageHelper.Compare2Images(Before, After = ImageHandleHelper.CopyScreen(ClickRange), 0) != 0) { Before.Dispose(); Before = After; MouseClick(MouseClickType.LeftRelease); break; } After.Dispose(); } } for (int i = 0; i < 200; i++) { if (OpencvImageHelper.CheckChange(ClickRange, 4)) { using (Before) { } return; } } MouseMove(ClickRange.X > ClickRange.Width ? 0 : ClickRange.X + ClickRange.Width + 1000, 0); MouseClick(MouseClickType.LeftRelease); throw new Exception("界面卡死。。(ERROR:-999)"); }
/// <summary> /// 观察某一个地方是否发生变化,如果发生变化则返回, /// 如果不发生变化则阻塞,直到真正发生变化或观察超时为止 /// </summary> /// <param name="Before">变化前截图</param> /// <param name="Range">观察范围</param> /// <param name="AutoDispose">是否自动是否变化前截图</param> public static void ObserveChange(Bitmap Before, Rectangle Range, bool AutoDispose = true) { for (int i = 0, x, y; i < 1000; i++) { Thread.Sleep(10); if (!ImageHandleHelper.CopyScreen(Range).OCRImages(Before, out x, out y)) { if (AutoDispose) { using (Before) { } } return; } } throw new Exception("界面卡死!(ERROR:-999)"); }
/// <summary> /// 重复点击同一个按钮,直到界面发生变化时才停止点击,用于确保 /// 界面卡顿也能触发点击事件。 /// </summary> /// <param name="ChangeRange">需要观察的变化范围</param> public static void RepeatClickBtn(Rectangle ChangeRange) { Bitmap Before = ImageHandleHelper.CopyScreen(ChangeRange); for (int i = 0; i < 1000; i++) { if (i % 10 == 0) { MouseHelper.MouseClick(MouseClickType.LeftClick); } Thread.Sleep(25); if (OpencvImageHelper.Compare2Images(Before, ImageHandleHelper.CopyScreen(ChangeRange), 1) != 0) { return; } } throw new Exception("界面卡死!(ERROR:-999)"); }
/// <summary> /// 长按一个按钮,并观察画面是否发生变化,如果有变化则长按事件完成。 /// 如果长时间没有变化,则认为手机模拟器卡死。 /// </summary> /// <param name="pressRange">观察范围</param> public static void LongPressBtn(Rectangle pressRange) { using (Bitmap Before = ImageHandleHelper.CopyScreen(pressRange)) { for (int i = 0, x, y; i < 1000; i++) { if (i % 10 == 0) { MouseClick(MouseClickType.LeftPress); Thread.Sleep(500); } if (!ImageHandleHelper.CopyScreen(pressRange).OCRImages(Before, out x, out y, true, .9)) { MouseClick(MouseClickType.LeftRelease); return; } } } throw new Exception("界面卡死!(ERROR:-999)"); }
/// <summary> /// 检查当前微信app页是否在变化。如果检查多次界面一样时, /// 认为界面不动,如果有任意两次不一样,则认为界面是动的 /// </summary> /// <param name="CheckRange">检查范围</param> /// <param name="Times">检查次数</param> /// <returns>返回true,界面不动;返回false,界面在动</returns> public static bool CheckChange(Rectangle CheckRange, int Times = 8) { int SameTimes = 0; using (Bitmap Before = ImageHandleHelper.CopyScreen(CheckRange)) { for (int i = 0, x, y; i < Times; i++) { Thread.Sleep(12); Bitmap After = ImageHandleHelper.CopyScreen(CheckRange); if (After.OCRImages(Before, out x, out y)) { SameTimes++; } else { return(false); } } } return(SameTimes == Times); }