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

我得到以下代码在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(); } } 

有用。 但我想让程序最小化(仅在Windows启动时)。 我没有找到工作代码/很好的解释如何做到这一点。 你能帮我吗?

谢谢。

你有没有尝试过

 this.WindowState = FormWindowState.Minimized; 

如果你想在Windows启动时启动最小化,你可以在命令行中添加额外的参数,比如myapp.exe --start-minimized ,然后你可以解析这个参数并检测你是否需要启动最小化。

由于这只是向SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run添加了一个注册表项,导致操作系统在启动时启动应用程序,除非您要启动的应用程序接受命令行参数,否则您无法做很多事情。 start最小化(然后可以将参数添加到密钥的可执行路径)。

如果这是一个必要的function,你不能修改程序接受一个参数,以尽量减少我能想到的唯一的事情是编写一个程序,在操作系统启动后最小化这些应用程序。

很难找到一个好的答案,终于在一本非常古老的书中找到了它。 在我的Forms应用程序上,刚打开program.cs并进行了更改

 Application.Run(new Form1()); 

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

它打开时没有直接闪烁到托盘上。 这个应用程序不仅仅是一个服务,所以将其设置为右键单击时只有一个通知图标和退出按钮。 希望这可以帮助!!

通常不会恢复旧线程但只有一种简单方法,包括最小化到系统托盘,对于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; } } } 

你的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; } 

第一次工作所以不得不张贴。 我正在使用WPF notifyicon,因此我需要它在Windows启动时转到系统托盘。

我遇到了同样的问题,找到了一个有效的解决方案:

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")); } } 

Form1.cs ,您可以使用传递的参数调用函数并最小化应用程序:

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

例如,使用我使用的此function,如果使用-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(); } }