質問

I am trying to minimize my winapp to system tray. I have downloaded a sample project from codeproject. But it goes to systary on Form.Resize event. Code -

    private void Form_Resize(object sender, EventArgs e)
    {
        notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
        notifyIcon1.BalloonTipText = "You have successfully minimized your form.";

        if (FormWindowState.Minimized == this.WindowState)
        {
            notifyIcon1.Visible = true;
            notifyIcon1.ShowBalloonTip(500);
            this.Hide();    
        }
        else if (FormWindowState.Normal == this.WindowState)
        {
            notifyIcon1.Visible = false;
        }
    }

Is it necessary to handle it on resize event? Can i do it on button click event?

役に立ちましたか?

解決

You can do this in your button. For obvious reasons you cannot rely on the WindowState in your button, because it can only be clicked when the window is no minimized to tray anyway.

private void button1_Click(object sender, EventArgs e)
{
    notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
    notifyIcon1.BalloonTipText = "You have successfully minimized your form.";

    notifyIcon1.Visible = true;
    notifyIcon1.ShowBalloonTip(500);
    this.Hide();
} 

This should work to "minimize" to tray. Although it should really be called hide-on-button-click-to-tray.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top