简体   繁体   English

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

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

I have a single form window application now I want to change the form opacity when application runs.我现在有一个表单窗口应用程序,我想在应用程序运行时更改form opacity Means when application run it will show low opacity form and as time increse it will show complete form with 100 opacity .意味着当应用程序运行时,它将显示low opacity表单,随着时间的推移,它将显示完整的表单, 100 opacity100 opacity So how to do that.那么如何做到这一点。 (should I use timer control to control opacity, if yes then how????) (我应该使用定时器控件来控制不透明度,如果是,那么如何????)

in constructor of the form you can write something like this.在表单的构造函数中,您可以编写这样的内容。

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

And then define a method like this然后定义一个这样的方法

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

To fade forms in and out, I usually do this:要淡入淡出表单,我通常这样做:

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

It's a nice, simple solution if you'll be doing it very infrequently.如果您很少这样做,这是一个不错的简单解决方案。 Otherwise, I would recommend using threads.否则,我会建议使用线程。

For Exit Decreasing Opacity Animation对于退出降低不透明度动画

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

In the constructor, start the timer control that will call a method at each 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(); 
  }

increasing Opacity Animation while Application start在应用程序启动时增加不透明度动画

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

In the constructor, set the opacity to 0 and start a timer with an interval of something small like 10 or 100 milliseconds.在构造函数中,将不透明度设置为 0,并以 10 或 100 毫秒的小间隔启动计时器。 In the timer_Tick event, you simply need to run this.Opacity += 0.01;timer_Tick事件中,你只需要运行this.Opacity += 0.01;

This will make it so that the opacity starts at 0 and increase by .01 every few milliseconds until it's 1 (opacity is a double, when it reaches a value of 1 it's fully opaque)这将使不透明度从 0 开始,每几毫秒增加 0.01,直到它为 1(不透明度是双倍,当它达到 1 的值时,它是完全不透明的)

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

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