Drumnbass
Drumnbass

Reputation: 924

How to make a form visible by click in its windows tray icon

In somewhere of this page I read that the best way to "override" the minimize method is to use onResize() event. I've done it and it works! I've coded that when the form is minimized it turns no visible and put an icon in the windows tray. As yet everything is working fine, but I've also programmed (or tried it at least) that when the icon in windows tray is clicked, the form turns visible again, but it doesn't work and I don't know why.

I've tried to code both events (Click and MouseClick), but the code is the same in both and it still doesn't work, so I must be doing something wrong, but obviusly I don't know what.

Remember that what I want is to code the click event on windows tray icon, not on task bar icon, so maybe that's why it isn't working, maybe is another event.

Here is my code:

private void onResize(object sender, EventArgs e)
    {
        this.ShowInTaskbar = false;
        notifyIcon1.Visible = true;
        this.Visible = false;
    }

    private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
    {
        this.ShowInTaskbar = true;
        this.Visible = true;
    }

    private void notifyIcon_Click(object sender, EventArgs e)
    {
        this.ShowInTaskbar = true;
        this.Visible = true;
    }

Thanks.

EDIT: the problem was that I just assigned the image to the icon on the Form constructor like notifyIcon1.Icon = new Icon("greenCircle.ico"); but I didn't initialize any image on the notifyIcon properties... By initializing the image on notifyIcon properties it worked fine!

Upvotes: 0

Views: 2404

Answers (2)

Hans Passant
Hans Passant

Reputation: 942408

    this.ShowInTaskbar = true;
    this.Visible = true;

This doesn't do what you hope it does. A tricky problem in Windows that has bedeviled many programmers. The ShowInTaskbar property is "special", very unlikely most other Form properties. It is specified in the underlying CreateWindowEx() winapi function as a style bit. There are other properties like that, like FormBorderStyle, ControlBox, LeftToRight. Each corresponding with a style bit.

Trouble is, changing the property requires Winforms to perform considerable gymnastics, it has the destroy the native window and re-create it again. That tends to have side-effects, you found one.

Just swap the two statements and you'll see it working very differently, now it does manage to make the window visible again. But still not smoothly, it doesn't always manage to get into the foreground.

You might be puzzled at what looks like a massive bug. The real problem is that you are doing something that most other programmers never do, always a good way to get a problem that nobody else has. You don't actually have to change the ShowInTaskbar property at all. The taskbar button is already automatically invisible when you hide your window, no need to help at all.

Delete all ShowInTaskbar assignments in your program to fix your problem.

Upvotes: 1

Grant Winney
Grant Winney

Reputation: 66501

You probably don't want to hide it when they resize it, just when they minimize the Form:

private void onResize(object sender, EventArgs e)
{
    if (WindowState == FormWindowState.Minimized)  // only hide if minimizing the form
    {
        this.ShowInTaskbar = false;
        notifyIcon1.Visible = true;
        this.Visible = false;
    }
}

To show the minimized Form again, you'll need to restore it:

private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
{
    this.ShowInTaskbar = true;
    this.Visible = true;

    WindowState = FormWindowState.Normal;
}

Upvotes: 1

Related Questions