所以我在用 C# 4.0 编写的 Winforms 应用程序中有一个自定义 Button 类。该按钮通常包含一个静态图像,但当发生刷新操作时,它会切换到动画 AJAX 样式按钮。为了使图像动画化,我设置了一个计时器,它在每个刻度上推进图像动画并将其设置为按钮图像。这是我让它工作的唯一方法。我的解决方案不是那么有效;任何意见将是有益的。
所以有两个问题: 1. 有没有更简单的方法——就像我忽略了一些我应该使用的功能一样?2. 有没有更好的手动动画图像的方法?
在下面找到我在每个刻度上所做的代码。第一个改进领域:也许将图像的每一帧复制到一个列表中?注意 _image.Dispose(); 未能处理本地图像会导致内存泄漏。
感谢您在这里的任何建议。一旦我有一个有效的解决方案,我会在网上发布一些东西并链接它。
private void TickImage()
{
if (!_stop)
{
_ticks++;
this.SuspendLayout();
Image = null;
if(_image != null)
_image.Dispose();
//Get the animated image from resources and the info
//required to grab a frame
_image = Resources.Progress16;
var dimension = new FrameDimension(_image.FrameDimensionsList[0]);
int frameCount = _image.GetFrameCount(dimension);
//Reset to zero if we're at the end of the image frames
if (_activeFrame >= frameCount)
{
_activeFrame = 0;
}
//Select the frame of the animated image we want to show
_image.SelectActiveFrame(dimension, _activeFrame);
//Assign our frame to the Image property of the button
Image = _image;
this.ResumeLayout();
_activeFrame++;
_ticks--;
}
}