[英]Stop and start gif animation in PictureBox by enabling and disabling it in mouse events
我向 PictureBox 添加了一個 gif,當加載表單時,我禁用了 PictureBox 以停止播放 gif。 然后當我在 PictureBox 上輸入 hover cursor 時,我想讓 PictureBox 開始播放 gif,但它不播放 gif。
為什么我無法在鼠標 hover 上啟用 PictureBox 和播放 gif,我該如何解決這個問題?
代碼:
private void MainPage_Load(object sender, EventArgs e)
{
pictureBox1.Enabled = false;
}
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
pictureBox1.Enabled = true;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Enabled = false;
}
接收禁用控件的鼠標事件
當控件被禁用時,鼠標事件將不會被控件接收,而是由其父級接收。
因此,在這種情況下,您可以處理父級的MouseHover
事件並查看鼠標 position 是否在PictureBox
的邊界內,然后啟用它。
例如。 假設圖片框的父級是以下形式:
private void form1_MouseHover(object sender, EventArgs e)
{
if (pictureBox1.Bounds.Contains(this.PointToClient(Cursor.Position)))
{
pictureBox1.Enabled = true;
}
}
在 PictureBox 中停止或啟動 gif animation
除了禁用和啟用PictureBox
啟動或停止 gif animation 之外,另一種通過調用私有 void Animate(bool animate)方法來啟用或禁用 animation 的選項:
void 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 });
}
然后在不禁用控件的情況下:
Animate(pictureBox1, true); //Start animation
Animate(pictureBox1, false); //Stop animation
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.