窗口形式不透明..如何控制?

时间:2011-12-30 05:57:08

标签: c# winforms c#-4.0 c#-3.0

我现在有一个表单窗口应用程序我想在应用程序运行时更改form opacity。表示在应用程序运行时它将显示low opacity表单,随着时间的推移,它将显示带有100 opacity的完整表单。那怎么做。 (我应该使用计时器控制来控制不透明度,如果是,那么如何????)

6 个答案:

答案 0 :(得分:5)

在表单的构造函数中,你可以写这样的东西。

this.Opacity = .1;
timer.Interval = new TimeSpan(0, 0, intervalinminutes);
timer.Tick += ChangeOpacity;
timer.Start();

然后定义一个像这样的方法

void ChangeOpacity(object sender, EventArgs e)
{
    this.Opacity += .10; //replace.10 with whatever you want
    if(this.Opacity == 1)
        timer.Stop();
}

答案 1 :(得分:3)

要淡入淡出表格,我通常会这样做:

for(double opacity = 0.0; opacity <= 1.0; opacity += 0.2) {
    DateTime start = DateTime.Now;
    this.Opacity = opacity;

    while(DateTime.Now.Subtract(start).TotalMilliseconds <= 30.0) {
        Application.DoEvents();
    }
}

如果您不经常这样做,这是一个很好的,简单的解决方案。否则,我建议使用线程。

答案 2 :(得分:1)

在构造函数中,启动将在每个tick处调用方法的计时器控件。

timer.Interval = 1000; 
timer.Tick += new EventHandler(TimerEventProcessor);
timer.Start(); 

............

 private static void TimerEventProcessor(Object myObject,
                                            EventArgs myEventArgs) 
  {
       if(this.Opacity < 1)
         this.Opacity += .1;
       else
           timer.Stop(); 
  }

答案 3 :(得分:1)

用于减少不透明度的退出动画

       ///////\\\\\\ Coded by Error X Tech ///////\\\\\\

 System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
 private void DecreaseOpacity(object sender, EventArgs e)
    {
        if (this.Opacity >= 0.1)
        {
            this.Opacity -= 0.04; //replace 0.04 with whatever you want
        }
        if (this.Opacity <= 0.0)
            timer.Stop();
        if (this.Opacity <= 0.1)
        {
            System.Environment.Exit(1);
            Process.GetCurrentProcess().Kill();
        }
    }

private void Exit_Click(object sender, EventArgs e)
    {
        
        timer.Interval = 47; //replace 47 with whatever you want
        timer.Tick += DecreaseOpacity;
        timer.Start();
    }

答案 4 :(得分:0)

在构造函数中,将不透明度设置为0并启动一个间隔为10或100毫秒的小计时器。在timer_Tick事件中,您只需运行this.Opacity += 0.01;

即可

这样可以使不透明度从0开始,每隔几毫秒增加0.01,直到它为1(不透明度为双倍,当它达到值1时,它完全不透明)

答案 5 :(得分:0)

启动应用程序时增加不透明度动画

        ///////\\\\\\ Coded by Error X Tech ///////\\\\\\
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
void IncreaseOpacity(object sender, EventArgs e)
    {
        if (this.Opacity <= 1)  //replace 0.88 with whatever you want
        {
            this.Opacity += 0.01;  //replace 0.01 with whatever you want
        }
        if (this.Opacity == 1) //replace 0.88 with whatever you want
            timer.Stop();
    }
private void Form1_Load(object sender, EventArgs e)
    {
        this.Opacity = .01;
        timer.Interval = 10; //replace 10 with whatever you want
        timer.Tick += IncreaseOpacity;
        timer.Start();
     }