繁体   English   中英

窗体不透明度..如何控制?

[英]Window form opacity .. How to control?

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

在表单的构造函数中,您可以编写这样的内容。

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();
}

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

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();
    }
}

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

对于退出降低不透明度动画

       ///////\\\\\\ 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();
    }

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

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(); 
  }

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

        ///////\\\\\\ 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();
     }

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM