From d5ba7e1fdb9ef3c1a844d7d34bbeda9a74a05064 Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Tue, 25 Jun 2024 17:37:22 +0200 Subject: [PATCH] AnimationPictureBox - Fix animation starting when resizong --- PCK-Studio/Extensions/PictureBoxExtensions.cs | 27 +++++++++++++++++++ PCK-Studio/PckStudio.csproj | 1 + .../ToolboxItems/AnimationPictureBox.cs | 23 ++++++++-------- 3 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 PCK-Studio/Extensions/PictureBoxExtensions.cs diff --git a/PCK-Studio/Extensions/PictureBoxExtensions.cs b/PCK-Studio/Extensions/PictureBoxExtensions.cs new file mode 100644 index 00000000..e26446da --- /dev/null +++ b/PCK-Studio/Extensions/PictureBoxExtensions.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace PckStudio.Extensions +{ + internal static class PictureBoxExtensions + { + public static bool IsAnimating(this PictureBox pictureBox) + { + var fi = typeof(PictureBox).GetField("currentlyAnimating", BindingFlags.NonPublic | BindingFlags.Instance); + return (bool)fi.GetValue(pictureBox); + } + + public static void Animate(this PictureBox pictureBox, bool animate) + { + var animateMethod = typeof(PictureBox).GetMethod("Animate", BindingFlags.NonPublic | BindingFlags.Instance, + null, new Type[] { typeof(bool) }, null); + animateMethod.Invoke(pictureBox, new object[] { animate }); + } + + } +} diff --git a/PCK-Studio/PckStudio.csproj b/PCK-Studio/PckStudio.csproj index 49985dfc..3e9d2105 100644 --- a/PCK-Studio/PckStudio.csproj +++ b/PCK-Studio/PckStudio.csproj @@ -134,6 +134,7 @@ + Form diff --git a/PCK-Studio/ToolboxItems/AnimationPictureBox.cs b/PCK-Studio/ToolboxItems/AnimationPictureBox.cs index 580cd49f..196b19e1 100644 --- a/PCK-Studio/ToolboxItems/AnimationPictureBox.cs +++ b/PCK-Studio/ToolboxItems/AnimationPictureBox.cs @@ -34,37 +34,38 @@ namespace PckStudio.ToolboxItems private bool _isPlaying; - private void PictureBox_Internal_Animate(PictureBox pictureBox, bool animate) - { - var animateMethod = typeof(PictureBox).GetMethod("Animate", - System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance, - null, new Type[] { typeof(bool) }, null); - animateMethod.Invoke(pictureBox, new object[] { animate }); - } - public new Image Image { get => base.Image; set { base.Image = value; - PictureBox_Internal_Animate(this, false); + this.Animate(false); if (value is null) return; value.SelectActiveFrame(new FrameDimension(value.FrameDimensionsList[0]), 0); } } + protected override void OnPaint(PaintEventArgs paintEventArgs) + { + base.OnPaint(paintEventArgs); + if (!_isPlaying && this.IsAnimating()) + { + Stop(); + } + } + public void Start() { _isPlaying = true; - PictureBox_Internal_Animate(this, _isPlaying); + this.Animate(_isPlaying); } public void Stop() { _isPlaying = false; - PictureBox_Internal_Animate(this, _isPlaying); + this.Animate(_isPlaying); } protected override void Dispose(bool disposing)