本文介绍了第二个 timer.Start() 没有被第一个计时器滴答触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在 Form 的右下角有一个小标志,我想以预设的速度淡入和淡出,每次淡入大约 6 秒.我尝试了几种不同的方法,但是一旦第一个计时器结束,我就无法让图片再次淡入淡出.这是我的 2 个计时器及其各自的滴答方法的代码.

So I have a small logo in the lower right corner of a Form that I want to fade in and out at a preset speed, about 6 seconds per fade. I have tried a few different methods but I can never get the picture to fade back in again once the first timer has finished. Here's my code for the the 2 timers and their respective tick methods.

编辑现在包含的定时器声明.

EDIT The declarations for the timers included now.

        Timer fade = new Timer();
        Timer fade2 = new Timer();


                fade.Interval = (200);
                fade.Tick += new EventHandler(fade_Tick);


                fade2.Interval = (200);
                fade2.Tick += new EventHandler(fade_Tick_Two);

                fade.Start();

private void fade_Tick(object sender, EventArgs e)
        {
            if (alpha < 255)
            {
                image = picboxPic.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();
                }
                picboxPic.Image = image;
                this.Invalidate();
                alpha++;
            }
            else
            {
                fade.Stop();
                fade2.Start();
            }


        }
private void fade_Tick_Two(object sender, EventArgs e)
        {

            if (alpha > 0)
            {
                image = picboxPic.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();
                }
                picboxPic.Image = image;
                this.Invalidate();
                alpha--;
            }
            else
            {
                fade2.Stop();
                fade.Start();
            }

        }

关于为什么第二个计时器不会启动的任何想法?我使用了断点并且 alpha 级别确实达到了 255,但它不会触发第二个 Tick 事件.

Any ideas as to why the second timer won't start? I've used breakpoints and the alpha level does reach 255 but then it doesn't trigger the second Tick event.

推荐答案

我引用的链接中描述的方法对我有用:

The method described in the link I quoted works for me:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        test();
    }
    System.Timers.Timer fade = new System.Timers.Timer(50);
    System.Timers.Timer fade2 = new System.Timers.Timer(50);
    Image originalImage = Image.FromFile(@"D:\kevin\Pictures\odds&Sods\kitchener.jpg");

    int alpha = 100;

    void test()
    {
        fade.Elapsed +=new System.Timers.ElapsedEventHandler(fade_Tick);
        fade2.Elapsed+=new System.Timers.ElapsedEventHandler(fade_Tick_Two);
        fade.Start();

    }
    delegate void timerDelegate(object sender, EventArgs e);
    private void fade_Tick(object sender, EventArgs e)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new timerDelegate(fade_Tick), sender, e);
            return;
        }
        if (alpha >= 0)
        {
            picboxPic.Image =  SetImgOpacity(originalImage, alphaToOpacity(alpha));
            picboxPic.Invalidate();
            alpha--;
        }
        if (alpha < 0)
        {
            alpha = 0;
            fade.Stop();
            fade2.Start();
        }
    }
    private void fade_Tick_Two(object sender, EventArgs e)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new timerDelegate(fade_Tick_Two), sender, e);
            return;
        }
        if (alpha <= 100)
        {
            picboxPic.Image = SetImgOpacity(originalImage, alphaToOpacity(alpha));
            picboxPic.Invalidate();
            alpha++;
        }
        if (alpha > 100)
        {
            alpha = 100;
            fade2.Stop();
            fade.Start();
        }
    }

    float alphaToOpacity(int alpha)
    {
        if (alpha == 0)
            return 0.0f;

        return (float)alpha / 100.0f;
    }

    //Setting the opacity of the image
    public static Image SetImgOpacity(Image imgPic, float imgOpac)
    {
        Bitmap bmpPic = new Bitmap(imgPic.Width, imgPic.Height);
        Graphics gfxPic = Graphics.FromImage(bmpPic);

        ColorMatrix cmxPic = new ColorMatrix();
        cmxPic.Matrix33 = imgOpac;
        ImageAttributes iaPic = new ImageAttributes();
        iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
        gfxPic.DrawImage(imgPic, new Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, iaPic);
        gfxPic.Dispose();

        return bmpPic;
    }
}

代码有点粗糙,关闭表单时需要处理处置,但它会逐渐淡入淡出,其余的可以轻松处理-我还使其更快​​进行测试'因为生命是短暂的:-)

The code is a bit crude and you will need to take care of the dispose when you close the form, but it fades up and and down and the rest can easily be taken care of - I also made it quicker for testing 'cos life's short :-)

这篇关于第二个 timer.Start() 没有被第一个计时器滴答触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:08