📄 animatectl.cs
字号:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
namespace gifTest
{
public class AnimateCtl : System.Windows.Forms.Control
{
System.Windows.Forms.Timer fTimer = null; //创建一个Time类对象fTime
protected Graphics graphics = null; //前台绘图表面
private int loopCount =0; //定义动画循环控制变量
private int loopCounter = 0; //定义停止动画循环变量
private int count = 0; //定义动画宽度
private int currentFrame = 0; //定义动画帧变量
// 动画类构造方法
public AnimateCtl()
{
//缓存 Graphics 对象
this.graphics = this.CreateGraphics();
//实例化 Timer
fTimer = new System.Windows.Forms.Timer();
//与 Timer 的 Tick 事件挂钩
fTimer.Tick += new System.EventHandler(this.timer1_Tick);
}
private Bitmap bitmap;
public Bitmap Bitmap
{
get
{
return bitmap;
}
set
{
bitmap = value;
}
}
private void Draw(int iframe)
{
//计算图形框的左边位置
int XLocation = iframe * Width;
Rectangle rect = new Rectangle(XLocation, 0, Width,Height);
//绘制图像
graphics.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
}
public void StartAnimation(int frWidth, int DelayInterval, int LoopCount)
{
Width = frWidth;
//循环次数
loopCount = LoopCount;
//重置循环计数器
loopCounter = 0;
//计算 count
count = bitmap.Width / Width;
Height = bitmap.Height;
//向计时器指定延迟间隔
fTimer.Interval = DelayInterval;
//启动计时器
fTimer.Enabled = true;
}
private void timer1_Tick(object sender, System.EventArgs e)
{
if (loopCount == -1) //不停地循环
{
this.DrawFrame();
}
else
{
if (loopCount == loopCounter) //停止动画
fTimer.Enabled = false;
else
this.DrawFrame();
}
}
private void DrawFrame()
{
if (currentFrame < count - 1)
{
//移到下一个帧
currentFrame++;
}
else
{
//递增 loopCounter
loopCounter++;
currentFrame = 0;
}
Draw(currentFrame);
}
//动画停止方法
public void StopAnimation()
{
fTimer.Enabled = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -