C# - 在系统启动时最小化到托盘

时间:2009-10-19 23:00:49

标签: c# startup system-tray minimize

在我的应用程序中,我可以选择在Windows启动时启动应用程序。这很好用。我也有它,以便在最小化时,应用程序最小化到系统托盘。有没有办法让我在与Windows同时启动时自动最小化?我能想到的唯一方法是检索系统所处的时间,并使用该数据来确定机器最近是否已启动。显然,这个理论存在很多缺陷。任何人都有任何其他想法如何做到这一点?

4 个答案:

答案 0 :(得分:7)

在程序中实现命令行开关,使程序最小化到托盘。当您使用Windows启动程序启动程序时,只需包含开关。

http://msdn.microsoft.com/en-us/library/acy3edy3.aspx

答案 1 :(得分:3)

在窗体中,WindowState中的“属性”更改为“最小化”或代码:

     //After this:
     InitializeComponent();
     //Place this line:
     WindowState = FormWindowState.Minimized;

希望这有帮助!

答案 2 :(得分:1)

使用命令行参数,例如/ startminimised。在您的应用中,在应用启动时检查是否存在此开关(使用Environment.GetCommandLineArgs),并在交换机存在时自动最小化。

然后在“启动时运行”选项中,确保使用此开关启动应用,例如将Run注册表项或Startup组快捷方式设置为myapp.exe /startminimised

但是,当用户运行您的应用时,他们不会(通常!)指定开关,因此该应用将显示为一个窗口。

答案 3 :(得分:0)

You can call your program with a parameter, for example "-minimized" and then handle that parameter in your program:

In your program.cs, handle the parameter, and then pass that parameter to Form1:

<div class="left">

    <div class="holder">

        <div class="tweet" data-pos="0">
            <div class="avatar">
                <img class="user" src="https://pbs.twimg.com/profile_images/478523550886658048/rLUgKkv7_normal.png" boder="0">
                <div class="mask"><img src="/theme/images/avatar_mask.png" boder="0"></div>
            </div>
            <div class="text">
                <div class="username">Software AG Tu00fcrkiye <small>@SoftwareAGTR</small></div>
                <div class="tweet">TEXT</div>
            </div>
        </div>

        <div class="tweet" data-pos="0">
            <div class="avatar">
                <img class="user" src="https://pbs.twimg.com/profile_images/478523550886658048/rLUgKkv7_normal.png" boder="0">
                <div class="mask"><img src="/theme/images/avatar_mask.png" boder="0"></div>
            </div>
            <div class="text">
                <div class="username">Software AG Tu00fcrkiye <small>@SoftwareAGTR</small></div>
                <div class="tweet">TEXT!</div>
            </div>
        </div>

        <div class="tweet" data-pos="0">
            <div class="avatar">
                <img class="user" src="https://pbs.twimg.com/profile_images/528538190798675969/La7toYrv_normal.jpeg" boder="0">
                <div class="mask"><img src="/theme/images/avatar_mask.png" boder="0"></div>
            </div>
            <div class="text">
                <div class="username">Ece Vahapoglu <small>@ecevahapoglu</small></div>
                <div class="tweet">text https://t.co/JDfP6ATMWk</div>
            </div>
        </div>

        <div class="tweet" data-pos="0">
            <div class="avatar">
                <img class="user" src="https://pbs.twimg.com/profile_images/581787648584511488/Kxy-mZGu_normal.jpg" boder="0">
                <div class="mask"><img src="/theme/images/avatar_mask.png" boder="0"></div>
            </div>
            <div class="text">
                <div class="username">Fatih Mert Esmer <small>@mertesmer</small></div>
                <div class="tweet">#DASummit15 http://t.co/mpnBIh8zJK</div>
            </div>
        </div>

        <div class="tweet" data-pos="0">
            <div class="avatar">
                <img class="user" src="https://pbs.twimg.com/profile_images/475291049724620800/TmAbgWKF_normal.jpeg" boder="0">
                <div class="mask"><img src="/theme/images/avatar_mask.png" boder="0"></div>
            </div>
            <div class="text">
                <div class="username">Agah Alptekin <small>@AgahAlptekin</small></div>
                <div class="tweet">RT @digitalage: TEXT ://t.…</div>
            </div>
        </div>
    </div>
</div>

In your Form1.cs, you can call a function with the passed parameter and minimize the app:

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

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();
        SetStartup(); //This function will set your app in the registry to run on startup. I'll explain this function below.
        MinimizeApp(parameter);
    }

The SetStartup function puts your program into the registry, so it'll run on startup.

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

}

Right now, when you start your program with -minimized parameter, for example: "c:/programs/app.exe" -minimized then it will start minimized, and when you restart your computer, it also starts automatically minimized.