我在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启动时)。 我没有找到工作代码/很好的解释如何做到这一点。 你能帮帮我吗?
感谢。
答案 0 :(得分:12)
你试过吗
this.WindowState = FormWindowState.Minimized;
如果你想在Windows启动时启动最小化,你可以在命令行中添加额外的参数,比如myapp.exe --start-minimized
,然后你可以解析这个参数并检测你是否需要开始最小化。
答案 1 :(得分:1)
由于这只是向SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run添加一个注册表项,导致操作系统在启动时启动应用程序,除非您要启动的应用程序接受命令,否则您无法做很多事情最小化的行参数(然后可以将参数添加到键的可执行路径中)。
如果这是一个必要的功能,你不能修改程序接受一个参数来最小化我能想到的唯一的事情就是编写一个程序,在操作系统启动它们之后将这些应用程序最小化。 / p>
答案 2 :(得分:0)
通常不会恢复旧线程但只有一种简单的方法,包括最小化到系统托盘,对于这样的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启动时转到系统托盘。
答案 3 :(得分:0)
真的很难找到一个好的答案,终于在一本非常古老的书中找到了它。在我的Forms应用程序上,刚刚打开了program.cs并更改了
Application.Run(new Form1());
到
Form1 f = new Form1();
f.WindowState = FormWindowState.Minimized;
f.ShowInTaskbar = false;
Application.Run(f);
它打开时没有直接闪烁到托盘。这个应用程序不仅仅是一个服务,所以将其设置为右键单击时只有一个通知图标和退出按钮。希望这会有所帮助!!
答案 4 :(得分:0)
I have strugled with the same issue, and found a working solution:
In your private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
string[] charCombinations = new string[2];
charCombinations[0] = "sin(";
charCombinations[1] = "cos(";
string txt = this.textBox1.Text;
int caretLocation = this.textBox1.SelectionStart;
if (e.KeyCode == Keys.Delete)
{
//get text in front
string s = txt.Substring(caretLocation);
string notChecking = txt.Substring(0, caretLocation);
foreach (string combo in charCombinations)
{
if (s.StartsWith(combo))
{
txt = notChecking + s.Substring(combo.Length - 1);
break;
}
}
}
if (e.KeyCode == Keys.Back)
{
//get text in behind
string s = txt.Substring(0, caretLocation);
string notChecking = txt.Substring(caretLocation);
foreach (string combo in charCombinations)
{
if (s.EndsWith(combo))
{
txt = s.Substring(0, s.Length - combo.Length + 1) + notChecking;
caretLocation -= combo.Length - 1;
break;
}
}
}
this.textBox1.Text = txt;
//changing the txt feild will reset caret location
this.textBox1.SelectionStart = caretLocation;
}
, handle the parameter, and then pass that parameter to program.cs
:
Form1
In your 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"));
}
}
, you can call a function with the passed parameter and minimize the app:
Form1.cs
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.
public Form1(string parameter)
{
InitializeComponent();
MinimizeApp(parameter);
}