简体   繁体   中英

C# - Hiding mouse cursor in program

I have been looking for resource for this issue but could not find what I want. I have a C# application in WPF which uses touch screen with mouse cursor present on the screen for users. The problem is I want to hide this mouse cursor but when I run the program in back end, the application should have mouse cursor displayed.

Does anyone have any good suggestion on where to start?

It's very simple:

if(runningAsClient)
    Cursor.Hide();

Of course you need a technique to determine the system you're running on, like a preprocessor directive. You can put this in your main method and set the directive in the client project configuration.

#if CLIENT
    Cursor.Hide();

If you use WPF, you have to set the Cursor property of your window to

Cursor = Cursors.None;

I suggest you to use command line arguments to achieve this:

public static class Program
{
    public static void Main(String[] args)
    {
        Boolean backend = args.Contains("-b");

        // ...

        MyApp app = new MyApp(backend);
        app.Run();
    }
}

public partial class MyApp : Application
{
    public MyApp(Boolean backend)
    {
        InitializeComponent();

        if (backend)
            Cursor = Cursors.None;
    }
}

To launch your app as "backend", just use the following command:

MyProgram.exe -b

and the mouse cursor will be hidden.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM