简体   繁体   English

C#在Windows启动时运行应用程序MINIMIZED

[英]C# Run application MINIMIZED at windows startup

I got the following code to run the application at windows startup: 我在Windows启动时获得了以下代码来运行应用程序:

    private void SetStartup(string AppName, bool enable)
    {
        string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";

        Microsoft.Win32.RegistryKey startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey);

        if (enable)
        {
            if (startupKey.GetValue(AppName) == null)
            {
                startupKey.Close();
                startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
                startupKey.SetValue(AppName, Application.ExecutablePath.ToString());
                startupKey.Close();
            }
        }
        else
        {
            startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
            startupKey.DeleteValue(AppName, false);
            startupKey.Close();
        }
    }

It works. 有用。 but I want the program to start minimized (at windows startup only). 但我想让程序最小化(仅在Windows启动时)。 I didnt find a working code / good explanation how to do it. 我没有找到工作代码/很好的解释如何做到这一点。 Can you help me please? 你能帮我吗?

thanks. 谢谢。

Have you tried 你有没有尝试过

this.WindowState = FormWindowState.Minimized;

If you want to start minimized at windows startup only you can add extra argument to command line, like myapp.exe --start-minimized , then you can parse this parameter and detect whether you need to start minimized or not. 如果你想在Windows启动时启动最小化,你可以在命令行中添加额外的参数,比如myapp.exe --start-minimized ,然后你可以解析这个参数并检测你是否需要启动最小化。

Since this is only adding a registry key to SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run which causes the OS to start the app at startup there isn't a lot you can do unless the application you want to startup accepts a command line parameter to start minimized (You could then add the parameter to the executable path of the key). 由于这只是向SOFTWARE \\ Microsoft \\ Windows \\ CurrentVersion \\ Run添加了一个注册表项,导致操作系统在启动时启动应用程序,除非您要启动的应用程序接受命令行参数,否则您无法做很多事情。 start最小化(然后可以将参数添加到密钥的可执行路径)。

If this is a necessary function and you can't modify the program to accept a parameter to minimize the only thing I can think of doing would be to write a program that would minimize these apps after the OS has started them. 如果这是一个必要的功能,你不能修改程序接受一个参数,以尽量减少我能想到的唯一的事情是编写一个程序,在操作系统启动后最小化这些应用程序。

Had a really hard time finding a good answer to this, finally found it in a really old book. 真的很难找到一个好的答案,终于在一本非常古老的书中找到了它。 On my Forms application, just opened the program.cs and changed 在我的Forms应用程序上,刚打开program.cs并进行了更改

Application.Run(new Form1());

to

Form1 f = new Form1();
f.WindowState = FormWindowState.Minimized;
f.ShowInTaskbar = false;
Application.Run(f);

and it opens without a flicker directly to the tray. 它打开时没有直接闪烁到托盘上。 This app was more just a service, so set it to just have a notify icon and exit button when right clicked. 这个应用程序不仅仅是一个服务,所以将其设置为右键单击时只有一个通知图标和退出按钮。 Hope this helps!! 希望这可以帮助!!

I have strugled with the same issue, and found a working solution: 我遇到了同样的问题,找到了一个有效的解决方案:

In your program.cs , handle the parameter, and then pass that parameter to Form1 : program.cs ,处理参数,然后将该参数传递给Form1

static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    if (args.Length != 0){
        Application.Run(new Form1(args[0]));
    }
    else
    {
        Application.Run(new Form1("normalState"));
    }
}

In your Form1.cs , you can call a function with the passed parameter and minimize the app: Form1.cs ,您可以使用传递的参数调用函数并最小化应用程序:

public Form1(string parameter)
{
    InitializeComponent();
    MinimizeApp(parameter);
}

For example, with this function i used, if you start the application with the -minimized parameter, then it will start minimized, a notifyicon pops up in the taskbar and a bubble saying the app is started and running in the background. 例如,使用我使用的此功能,如果使用-minimized参数启动应用程序,则它将启动最小化,任务栏中会弹出一个notifyicon,并显示应用程序已启动并在后台运行的气泡。

public void MinimizeApp(string parameter)
{
    if (parameter == "-minimized")
    {
        this.WindowState = FormWindowState.Minimized;
        notifyIcon1.Visible = true;
        notifyIcon1.BalloonTipText = "Program is started and running in the background...";
        notifyIcon1.ShowBalloonTip(500);
        Hide();
    }

}

Don't normally revive old threads but one Easy way including minimize to system tray, for WPF like this: 通常不会恢复旧线程但只有一种简单方法,包括最小化到系统托盘,对于WPF,如下所示:

     public class EntryPoint
    {
        [STAThread]
        public static void Main(string[] args)
        {
            SingleInstanceManager manager = new SingleInstanceManager();
            manager.Run(args);
        }
    }

    public class SingleInstanceManager : WindowsFormsApplicationBase
    {
        SingleInstanceApplication app;

        public SingleInstanceManager()
        {
            this.IsSingleInstance = true;
        }

        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
        {
            app = new SingleInstanceApplication();
            app.Run();
            return false;
        }

        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
            base.OnStartupNextInstance(eventArgs);
            app.Activate();
        }
    }

    public class SingleInstanceApplication : Application
    {
        protected override void OnStartup(System.Windows.StartupEventArgs e)
        {
            base.OnStartup(e);

            bool startMinimized = false;
            for (int i = 0; i != e.Args.Length; ++i)
            {
                if (e.Args[i] == "/StartMinimized")
                {
                    startMinimized = true;
                }
            }

            MainWindow mainWindow = new MainWindow();
            if (startMinimized)
            {
                mainWindow.WindowState = WindowState.Minimized;
            }
            mainWindow.Show();
        }


        public void Activate()
        {
            this.MainWindow.Activate();
            this.MainWindow.WindowState = WindowState.Normal;
        }
    }
}

Your Window class: 你的Window类:

  public partial class MainWindow : Window
{
    private Window _window;

    public MainWindow()
    {
    InitializeComponent();

        SetStartup("AppName", true);
    }
 private void SetStartup(string AppName, bool enable)
    { 
        string runKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
        Microsoft.Win32.RegistryKey startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey); 
        if (enable)
        { 
            if (startupKey.GetValue(AppName) == null) 
            { 
                startupKey.Close(); 
                startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
                startupKey.SetValue(AppName, Assembly.GetExecutingAssembly().Location + " /StartMinimized");
                startupKey.Close(); 
            }
        } 
        else 
        {
            startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
            startupKey.DeleteValue(AppName, false); 
            startupKey.Close(); 
        }
    }

 private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        if (this.WindowState == System.Windows.WindowState.Minimized)
        {
            var minimized = (_window.WindowState == WindowState.Minimized);

            _window.ShowInTaskbar = !minimized;
        }
        else
            ShowInTaskbar = true;

    }

Worked first time so had to post. 第一次工作所以不得不发布。 I'm using WPF notifyicon, hence why i needed it to go to system tray on windows startup. 我正在使用WPF notifyicon,因此我需要它在Windows启动时转到系统托盘。

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

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