C# winforms中的IE 9类按钮热跟踪

C# winforms中的IE 9类按钮热跟踪,c#,winforms,animation,C#,Winforms,Animation,我有一个类,目前在正常和鼠标悬停状态的2个图像之间切换,但我想添加一个微妙的动画,在2个图像之间切换 是否有任何(简单的)方法可以让按钮从一个图像动画到下一个图像,如IE 9浏览器中的按钮 编辑: 由于joe_coolish的回答,这里是我用于OnPaint过载的最终解决方案 protected override void OnPaint(PaintEventArgs e) { e.Graphics.DrawImage(_normalImage, 0, 0);

我有一个类,目前在正常和鼠标悬停状态的2个图像之间切换,但我想添加一个微妙的动画,在2个图像之间切换

是否有任何(简单的)方法可以让按钮从一个图像动画到下一个图像,如IE 9浏览器中的按钮

编辑:

由于joe_coolish的回答,这里是我用于OnPaint过载的最终解决方案

protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.DrawImage(_normalImage, 0, 0);

        ColorMatrix matrix = new ColorMatrix();
        matrix.Matrix33 = _opacity;

        ImageAttributes attributes = new ImageAttributes();
        attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

        e.Graphics.DrawImage(_hotImage, this.DisplayRectangle, 0, 0,
            this.DisplayRectangle.Width, this.DisplayRectangle.Height, GraphicsUnit.Pixel, attributes);

        if (_opacity < 1 && _over)
        {
            _opacity += 0.02f;
            _opacity = Math.Min(1, _opacity);
            this.Invalidate();
        }
        if (_opacity > 0 && !_over)
        {
            _opacity -= 0.02f;
            _opacity = Math.Max(0, _opacity);
            this.Invalidate();
        }
    }

这些图像有多不同?如果您想要“淡入/淡出”类型的东西,请尝试以下方法:

    private float _opacity = 0.0f;
    private bool _over = false;

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.DrawImage(imgOne);

        ColorMatrix matrix = new ColorMatrix();
        matrix.Matrix33 = _opacity; //opacity 0 = completely transparent, 1 = completely opaque

        ImageAttributes attributes = new ImageAttributes();
        attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

        e.Graphics.DrawImage(imgTwo, new Rectangle(0, 0, imgTwo.Width, imgTwo.Height), 0, 0, imgTwo.Width, imgTwo.Height, GraphicsUnit.Pixel, attributes);

        if(_opacity < 1 && _over)
        {
            _opacity += 0.05f;   //  Play with this!!
            Invalidate();
        }
        if(_opacity > 0 && !_over)
        {
            _opacity -= 0.05f;   //  Play with this!!
            Invalidate();    
        }
    }
private float\u不透明度=0.0f;
private bool _over=假;
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
e、 绘图图像(imgOne);
ColorMatrix=新的ColorMatrix();
matrix.Matrix33=\u opacity;//不透明度0=完全透明,1=完全不透明
ImageAttributes=新的ImageAttributes();
SetColorMatrix(矩阵,ColorMatrixFlag.Default,ColorAdjustType.Bitmap);
e、 Graphics.DrawImage(imgTwo,新矩形(0,0,imgTwo.Width,imgTwo.Height),0,0,imgTwo.Width,imgTwo.Height,GraphicsUnit.Pixel,属性);
如果(_不透明度<1&&u超过)
{
_不透明度+=0.05f;//玩这个!!
使无效();
}
如果(_不透明度>0&!_超过)
{
_不透明度-=0.05f;//玩这个!!
使无效();
}
}
然后,当鼠标进出时,只需将
\u设置在
上即可


这是一个非常粗糙的代码集,IDK甚至可以编译,但这是一个非常好的起点

在焦点或鼠标上方更改图像是否有意义?我是否遗漏了什么?我希望控件在鼠标悬停时更改图像,并在鼠标离开时恢复正常。我的问题不是关于状态如何/何时改变,而是如何在状态改变发生时设置动画。我的解决方案对您有效吗?图像几乎相同,只是颜色不同-我尝试使用您的代码,但它似乎不会影响图像之间的转换,而只是简单地将它们切换到更低的+=-=值。应该有淡入淡出。尝试类似于0.01的值。如果这不起作用,请告诉我,我会进一步调查:)我最终使用了.02来提供一个很好的微妙的褪色效果,这正是我想要的。感谢您的解决方案,它工作得非常好!
    private float _opacity = 0.0f;
    private bool _over = false;

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.DrawImage(imgOne);

        ColorMatrix matrix = new ColorMatrix();
        matrix.Matrix33 = _opacity; //opacity 0 = completely transparent, 1 = completely opaque

        ImageAttributes attributes = new ImageAttributes();
        attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

        e.Graphics.DrawImage(imgTwo, new Rectangle(0, 0, imgTwo.Width, imgTwo.Height), 0, 0, imgTwo.Width, imgTwo.Height, GraphicsUnit.Pixel, attributes);

        if(_opacity < 1 && _over)
        {
            _opacity += 0.05f;   //  Play with this!!
            Invalidate();
        }
        if(_opacity > 0 && !_over)
        {
            _opacity -= 0.05f;   //  Play with this!!
            Invalidate();    
        }
    }