0

我尝试让我的 Windows 窗体应用程序将不透明度从 1 更改为 0(不可见形式),然后在按下按钮时从 0 不透明度更改为 1(正常、可见形式)。

每个不透明度更改步骤都与计时器相关联。它会在每个 timer1_tick 中改变不透明度 (-/+0,10)。

我从不透明度 = 1 (100%) 开始

现在我有类似的东西:

public partial class Form1 : Form
{
    double OpacityStep; 

    public Form1()
    {
        OpacityStep = 0.10; 
        InitializeComponent();
        updateButton1(); 
        updateButton2();
    }

    private void updateButton1() 
    {
        if (Opacity < 1.00) button1.Enabled = true; 
        else 
        {
            button1.Enabled = false;
        }
    }

    private void updateButton2()
    {
        if (Opacity > 0.0) button2.Enabled = true;
        else 
        {
            button1.Focus(); 
            button2.Enabled = false;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        double NewOpacity = Opacity + OpacityStep; 
        if (NewOpacity > 1.0) Opacity = 1.0; 
        else Opacity = NewOpacity;
        updateButton2(); 
        updateButton1();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        double NewOpacity = Opacity - OpacityStep;

        if (NewOpacity < 0.0) Opacity = 0.0; 
        else Opacity = NewOpacity;
        updateButton1();
        updateButton2();
    }

    private void button3_Click(object sender, EventArgs e) // app start
    {
        this.timer1.Interval = 1000;
        this.timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {   
        button2_Click(sender, e);
    }
}

此代码使我的应用程序不可见,但如何使其恢复可见 (opacity = 1) ?? ?

4

1 回答 1

1

“我尝试让我的 Windows 窗体应用程序将不透明度从 1 更改为 0(不可见形式),然后在按下按钮时从 0 不透明度变为 1(正常、可见形式)。”

对我来说听起来很清楚:

public partial class Form1 : Form
{

    double OpacityStep = 0; 

    public Form1() 
    {            
        InitializeComponent();

        timer1.Interval = 100;
        timer1.Enabled = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        this.Opacity = 1;
        OpacityStep = -0.10;
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        double NewOpacity = Opacity + OpacityStep;
        if (NewOpacity <= 0)
        {
            NewOpacity = 0;
            OpacityStep = 0.10;
        }
        else if(NewOpacity >= 1 && OpacityStep  > 0)
        {
            NewOpacity = 1.0;
            button1.Enabled = true;
            timer1.Stop();
        }
        this.Opacity = NewOpacity;
    }

}

编辑:“如果我需要循环这个(不透明度从 1 到 0 并从 0 到 1,在循环中),我应该在这段代码中改变什么?”

使用 for 循环:

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;

        // fade out:
        this.Opacity = 1.0;
        for (double opacity = 1.0; opacity >= 0.0; opacity = opacity - .1)
        {
            System.Threading.Thread.Sleep(100);
            this.Opacity = opacity;
            Application.DoEvents();
        }

        // fade in:
        this.Opacity = 0.0;
        for (double opacity = 0.0; opacity <= 1.0; opacity = opacity + .1)
        {
            System.Threading.Thread.Sleep(100);
            this.Opacity = opacity;
            Application.DoEvents();
        }
        this.Opacity = 1.0;

        button1.Enabled = true;
    }
于 2013-06-05T19:00:31.427 回答