1

I created a default form in Visual Studio 2010 and on the form design I have not changed anything. I only added following code in Form1.cs:

using System;
using System.Windows.Forms;

namespace WinFormTest1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.TopMost = true;
            this.ShowInTaskbar = true;
            this.Load += new EventHandler(Form1_Load);
            this.Shown += new EventHandler(Form1_Shown);
        }

        void Form1_Load(object sender, EventArgs e)
        {
            this.Opacity = 0;
        }

        void Form1_Shown(object sender, EventArgs e)
        {
            this.Opacity = 1;
        }
    }
}

Starting this program the form does not appear on the taskbar. It only appears on the task bar when made ​​active any other window, and then activating this form.

What is the reason of such behavior?

Edited: Why do I need to set the opacity of it in handler Form1_Load? I created class FormAppearingEffect whose code below:

using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;

namespace AG.FormAdditions
{
    public class FormAppearingEffect
    {
        private Form form;
        double originalOpacity;

        public FormAppearingEffect(Form form)
        {
            this.form = form;
            form.Load += form_Load;
            form.Shown += form_Shown;
        }

        void form_Load(object sender, EventArgs e)
        {
            originalOpacity = form.Opacity;
            form.Opacity = 0;
        }

        private void form_Shown(object sender, EventArgs e)
        {
            try
            {
                double currentOpacity = 0;
                form.Opacity = currentOpacity;
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                for (int i = 1; currentOpacity < originalOpacity; i++)
                {
                    currentOpacity = 0.1 * i;
                    form.Opacity = currentOpacity;
                    Application.DoEvents();

                    //if processor loaded and does not have enough time for drawing form, then skip certain count of steps
                    int waitMiliseconds = (int)(50 * i - stopwatch.ElapsedMilliseconds);
                    if (waitMiliseconds >= 0)
                        Thread.Sleep(waitMiliseconds);
                    else
                        i -= waitMiliseconds / 50 - 1;
                }
                stopwatch.Stop();
                form.Opacity = originalOpacity;
            }
            catch (ObjectDisposedException) { }
        }
    }
}

In any form of program I use this class like this:

using System;
using System.Windows.Forms;
using AG.FormAdditions;

namespace WinFormTest1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            FormAppearingEffect frmEffects = new FormAppearingEffect(this);
        }
    }
}

Thus my form appears on the screen "gradually".

This is where I found a bug, which we are in this topic. And that's it for this reason I need to set the opacity in the event handlers.

Anatolii Humennyi
  • 1,807
  • 3
  • 26
  • 37
  • 2
    Have you tried disabling the `this.Opacity = 0;` line? – Nir Kornfeld Sep 09 '13 at 15:03
  • 2
    I can replicate this behavior. The `ShowInTaskbar` is the only unneeded line; other than that this is a minimal repo. Just changing TopMost alone, or setting the Opacity without changing TopMost, or changing the Opacity in the constructor directly, won't create this behavior. As hesitant as I am to say it, this seems like a framework bug; some odd race condition most likely. – Servy Sep 09 '13 at 15:05
  • @NirKornfeld yes. Then everything works fine. But I am interested in is the reason why the change of opacity of form in these handlers affect the visibility of the form on the taskbar. – Anatolii Humennyi Sep 09 '13 at 15:13
  • @AnatoliiGumennyi If you're just looking for a workaround for the time being, just set the opacity to zero in the constructor, rather than the form load event. Not that that explains the current behavior. – Servy Sep 09 '13 at 15:18
  • 1
    Rather than using `DoEvents` and `Thread.Sleep` in a loop you should *really* be using a `Timer` to do your an animation instead. It will be dramatically less bug prone, will perform much better, and result in a more responsive UI. That said, it's not related to the problems you're describing given that your minimal repo has the same problem. – Servy Sep 09 '13 at 16:15
  • 1
    Please be aware that performing effects such as dialog opacity fade-in will have terrible consequences to anyone running the application through Remote Desktop, PCAnywhere, etc. Every frame of the fade occurs at a noticeably slow speed, which can impact application performance significantly. Forms can literally take many seconds to fade in. – deegee Sep 09 '13 at 16:25

0 Answers0