1

I am developing an application that starts minimized and shown at system tray with a notify icon.

When application is running and I minimize it, form runs the Hide() method in Resize event to hide the window and it works (window is hidden from taskbar and shown the notify icon at system tray) . The problem is when the application is starting up. It is configured to run minimized.

When it starts up, the system tray icon appears and the window appears minimized, but it is shown in taskbar.

What is the problem?

This is the Resize event:

    private void frmMain_Resize(object sender, EventArgs e)
    {
        if (this.WindowState == FormWindowState.Minimized)
        {
            Hide();
            notifyIcon.Visible = true;
            notifyIcon.ShowBalloonTip(200);
        }
    }
jstuardo
  • 3,901
  • 14
  • 61
  • 136

2 Answers2

1

Try this

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            HideWindow();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            HideWindow();
        }

        private void HideWindow()
        {
            if (this.Visible == true)
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    this.Hide();
                }
            }
        }
    }
}
Marc
  • 973
  • 4
  • 13
0

I'd try putting all of that code in Load event instead of Resize:

private void frmMain_Load(object sender, EventArgs e)
{
    if (this.WindowState == FormWindowState.Minimized)
    {
        Hide();
        notifyIcon.Visible = true;
        notifyIcon.ShowBalloonTip(200);
    }
}

Resize event is only called when the app is resized somehow manually (I understand that's why it works when you click on the minimize button but it doesn't on startup).

peter kover
  • 77
  • 2
  • 10
  • He does not wan't it just hidden on load, he wants it hidden any time he minimizes the window, including the first time the window loads when it starts in the minimized state. Your code handles the first launch case but it does not handle any minimizings after the first one. – Scott Chamberlain Apr 20 '16 at 13:38
  • as per OP's question: Hide() does not work when application is starting up. Using the code I provided Hide() will now work when app is starting. I guess OP can simply create an event that gets fired both on app startup and app resize and handle it from there. – peter kover Apr 20 '16 at 13:39
  • No... resize event is triggered at startup also. However, I have found where the problem is. At startup Visible property is false when the form is minimized automatically, so the Hide has no effect. So I have solved it by using the Shown event. If there is a notify icon, I call the Hide again. That event is only triggered when the form is first shown. – jstuardo Apr 20 '16 at 13:40
  • Yes, the OP's question is about first opening but he also says *"When application is running and I minimize it, form runs the Hide() method in Resize event to hide the window and it works"*, if he "[put] all of that code in Load event **instead of Resize**" you would fix the question he asked but would have broken the other behavior. – Scott Chamberlain Apr 20 '16 at 13:44
  • Please don't quote only half of my discourse as I also wrote "I guess OP can simply create an event that gets fired both on app startup and app resize and handle it from there" which would handle all of that just fine. I'm not able to find information about resize event being triggered at startup too but I remember I had some trouble in the past with the visible property so I guess that's it. – peter kover Apr 20 '16 at 13:46