public void ToAbsolute() { // Init Point p = CreateStdPoint(); StretchedPoint sp = CreateStdStretchedPoint(); Window w = CreateStdWindow(); Coordinate c1 = new Coordinate(CoordinateType.Absolute, p); Coordinate c2 = new Coordinate(CoordinateType.Relative, p); Coordinate c3 = new Coordinate(sp); // Null argument Point a = c1.ToAbsolute(null); Assert.AreEqual(a.X, p.X); Assert.AreEqual(a.Y, p.Y); Assert.Catch<NullReferenceException>(() => c2.ToAbsolute(null)); Assert.Catch<NullReferenceException>(() => c3.ToAbsolute(null)); // Normal calls Point a1 = c1.ToAbsolute(w); Assert.AreEqual(a1.X, p.X); Assert.AreEqual(a1.Y, p.Y); Point r = c2.ToAbsolute(w); Assert.AreEqual(r.X, 700); Assert.AreEqual(r.Y, 700); Point s = c3.ToAbsolute(w); Assert.AreEqual(s.X, 696); Assert.AreEqual(s.Y, 620); }
private void Click(MouseActions action, Coordinate point, bool moveCursor) { ActivateIfNeeded(); int x = 0; int y = 0; MouseFlag absflag = MouseFlag.Empty; if(moveCursor) { SetCursorPosition(point); } else { Point pt = point.ToAbsolute(this); x = pt.X; y = pt.Y; absflag = MouseFlag.Absolute; } switch(action) { case MouseActions.LeftClick: { InputSimulator.SimulateClickPress(MouseFlag.LeftDown | absflag, MouseFlag.LeftUp | absflag, x, y); break; } case MouseActions.LeftDown: { InputSimulator.SimulateClick(MouseFlag.LeftDown | absflag, x, y); break; } case MouseActions.LeftUp: { InputSimulator.SimulateClick(MouseFlag.LeftUp | absflag, x, y); break; } case MouseActions.MiddleClick: { InputSimulator.SimulateClickPress(MouseFlag.MiddleDown | absflag, MouseFlag.MiddleUp | absflag, x, y); break; } case MouseActions.MiddleDown: { InputSimulator.SimulateClick(MouseFlag.MiddleDown | absflag, x, y); break; } case MouseActions.MiddleUp: { InputSimulator.SimulateClick(MouseFlag.MiddleUp | absflag, x, y); break; } case MouseActions.RightClick: { InputSimulator.SimulateClickPress(MouseFlag.RightDown | absflag, MouseFlag.RightUp | absflag, x, y); break; } case MouseActions.RightDown: { InputSimulator.SimulateClick(MouseFlag.RightDown | absflag, x, y); break; } case MouseActions.RightUp: { InputSimulator.SimulateClick(MouseFlag.RightUp | absflag, x, y); break; } case MouseActions.X1Click: { InputSimulator.SimulateClickPress(MouseFlag.XDown | absflag, MouseFlag.XUp | absflag, 1, x, y); break; } case MouseActions.X1Down: { InputSimulator.SimulateClick(MouseFlag.XDown | absflag, 1, x, y); break; } case MouseActions.X1Up: { InputSimulator.SimulateClick(MouseFlag.XUp | absflag, 1, x, y); break; } case MouseActions.X2Click: { InputSimulator.SimulateClickPress(MouseFlag.XDown | absflag, MouseFlag.XUp | absflag, 2, x, y); break; } case MouseActions.X2Down: { InputSimulator.SimulateClick(MouseFlag.XDown | absflag, 2, x, y); break; } case MouseActions.X2Up: { InputSimulator.SimulateClick(MouseFlag.XUp | absflag, 2, x, y); break; } default: { throw new ArgumentException("Invalid action.", "action"); } } }
protected double Radius(Coordinate center, Coordinate radiusPoint) { Point centerpt = center.ToAbsolute(this); Point radiuspt = radiusPoint.ToAbsolute(this); int diffx = centerpt.X - radiuspt.X; int diffy = centerpt.Y - radiuspt.Y; return Math.Sqrt(diffx * diffx + diffy * diffy); }
public virtual void SetCursorPosition(Coordinate point) { Point abs = point.ToAbsolute(this); System.Drawing.Point pt = new System.Drawing.Point(abs.X, abs.Y); Cursor.Position = pt; }
public Bitmap Screenshot(Coordinate topLeft, Coordinate bottomRight) { ActivateIfNeeded(); Point pt1 = topLeft.ToAbsolute(this); Point pt2 = bottomRight.ToAbsolute(this); Bitmap screenshot = new Bitmap(pt2.X - pt1.X, pt2.Y - pt1.Y, PixelFormat.Format32bppArgb); using(Graphics gdi = Graphics.FromImage(screenshot)) { gdi.CopyFromScreen(pt1.X, pt1.Y, 0, 0, new Size(pt2.X - pt1.X, pt2.Y - pt1.Y), CopyPixelOperation.SourceCopy); } return screenshot; }
public virtual void Resize(Coordinate rightBottom) { Point pt = rightBottom.ToAbsolute(Desktop.Primary); WinApi.SetWindowPos(Handle.Handle, IntPtr.Zero, 0, 0, pt.X, pt.Y, WinApi.SwpNoMove | WinApi.SwpNoZOrder); }
public virtual void Move(Coordinate position) { Point pt = position.ToAbsolute(Desktop.Primary); WinApi.SetWindowPos(Handle.Handle, IntPtr.Zero, pt.X, pt.Y, 0, 0, WinApi.SwpNoSize | WinApi.SwpNoZOrder); }
public Color GetPixelColor(Coordinate pixel) { ActivateIfNeeded(); Point pt = pixel.ToAbsolute(this); IntPtr hdc = WinApi.GetDC(IntPtr.Zero); uint colorint = WinApi.GetPixel(hdc, pt.X, pt.Y); WinApi.ReleaseDC(IntPtr.Zero, hdc); Color color = Color.FromArgb((int)(colorint & 0x000000FF), (int)(colorint & 0x0000FF00) >> 8, (int)(colorint & 0x00FF0000) >> 16); return color; }
public Coordinate FindColorInRectangle(Color color, Coordinate topLeft, Coordinate bottomRight) { ActivateIfNeeded(); using(Bitmap bmp = Screenshot(topLeft, bottomRight)) { for(int y = 0; y < bmp.Height; y++) for(int x = 0; x < bmp.Width; x++) { if(CompareColors(color, bmp.GetPixel(x, y))) { Point origin = topLeft.ToAbsolute(this); return new Coordinate(CoordinateType.Absolute, new Point() { X = origin.X + x, Y = origin.Y + y }); } } } return null; }
public Coordinate FindColorInCircle(Color color, Coordinate center, Coordinate radiusPoint) { ActivateIfNeeded(); Point centerpt = center.ToAbsolute(this); double radius = Radius(center, radiusPoint); int left = (int)(centerpt.X - radius); left = left < 0 ? 0 : left; int top = (int)(centerpt.Y - radius); top = top < 0 ? 0 : top; int right = (int)(centerpt.X + radius); right = right >= Width ? Width - 1 : right; int bottom = (int)(centerpt.Y + radius); bottom = bottom >= Height ? Height - 1 : bottom; using(Bitmap bmp = Screenshot( new Coordinate(CoordinateType.Absolute, new Point() { X = left, Y = top }), new Coordinate(CoordinateType.Absolute, new Point() { X = right, Y = bottom }))) { for(int y = 0; y < bmp.Height; y++) for(int x = 0; x < bmp.Width; x++) { if(CompareColors(color, bmp.GetPixel(x, y))) { if(IsInRadius(new Point() { X = x + left, Y = y + top }, centerpt, radius)) { return new Coordinate(CoordinateType.Absolute, new Point() { X = left + x, Y = top + y }); } } } } return null; }
public void WrongPoint() { StretchedPoint wrongP1 = new StretchedPoint {X = 100.0, Y = 0.0}; StretchedPoint wrongP2 = new StretchedPoint {X = 0.0, Y = 100.0}; StretchedPoint wrongP3 = new StretchedPoint {X = 100.0, Y = 200.0}; Coordinate c1 = new Coordinate(wrongP1); Coordinate c2 = new Coordinate(wrongP2); Coordinate c3 = new Coordinate(wrongP3); Window w = CreateStdWindow(); Assert.Catch<CoordinatesOutOfRangeException>(() => c1.ToAbsolute(w)); Assert.Catch<CoordinatesOutOfRangeException>(() => c2.ToAbsolute(w)); Assert.Catch<CoordinatesOutOfRangeException>(() => c3.ToAbsolute(w)); Assert.Catch<CoordinatesOutOfRangeException>(() => c1.ToRelative(w)); Assert.Catch<CoordinatesOutOfRangeException>(() => c2.ToRelative(w)); Assert.Catch<CoordinatesOutOfRangeException>(() => c3.ToRelative(w)); Assert.Catch<CoordinatesOutOfRangeException>(() => c1.ToStretched(w)); Assert.Catch<CoordinatesOutOfRangeException>(() => c2.ToStretched(w)); Assert.Catch<CoordinatesOutOfRangeException>(() => c3.ToStretched(w)); }
public void WrongCalls() { Window w = CreateStdWindow(); Coordinate c0 = new Coordinate(); Assert.Catch<InitializationException>(() => c0.ToAbsolute(null)); Assert.Catch<InitializationException>(() => c0.ToAbsolute(w)); Assert.Catch<InitializationException>(() => c0.ToRelative(null)); Assert.Catch<InitializationException>(() => c0.ToRelative(w)); Assert.Catch<InitializationException>(() => c0.ToStretched(null)); Assert.Catch<InitializationException>(() => c0.ToStretched(w)); }