简体   繁体   English

C#-具有GUI的后台应用程序

[英]C# - Background application with GUI

My problem is that I want to create a background application but with a user interface that could be restored and minimized to the system tray and it starts with windows. 我的问题是我想创建一个后台应用程序,但是要使用一个可以还原并最小化到系统托盘的用户界面,并且它以Windows开头。 I tried searching how to start but I only found threads about Windows Service without UI or creating form and hiding it. 我尝试搜索如何启动,但是我只找到有关Windows Service的线程,而没有UI或创建表单并将其隐藏。 So my question is how should I start ? 所以我的问题是我应该如何开始? A Windows Form ? Windows窗体? A Service and add an interface somehow ? 服务并以某种方式添加接口?

Thank you! 谢谢!

Creating a form and hiding it is the way to go if you want your application to start when the user logs in. 如果要在用户登录时启动应用程序,则创建并隐藏表单是一种方法。

If you want your code to run even if no user is logged-in, then you will require a windows service. 如果即使没有用户登录也要运行代码,则需要Windows服务。

If you choose to go down that road, you most likely will want to have an application that, somehow, interacts with your service. 如果您选择走这条路,那么您很可能希望拥有一个以某种方式与您的服务交互的应用程序。

Here's a way to create an app that isn't tied to a Form. 这是一种创建与表单无关的应用程序的方法。 Use a Standard WinForms Project, but pass your custom implementation of ApplicationContext to Application.Run() : 使用标准WinForms项目,但将您自定义的ApplicationContext实现传递给Application.Run()

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyContext());
    }

}

public class MyContext : ApplicationContext
{

    public MyContext()
    {
        // this is the new "entry point" for your application

        // use Application.Exit() to shut down the application

        // you can still create and display a NotifyIcon control via code for display in the tray
        // (the icon for the NotifyIcon can be an embedded resource)
        // you can also display forms as usual when necessary
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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