|
using System; |
|
using System.Diagnostics; |
|
using System.Linq; |
|
using System.Runtime.InteropServices; |
|
using System.Threading; |
|
using System.Drawing; |
|
using System.Windows; |
|
using Point = System.Drawing.Point; |
|
|
|
namespace GremlinsForWindowsPhone |
|
{ |
|
public partial class MainWindow : Window |
|
{ |
|
public MainWindow() |
|
{ |
|
InitializeComponent(); |
|
} |
|
|
|
private void Button_Click(object sender, RoutedEventArgs e) |
|
{ |
|
Process[] processlist = Process.GetProcesses(); |
|
var process = processlist.FirstOrDefault(p => p.MainWindowTitle.Contains("Emulator")); |
|
if (process == null) |
|
return; |
|
|
|
HandleRef handleRef = new HandleRef(this, process.MainWindowHandle); |
|
|
|
var windowBounds = GetWindowRectangle(handleRef); |
|
SetForegroundWindow(process.MainWindowHandle); |
|
|
|
ClickOnPoint(new Point(windowBounds.X + 100, windowBounds.Y + 100)); |
|
ClickOnPoint(new Point(windowBounds.X + 200, windowBounds.Y + 200)); |
|
Thread.Sleep(4000); |
|
ClickOnPoint(new Point(windowBounds.X + 300, windowBounds.Y + 200)); |
|
} |
|
|
|
|
|
[System.Runtime.InteropServices.DllImport("user32.dll")] |
|
public static extern bool SetForegroundWindow(IntPtr hWnd); |
|
|
|
[StructLayout(LayoutKind.Sequential)] |
|
public struct RECT |
|
{ |
|
public int Left; // x position of upper-left corner |
|
public int Top; // y position of upper-left corner |
|
public int Right; // x position of lower-right corner |
|
public int Bottom; // y position of lower-right corner |
|
} |
|
[DllImport("user32.dll")] |
|
[return: MarshalAs(UnmanagedType.Bool)] |
|
static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect); |
|
|
|
[Flags] |
|
enum MouseEventFlag : uint |
|
{ |
|
Move = 0x0001, |
|
LeftDown = 0x0002, |
|
LeftUp = 0x0004, |
|
RightDown = 0x0008, |
|
RightUp = 0x0010, |
|
MiddleDown = 0x0020, |
|
MiddleUp = 0x0040, |
|
XDown = 0x0080, |
|
XUp = 0x0100, |
|
Wheel = 0x0800, |
|
VirtualDesk = 0x4000, |
|
Absolute = 0x8000 |
|
} |
|
|
|
[DllImport("user32.dll")] |
|
static extern bool SetCursorPos(int X, int Y); |
|
|
|
[DllImport("user32.dll")] |
|
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo); |
|
|
|
public static Rectangle GetWindowRectangle(HandleRef windowHandleRef) |
|
{ |
|
Rectangle myRect = new Rectangle(); |
|
|
|
RECT rct; |
|
|
|
if (!GetWindowRect(windowHandleRef, out rct)) |
|
{ |
|
throw new ApplicationException("Oups"); |
|
} |
|
myRect.X = rct.Left; |
|
myRect.Y = rct.Top; |
|
myRect.Width = rct.Right - rct.Left + 1; |
|
myRect.Height = rct.Bottom - rct.Top + 1; |
|
|
|
return myRect; |
|
} |
|
|
|
private static void ClickOnPoint(Point clientPoint) |
|
{ |
|
SetCursorPos((int) clientPoint.X, (int) clientPoint.Y); |
|
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero); |
|
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero); |
|
} |
|
} |
|
} |