public AutomationState AddRelativeClick(int x, int y) { AutomationState state = AddState(AutomationType.RelativeClick, null); state.AddElement(x, y, null); return(state); }
public AutomationState AddKey(Keys key) { AutomationState state = AddState(AutomationType.Key, null); state.Key = key; return(state); }
private AutomationState AddState(AutomationType type, string path) { AutomationState state = new AutomationState(type, path); states.Add(state); return(state); }
public AutomationState AddWait(int milliseconds) { AutomationState state = AddState(AutomationType.Wait, null); state.Value = milliseconds; return(state); }
public AutomationState AddMoveMouseImageOffset(string path, int x, int y) { AutomationState state = AddState(AutomationType.MoveMouseImageOffset, CombinePath(path)); state.AddElement(x, y, null); return(state); }
public AutomationState AddClickableImage(string path, int clickCount) { AutomationState state = AddState(AutomationType.ClickableImage, CombinePath(path)); state.Value = clickCount; return(state); }
private void Click(AutomationState state, Rectangle referenceRect) { Click(state.X + referenceRect.X, state.Y + referenceRect.Y); }
private bool Run(bool secondAttempt) { Index = 0; Bitmap screenshot = null; Bitmap image = null; Stopwatch stopwatch = new Stopwatch(); string lastImagePath = null; Rectangle lastImageRect = Rectangle.Empty; try { screenshot = UpdateScreenshot(screenshot); Rectangle referenceRect = GetReferenceRect(screenshot); Rectangle startRect = GetStartRect(screenshot); if (referenceRect == Rectangle.Empty) { return(false); } // If we can't find a state spam escape and try again if (startRect == Rectangle.Empty) { if (secondAttempt) { return(false); } for (int i = 0; i < 10; i++) { SendKey(Keys.Escape); System.Threading.Thread.Sleep(50); } System.Threading.Thread.Sleep(1000); screenshot = UpdateScreenshot(screenshot); referenceRect = GetReferenceRect(screenshot); if (referenceRect == Rectangle.Empty) { System.Threading.Thread.Sleep(1000); } return(Run(true)); } AutomationState currentState = GetFirstState();//GetCurrentState(screenshot, referenceRect); if (currentState != null) { int index = states.IndexOf(currentState); for (int i = index; i < states.Count; i++) { Index = i; stopwatch.Stop(); stopwatch.Reset(); stopwatch.Start(); bool success = true; AutomationState state = states[i]; switch (state.Type) { case AutomationType.StartImage: break; case AutomationType.ReferenceImage: break; case AutomationType.Wait: if (state.Value > 0) { Thread.Sleep(state.Value); } break; case AutomationType.OpenImage: SendOpenImage(); break; case AutomationType.Copy: SendCopy(); break; case AutomationType.SelectAll: SendSelectAll(); break; case AutomationType.Text: SendKeys(state.Path); break; case AutomationType.TextReturn: SendKeys(state.Path); SendKey(Keys.Return); break; case AutomationType.Key: SendKey(state.Key); break; case AutomationType.AbsoluteClick: Click(state.X, state.Y); break; case AutomationType.WaitRelativeImage: case AutomationType.RelativeClick: success = false; for (int j = 0; j < attempts; j++) { screenshot = UpdateScreenshot(screenshot); if (UpdateReferenceImage) { referenceRect = GetReferenceRect(screenshot); } if (state.IsMatch(screenshot, referenceRect)) { success = true; break; } Thread.Sleep(attemptFailWait); if (stopwatch.Elapsed >= timeout) { break; } } if (success) { if (state.Type == AutomationType.RelativeClick) { Click(state, referenceRect); } } break; case AutomationType.MoveMouseImage: case AutomationType.MoveMouseImageOffset: case AutomationType.WaitImage: case AutomationType.ClickableImage: case AutomationType.ClickableImageOffset: DisposeBitmap(image); image = state.LoadImage(); if (image != null) { Rectangle rect = Rectangle.Empty; if (UseImageCaching && lastImagePath == state.Path && lastImageRect != Rectangle.Empty) { rect = lastImageRect; } else { for (int j = 0; j < attempts; j++) { screenshot = UpdateScreenshot(screenshot); rect = state.Find(screenshot, image); if (rect != Rectangle.Empty) { break; } Thread.Sleep(attemptFailWait); if (stopwatch.Elapsed >= timeout) { break; } } } success = rect != Rectangle.Empty; if (success) { lastImageRect = rect; lastImagePath = state.Path; int clickCount = state.Value <= 0 ? 1 : state.Value; for (int j = 0; j < clickCount; j++) { if (state.Type == AutomationType.ClickableImage) { Click(rect.Center()); } else if (state.Type == AutomationType.ClickableImageOffset) { Click(rect.X + state.X, rect.Y + state.Y); } else if (state.Type == AutomationType.MoveMouseImage) { MoveMouse(rect.Center()); } else if (state.Type == AutomationType.MoveMouseImageOffset) { MoveMouse(rect.X + state.X, rect.Y + state.Y); } } } } break; } if (!success) { return(false); } } } } finally { DisposeBitmap(screenshot); DisposeBitmap(image); } return(true); }
public AutomationState GetCurrentState(Bitmap screenshot, Rectangle referenceRect, bool imageOnly) { Point referencePos = Point.Empty; Bitmap image = null; try { AutomationState result = null; foreach (AutomationState state in states) { switch (state.Type) { //StartImage / ReferenceImage are special states and treated separate //case AutomationType.StartImage: //case AutomationType.ReferenceImage: case AutomationType.AbsoluteClick: case AutomationType.Wait: case AutomationType.OpenImage: case AutomationType.Text: case AutomationType.TextReturn: case AutomationType.Copy: case AutomationType.SelectAll: case AutomationType.Key: if (imageOnly) { continue; } return(state); case AutomationType.WaitRelativeImage: case AutomationType.RelativeClick: if (state.IsMatch(screenshot, referenceRect)) { return(state); } break; case AutomationType.MoveMouseImage: case AutomationType.MoveMouseImageOffset: case AutomationType.WaitImage: case AutomationType.ClickableImage: case AutomationType.ClickableImageOffset: try { DisposeBitmap(image); } catch { } try { image = (Bitmap)Bitmap.FromFile(state.Path); Rectangle rect = state.Find(screenshot, image); if (rect != Rectangle.Empty) { return(state); } } catch { } break; } } return(result); } finally { DisposeBitmap(image); } }