通过改变颜色的动画效果 [英] Animation effect by changing colors

查看:62
本文介绍了通过改变颜色的动画效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一段代码,以在执行过程中两次更改标签的背面颜色,以使其具有动画效果.这个想法说:

当按下按钮时,将颜色从黑色更改为红色.
然后放置一个空循环以创建一些延迟,然后颜色变回黑色.

I have written a code to change the back color of a label twice during execution to give it an animation effect. The idea says that :

when the button is pressed change the color from black to red.
Then a null loop is placed to create some delay and then the color changes back to black.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        box.BackColor = Color.Red
        For delay As Double = 0 To 900000000 Step 1
        Next
        box.BackColor = Color.Black
    End Sub



但是它的表现并不理想.标签不会变成红色,我怀疑程序首先执行了延迟.知道是什么问题吗?



But its not performing as desired. The label doesnt become red and i doubt the program is executing the delay first. Any idea whats the issue?

推荐答案

都错了!在您的片段中,您将更改颜色(为什么900000000 ?!没有意义!),并且仅显示最后一个黑色.

这是要执行的操作:创建一个定期更改颜色的线程.可以在显示表单时创建它,并在关闭表单或窗口时因覆盖System.Windows.Forms.Form.OnFormClosingSystem.Windows.Window.OnClosing而中止. (不,我不能这样回答.如果问题与UI有关,请始终标记您的UI库!)

现在,线程应循环使用System.Threading.Thread.Sleep来使颜色定期变化.现在,您需要使UI更改颜色.您不能从非UI线程调用类似您的box.BackColor = Color.Black之类的东西,它将引发异常.该怎么办?

您无法从非UI线程调用与UI相关的任何操作.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
All wrong! In your fragment you will change colors (why 900000000?! makes no sense!) and only the last black color will be shown.

Here is what to do: create a thread which periodically changes color. It can be created when the form is shown and aborted when the form or windows is being closing, from withing overridden System.Windows.Forms.Form.OnFormClosing or System.Windows.Window.OnClosing. (No, I cannot answer like that. Always tag you UI library if the question is about UI!)

Now, the thread should use System.Threading.Thread.Sleep in cycle to make color change periodic. Now you need to make UI changing color. You cannot call anything like your box.BackColor = Color.Black from a non-UI thread, it will throw exception. What to do?

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于通过改变颜色的动画效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆