7

I have an application that I do not want to show in the taskbar. When the application is minimized it minimizes to the SysTray.

The problem is, when I set ShowInTaskbar = false the minimized application shows above the task bar, just aboe the Windows 7 start button. If I set ShowInTaskbar = true the application minimizes correctly, but obviously the application shows in the taskbar.

Any idea why this is happening and how I can fix it?

Failure to minimize

EDIT: For the sake of clarity, here is the code I'm using:

private void Form1_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized) {                                                                                    
                this.Hide(); 
                this.Visible = false;         
                notifyIcon1.Visible = true;           
            }
            else
            {
                notifyIcon1.Visible = false; 
            }
        }

    private void btnDisable_Click(object sender, EventArgs e)
    {

        // Minimize to the tray
        notifyIcon1.Visible = true;
        WindowState = FormWindowState.Minimized; // Minimize the form
    }
mack
  • 2,715
  • 8
  • 40
  • 68
  • possible duplicate of [How to start WinForm app minimized to tray?](http://stackoverflow.com/questions/1730731/how-to-start-winform-app-minimized-to-tray) – Mike Perrenoud Jul 16 '13 at 16:21
  • Okay, it was a shot in the ark but I deleted the Form_Resize event, ran the app, then added the resize event back (using the same code) and it seems to work now. – mack Jul 16 '13 at 18:42

5 Answers5

4

My guess is that you also have to hide the window. To get this behavior in WPF, I have to do the following:

WindowState = WindowState.Minimized;
Visibility = Visibility.Hidden;
ShowInTaskbar = false;

Since WPF and WinForms both ultimately come down to Win32 windows, you probably have to do the same thing.

17 of 26
  • 27,121
  • 13
  • 66
  • 85
3

There is no event handler to establish when a form's minimise request has been fired. So to 'get in' before the form has been minimised you'll have to hook into the WndProc procedure

private const int WM_SYSCOMMAND = 0x0112;
private const int SC_MINIMIZE = 0xF020; 

[SecurityPermission(SecurityAction.LinkDemand, 
                    Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override void WndProc(ref Message m)
{
    switch(m.Msg)
    {
        case WM_SYSCOMMAND:
            int command = m.WParam.ToInt32() & 0xfff0;
            if (command == SC_MINIMIZE && this.minimizeToTray)
            {
                PerformYourOwnOperation();  // For example
            }
            break;
    }
    base.WndProc(ref m);
}

and then you can merely hide the form in the PerformYourOwnOperation(); method

public void PerformYourOwnOperation()
{
    Form form = GetActiveForm();
    form.Hide();
}

You will then need some mechanism to Show() the hidden form otherwise it will be lost.

Another way is using the the form's resize event. However, this fires after the form is minimised. To do this you can do something like

private void Form_Resize(object sender, EventArgs e)
{
    if (WindowState == FormWindowState.Minimized)
    {
        // Do some stuff.
    }
}

I hope this helps.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • 1
    Thanks Kc. I used the Form_Resize event. I had to cut the event, run the app, then add the event back and it works. – mack Jul 16 '13 at 18:44
2

Here is one of the simplest solutions (I think so):

//Deactivate event handler for your form.
private void Form1_Deactivate(object sender, EventArgs e)
{
   if (WindowState == FormWindowState.Minimized) Hide();
}
King King
  • 61,710
  • 16
  • 105
  • 130
1

You can use a NotifyIcon to get the program to show up in the system tray, and watch for the window's resize event to toggle the visibility to hidden.

Here is how to watch for the window's resize event.

How to detect when a windows form is being minimized?

Here is a tutorial for using the NotifyIcon provided by CodeProject. The NotifyIcon is a windows forms element so it will still work in your application.

http://www.codeproject.com/Articles/36468/WPF-NotifyIcon

Community
  • 1
  • 1
0

Add handler for resizing:

private void Form1_Resize(object sender, EventArgs e)
{
    Visible = WindowState != FormWindowState.Minimized;
}