Duolk
Duolk

Reputation: 19

Moving the mouse in other application on C#

I'm trying to make a program that basically executes a mouse macro on other windows/apps in a given interval of time.

I've already managed create the timer, mapping and saving the 3 positions on screen that I need (using [DllImport("User32.Dll")] and GetCursorPos/SetCursorPos/mouse_event to do the left click) and even manage to do the mouse move on screen using LinearSmoothMove.

The problem is when I execute the function the mouse moves until it reaches the side of the window of this other app and then it "stops" (actually it looks like it's moving under the window). However, it works with other things like opening Notepad and going between the lines.

Upvotes: 1

Views: 1570

Answers (1)

Didgeridoo
Didgeridoo

Reputation: 1262

Please, see answer to similar question here. But before using ClickOnPointTool.ClickOnPoint() you should write the next code:

To find the certain process like this:

private IntPtr GetProcessMainWindowHandle(string mainWindowTitle)
{
    var processes = Process.GetProcesses();
    var foundProcess = processes.Single(p => mainWindowTitle.Equals(p.MainWindowTitle, StringComparison.CurrentCulture));
    // Also you can use method Process.GetProcessesByName(), it depends on your business logic
    return foundProcess.MainWindowHandle;
}

And to activate the found window using P/Invoke call of method SetForegroundWindow() with value of variable mainWindowHandle, recieved from GetProcessMainWindowHandle():

[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

Upvotes: 2

Related Questions