C#运行应用程序在Windows启动时最小化
题
我有以下代码在Windows Startup上运行该应用程序:
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
, ,然后您可以解析此参数并检测是否需要开始最小化。
其他提示
由于这仅将注册表密钥添加到软件 Microsoft Windows currentversion run,这会导致操作系统在启动时启动应用程序,除非您要启动的应用程序接受命令行参数,否则您无能为力。开始最小化(然后您可以将参数添加到键的可执行路径中)。
如果这是一个必要的函数,并且您无法修改程序以接受参数以最大程度地减少我唯一能想到的事情,那就是编写一个程序,该程序在OS启动后将这些应用程序最小化。
很难找到一个很好的答案,终于在一本非常古老的书中找到了它。在我的表单应用程序上,刚刚打开了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;
}
}
}
您的窗口课:
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);
}
例如,使用此功能,如果您使用 - 最小化的参数启动应用程序,则它将开始最小化,在任务栏中弹出通知,并且一个气泡说该应用程序在后台启动并运行。
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();
}
}