0

My application is an automation for another app.

I want to move the cursor to a specific position on the screen. The problem i am having is that when the wpf application isn't the foreground application, the cursor doesn't move. So, basically, the app i am trying to make an automation to is displayed to the user and i want to have a process in the background that will move the cursor automatically.

Process[] processes = Process.GetProcessesByName("notepad");
        foreach (Process proc in processes)
        {
            IntPtr handle = proc.MainWindowHandle;
            RECT Rect = new RECT();

            GetWindowRect(handle, ref Rect);
            if (Rect.left == 0 || Rect.right == 0 || Rect.top == 0 || Rect.bottom == 0)
                continue;
            Int32 Y = Rect.top + 50;
            Int32 X = (Rect.right - Rect.left) / 2;

            POINT p = new POINT();
            p.x = X;
            p.y = Y;

            ClientToScreen(handle, ref p);
            SetCursorPos(p.x, p.y);

        }

It only works if the wpf application is the app that is currently displayed on the screen.

HakoStan
  • 69
  • 2
  • 10
  • 1
    You need to use P/Invoke and call SetCursorPos. – Gusman Oct 23 '16 at 19:47
  • @Gusman i did dllimport but the mouse doesnt move if the application isnt in the foreground display – HakoStan Oct 23 '16 at 19:55
  • 1
    http://stackoverflow.com/questions/18849421/how-to-move-the-cursor-or-simulate-clicks-for-other-applications – Gusman Oct 23 '16 at 19:56
  • The correct answer is System.Windows.Automation, unless you are automating certain VCL applications. Are you? – andlabs Oct 23 '16 at 20:44
  • Thanks, I added the following line to the application manifest and it worked. didn't knew that i needed Administrator privileges – HakoStan Oct 23 '16 at 21:23
  • You **don't** need administrator privileges, if you do it the right way, i.e. UI Automation. – IInspectable Oct 23 '16 at 23:58
  • Tnks, I'll try using it but i am skeptic about it. As I understood it is a library that implements the Win32 and makes it easy to create an automation over another *windows* application. The problem is that the other application doesn't have for example a normal windows button. Correct me if i am wrong. If you have any good tutorial to offer about UI Automation, it would be nice. Tnks. – HakoStan Oct 24 '16 at 14:21
  • 1
    [UI Automation Overview](https://msdn.microsoft.com/en-us/library/ms747327.aspx) is a good introduction. And your assumption is wrong: The accessibility interfaces are unrelated to the visual representation or implementation of controls. Qt widgets, for example, implement accessibility interfaces (even though they aren't backed by native `HWND`s). A quick way to find out, whether an application exposes the required automation interfaces is through the [Inspect](https://msdn.microsoft.com/en-us/library/windows/desktop/dd318521.aspx) tool. – IInspectable Oct 24 '16 at 15:38
  • Thanks a lot. Going to read it ! – HakoStan Oct 24 '16 at 17:04

0 Answers0