这在VBA中似乎是一件非常奇怪的事情,但我很好奇,所以听我说完。我创建了一个程序,当点击图像时,它会在屏幕上移动图像。这是可行的,虽然还好,但它仍然有效。
代码如下:
'Start primitive animation
Private Sub imgBean_Click()
'Limit for movement to right
Dim coordinates, limit As Integer
coordinates = 0
limit = Form.Width
Do While coordinates < limit
coordinates = coordinates + 5
'Move the image
Me.imgBean.Move coordinates
'Needed to add this to see the image move - updates the form
Form.Repaint
Loop
Debug.Print "Coordinates " & coordinates
Debug.Print limit
'Reset the limit
limit = 0
End Sub
正如之前所说的代码工作-但当图像移动屏幕被冻结,即我不能关闭表单或与其他组件交互。(这类似于在Android环境中阻塞UI线程——这是你永远不会做的!)
那么有办法避免这种情况吗?
感谢注:将焦点设置为表单会使图像偶尔出现或消失。
结果是我找不到一种方法来防止动画开始后与GUI的交互。我之所以说"视觉化"是因为在动画运行时可以点击按钮并输入内容。
它涉及到在MS Access中使用DoEvents
命令作为多任务处理的一种方式。在我的代码中,我在Do-While循环上面添加了命令(见下文):
'Essentially allow multitasking
DoEvents
Do While coordinates < limit
coordinates = coordinates + 5
'Move the image
Me.imgBean.Move coordinates
Form.Repaint
Loop
应该注意的是,当我把它放在Do-While循环中时,它不起作用。因此,当我通过点击图像开始动画时,我仍然可以按下其他按钮,因为我输入了一个字段的值,并单击一个按钮将其转换为另一个—而动画正在运行。
唯一的问题是如何在视觉上做到这一点