1 回答

TA贡献1803条经验 获得超3个赞
您可以从 PictureBox 继承并在其上绘制一个半透明的白色矩形,而不是更改图像:
public class FlashingPictureBox : PictureBox
{
private int flashIntensity;
public int FlashIntensity
{
get
{
return flashIntensity;
}
set
{
if (flashIntensity == value)
{
return;
}
flashIntensity = value;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
if (FlashIntensity == 0)
{
return;
}
using (SolidBrush brush = new SolidBrush(Color.FromArgb(FlashIntensity, 255, 255, 255)))
{
pe.Graphics.FillRectangle(brush, ClientRectangle);
}
}
}
然后将FlashIntensity属性设置为 0..255 以使它们亮起。
至于动画,我将创建一个单独的类,它采用“目标”图片框,并且可以通过自动画开始以来的毫秒数计算每个图片框所需的强度。
以下内容并不完全符合您的要求,但它可能会提供有关如何在单独的类中管理动画的想法:
public class WinAnimation
{
private readonly FlashingPictureBox[] boxes;
private readonly int started;
const int singleFlashDuration = 700;
const int nextBoxDelay = 100;
const int duration = 3000;
public WinAnimation(params FlashingPictureBox[] boxes)
{
this.boxes = boxes;
started = Environment.TickCount;
}
/// <summary>
/// Performs the animation at the indicated point in time.
/// </summary>
/// <param name="elapsed">The time elapsed since the animation started, in milliseconds</param>
/// <returns>true if the animation is running, false if the animation is completed</returns>
public bool Animate()
{
int elapsed = Environment.TickCount - started;
if (elapsed >= duration)
{
foreach (var box in boxes)
{
box.FlashIntensity = 0;
}
return false;
}
for (int i = 0; i < boxes.Length; i++)
{
var box = boxes[i];
int boxElapsed = elapsed - i * nextBoxDelay;
if (boxElapsed < 0)
{
box.FlashIntensity = 0;
}
else
{
int intensity = (boxElapsed % singleFlashDuration);
intensity = (intensity * 255) / singleFlashDuration;
box.FlashIntensity = intensity;
}
}
return true;
}
}
假设您有一个计时器,您可以按如下方式从您的表单中调用它:
private WinAnimation ani; // the animation object
private void button1_Click(object sender, EventArgs e)
{
ani = new WinAnimation(flashingPictureBox1, flashingPictureBox2, flashingPictureBox3, flashingPictureBox4, flashingPictureBox5);
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!ani.Animate())
{
timer1.Enabled = false;
ani = null;
}
}
- 1 回答
- 0 关注
- 144 浏览
添加回答
举报