Develop a program that runs in the background in .NET?

后端 未结 6 1705
天涯浪人
天涯浪人 2020-12-04 08:53

I have made a small program in C# that I want to run in the background and it should only appear when a certain key combination is pressed. How can I do this?

6条回答
  •  一向
    一向 (楼主)
    2020-12-04 09:09

    A quick and dirty solution (I think the Window Service Application template is unavailable in Visual Studio Express and Standard):

    Start a new Windows Forms Application. Add a new class to the solution and write the code you want inside it.

    Go back to the form designer, set the WindowState property to Minimized, and add a Load event to the form. In the event handler hide the form and call your class:

    private void Form1_Load(object sender, EventArgs e)
    {
        this.Hide(); 
        MyNewClass mynewclass=new MyNewClass();
    } 
    

    The application doesn't appear in the taskbar and you don't see it when you hit Alt+Tab. You can add a systray icon to it if you want, just like magol wrote:

    NotifyIcon trayIcon = new NotifyIcon();
    trayIcon.Icon=new Icon(@"C:\iconfilename.ico");
    trayIcon.Visible = true;
    

提交回复
热议问题