Fading out an image with transparency in WinForms UI (.NET3.5)

扶醉桌前 提交于 2019-11-30 14:05:11
Michael Petrotta

As dylantblack says, WPF gives you better tools to do this. If you choose to use Windows forms, here's a simple approach using a timer that fades the image out. Set up a timer with whatever frequency you like. Start the timer, increment alpha every time through, and draw white or whatever your form color is with increasing alpha channel value.

int alpha = 0;

...

private void timer1_Tick(object sender, EventArgs e)
{
    if (alpha++ < 255)
    {
        Image image = pictureBox1.Image;
        using (Graphics g = Graphics.FromImage(image))
        {
            Pen pen = new Pen(Color.FromArgb(alpha, 255, 255, 255), image.Width);
            g.DrawLine(pen, -1, -1, image.Width, image.Height);
            g.Save();
        }
        pictureBox1.Image = image;
    }
    else
    {
        timer1.Stop();
    }
}

In WinForms you'd need to use timers or something to animate the opacity of the OK or FAILED control to make it fade out, or do something similar using GDI+ to draw it manually.

If you're using .NET 3.5 anyway, I'd recommend using WPF, which is much easier for doing that sort of thing.

As an example, you can have a look at Scott Hanselman's Baby Smash app, which is open source and has a similar concept of fading things in and out.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!