public void SetCursorPosition(int x, int y) { var openWindows = SystemWindow.AllOpenSystemWindows.ToList(); var topSystemWindow = openWindows.LastOrDefault(); if (windowToDrawSimulatedMouseOn != topSystemWindow) { if (windowToDrawSimulatedMouseOn != null) { windowToDrawSimulatedMouseOn.AfterDraw -= DrawMouse; } windowToDrawSimulatedMouseOn = topSystemWindow; if (windowToDrawSimulatedMouseOn != null && DrawSimulatedMouse) { windowToDrawSimulatedMouseOn.AfterDraw += DrawMouse; } } foreach (var systemWindow in openWindows) { currentMousePosition = new Point2D(x, y); Point2D windowPosition = AutomationRunner.ScreenToSystemWindow(currentMousePosition, systemWindow); if (LeftButtonDown) { var aggEvent = new MouseEventArgs(MouseButtons.Left, 0, windowPosition.x, windowPosition.y, 0); UiThread.RunOnIdle(() => { systemWindow.OnMouseMove(aggEvent); systemWindow.Invalidate(); }); } else { var aggEvent = new MouseEventArgs(MouseButtons.None, 0, windowPosition.x, windowPosition.y, 0); UiThread.RunOnIdle(() => { systemWindow.OnMouseMove(aggEvent); systemWindow.Invalidate(); }); } } }
public void CreateMouseEvent(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo) { // Send mouse event into the first SystemWindow (reverse order) containing the mouse foreach (var systemWindow in SystemWindow.AllOpenSystemWindows.Reverse().ToList()) { Point2D windowPosition = AutomationRunner.ScreenToSystemWindow(currentMousePosition, systemWindow); if (systemWindow.LocalBounds.Contains(windowPosition)) { MouseButtons mouseButtons = MapButtons(dwFlags); if (dwFlags == MouseConsts.MOUSEEVENTF_LEFTDOWN) { this.ClickCount = (this.LeftButtonDown) ? 2 : 1; UiThread.RunOnIdle(() => { systemWindow.OnMouseDown(new MouseEventArgs(mouseButtons, this.ClickCount, windowPosition.x, windowPosition.y, 0)); systemWindow.Invalidate(); }); // Stop processing after first match break; } else if (dwFlags == MouseConsts.MOUSEEVENTF_LEFTUP) { // send it to the window UiThread.RunOnIdle(() => { systemWindow.OnMouseUp(new MouseEventArgs(mouseButtons, 0, windowPosition.x, windowPosition.y, 0)); systemWindow.Invalidate(); }); // Stop processing after first match break; } else if (dwFlags == MouseConsts.MOUSEEVENTF_RIGHTDOWN) { this.ClickCount = (this.RightButtonDown) ? 2 : 1; UiThread.RunOnIdle(() => { systemWindow.OnMouseDown(new MouseEventArgs(mouseButtons, this.ClickCount, windowPosition.x, windowPosition.y, 0)); systemWindow.Invalidate(); }); // Stop processing after first match break; } else if (dwFlags == MouseConsts.MOUSEEVENTF_RIGHTUP) { // send it to the window UiThread.RunOnIdle(() => { systemWindow.OnMouseUp(new MouseEventArgs(mouseButtons, 0, windowPosition.x, windowPosition.y, 0)); systemWindow.Invalidate(); }); // Stop processing after first match break; } else if (dwFlags == MouseConsts.MOUSEEVENTF_MIDDLEDOWN) { this.ClickCount = (this.MiddleButtonDown) ? 2 : 1; UiThread.RunOnIdle(() => { systemWindow.OnMouseDown(new MouseEventArgs(mouseButtons, this.ClickCount, windowPosition.x, windowPosition.y, 0)); systemWindow.Invalidate(); }); // Stop processing after first match break; } else if (dwFlags == MouseConsts.MOUSEEVENTF_MIDDLEUP) { // send it to the window UiThread.RunOnIdle(() => { systemWindow.OnMouseUp(new MouseEventArgs(mouseButtons, 0, windowPosition.x, windowPosition.y, 0)); systemWindow.Invalidate(); }); // Stop processing after first match break; } } } this.LeftButtonDown = (dwFlags == MouseConsts.MOUSEEVENTF_LEFTDOWN); this.MiddleButtonDown = (dwFlags == MouseConsts.MOUSEEVENTF_MIDDLEDOWN); this.RightButtonDown = (dwFlags == MouseConsts.MOUSEEVENTF_RIGHTDOWN); }