views:

463

answers:

2

I've successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs when trying to start with the app minimized. At first the problem was that the app would be minimized but would still appear in the alt-tab dialog. Changing the FormBorderStyle to one of the ToolWindow options (from the "None" option) fixed this, but introduced another problem. When the app first starts the titlebar of the minimized window is visible just above the start menu:

Pic.

Opening the form and the closing it causes it to hide properly. I've tried lots of variations, but here's essentially how it's working right now...

WindowState is set to Minimized in the Designer. After some initialization in the constructor I have the following lines:

this.Visible = false;
this.ShowInTaskbar = false;

When the NotifyIcon is double-clicked I have the following:

 this.WindowState = FormWindowState.Normal;
 this.Visible = true;
 this.ShowInTaskbar = true;

Like I said, I've tried lots of minor variations on this (this.Hide(), etc.). Is there a way to have the NotifyIcon be the primary component such that I can completely start and dispose of the form while leaving the NotifyIcon running? There's got to be a way to start the app with the form minimized without any of the weirdness. Please help me find it!

+5  A: 

In the constructor, remove these two lines:

this.Visible = false;
this.ShowInTaskbar = false;

and add after InitializeComponent();:

this.WindowState = FormWindowState.Minimized;

In designer, set ShowInTaskbar to false & FormWindowState to Normal.

EDIT: If you post the same in Load event, the window does get minimized but still shows minimized on the desktop. I think this is a bug.

Yogesh
Or in the form's load event, since the constructor is designer-generated and you're apt to have your change disappear on you now and then.
Joel Coehoorn
I agree, load event would be a better place
Miles
See my edit. Your change will disappear only if you post the line I have posted before `InitializeComponent();`. All your designer settings are loaded in `InitializeComponent();`.
Yogesh
Okay, I moved the initialization code to the load event and made the suggested change. No dice. I still get the minimized title bar on the desktop, only now it doesn't disappear even after manually closing the form. Any other ideas?
jluce50
Re: EDIT: It does seem like bug, but there have got to be people out there doing what I'm trying to do. I'd think a bug like this would be getting a lot more attention...
jluce50
Let it remain in the constructor only!! Read my answer again.
Yogesh
@Joel - only InitializeComponent is designer-generated... not the entire constructor!
Philip Wallace
@Yogesh: I did exactly as you described and still have the same problem. As I mentioned in the OP, I can resolve it by changing the FormBorderStyle to something other than ToolWindow, but then I have the problem of it appearing in the alt-tab dialog even when minimized.
jluce50
+3  A: 

The right way to do this is to prevent the form from getting visible in the first place. That requires overriding SetVisibleCore(). Let's assume a context menu for the NotifyIcon with a Show and Exit command. You can implement it like this:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        notifyIcon1.ContextMenuStrip = contextMenuStrip1;
        this.showToolStripMenuItem.Click += showToolStripMenuItem_Click;
        this.exitToolStripMenuItem.Click += exitToolStripMenuItem_Click;
    }

    bool mAllowVisible;     // ContextMenu's Show command used
    bool mAllowClose;       // ContextMenu's Exit command used
    bool mLoadFired;        // Form was shown once

    protected override void SetVisibleCore(bool value) {
        if (!mAllowVisible) value = false;
        base.SetVisibleCore(value);
    }

    protected override void OnFormClosing(FormClosingEventArgs e) {
        if (!mAllowClose) {
            this.Hide();
            e.Cancel = true;
        }
        base.OnFormClosing(e);
    }

    private void showToolStripMenuItem_Click(object sender, EventArgs e) {
        mAllowVisible = true;
        mLoadFired = true;
        Show();
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
        mAllowClose = mAllowVisible = true;
        if (!mLoadFired) Show();
        Close();
    }
}

Note a wrinkle with the Load event, it won't fire until the main form is first shown. So be sure to do initialization in the form's constructor, not the Load event handler.

Hans Passant
Brilliant, thanks! Took a little tweaking to get it to work with my specific app, but it works great!
jluce50
Great solution Hans, Thanks.
Segfault