private void Welcome_Load(object sender, EventArgs e)
        {
            for (double d = 0.01; d < 2; d += 0.03)
            {
                System.Threading.Thread.Sleep(1);
                Application.DoEvents();
                this.Opacity = d;
                this.Refresh();
            }
            for (double d = 1; d > 0; d -= 0.015)
            {
                System.Threading.Thread.Sleep(1);
                Application.DoEvents();
                this.Opacity = d;
                this.Refresh();
            }
        }
淡入和淡出的代码,运行起来的时候,那个窗体没有连贯的淡入淡出,但是伴随着闪动.有什么办法让他不要闪吗?

解决方案 »

  1.   

    this.Refresh();
     是 重新刷新的 意思! 所以会闪烁的 
      

  2.   

    你 将 this.Refresh(); 放入到 for 循环里面 所以 他会 来回的 调用 this.Refresh(); 并设置当前线程每次循环都休眠所以 就 会 出现这种闪烁的 效果!
      

  3.   


    for (double d = 0.01; d < 2; d += 0.03)
    {
    System.Threading.Thread.Sleep(1);
    Application.DoEvents();
    this.Opacity = d;
    }
      

  4.   

    闪是因为你一开始的Opacity = 1,然后突然变为0.01public Form2()
    {
    InitializeComponent();
    this.Opacity = 0;
    }
    private void Form2_Load(object sender, EventArgs e)
    {
    this.Show();
    for (double d = 0.01; d < 2; d += 0.03)
    {
    System.Threading.Thread.Sleep(1);
    Application.DoEvents();
    this.Opacity = d;
    }
    }
    这次肯定可以了
      

  5.   

    1.用Update,不要用Refresh
    2.这种事情不要兴师动众地用Thread还Sleep呢.用一个简单的Timer控件足矣.(个人看法)
      

  6.   

    刚才试了下用Timer控件,效果非常好:(先拖一个timer1到窗体,一切用默认值)        private void timer1_Tick(object sender, EventArgs e)
            {
                Opacity += 0.1;
                if (Opacity >= 1) timer1.Stop();
            }
            
            void Deeper()
            {
                timer1.Start();
            }