Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top