Block cursor movement during automation run

197 Views Asked by At

I am writing some Automation tests using Teststack white framework (C#, .net) for my WPF application. I want any cursor movement to be frozen while the tests are running. Is there any way to do that?

I already tried

public partial class NativeMethods
{
    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool BlockInput(bool fBlockIt);
}

but it is not working as my application is not running with Admin rights

I tried NativeMethods.BlockInput(true);

I am getting access denied exception

1

There are 1 best solutions below

0
Justin CI On

I think you can use ClipCursor

[DllImport("user32.dll")]
    static extern void ClipCursor(ref System.Drawing.Rectangle rect);

    [DllImport("user32.dll")]
    static extern void ClipCursor(IntPtr rect);

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        this.Loaded += MainWindow_Loaded;
        this.MouseMove += Window_MouseMove;
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        this.WindowState = WindowState.Maximized;
        HideMouse();
    }
    private void Window_MouseMove(object sender, MouseEventArgs e)
    {
        HideMouse();
    }

    private void HideMouse()
    {
        System.Drawing.Rectangle r = new System.Drawing.Rectangle(0, 0, 0, 0);
        ClipCursor(ref r);
    }