为什么我的《魔法》项目在OnKeypress方法内的循环中工作?
我需要创建一个应用程序,在某种情况下按特定键被按下后可以隐藏修发的应用程序。
我已经将键盘浏览设置为true,并且图像可以隐藏在Onkeypress方法中,一切正常。但是,一旦我在OnkeyPress方法内实现了一段时间的循环,则在按下了Apropropropraite键后不再隐藏。
我该怎么办?
I needed to create a application where the Timage can be hide after a certain key is pressed under a certain situation.
I have set KeyPreview to true, and the image can hide within the onKeyPress method, everything worked properly. But as soon as I implemented a while loop inside the onKeyPress method, the image doesn't hide anymore after the appropraite key is being pressed.
What should I do to fix that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该在活动处理程序内进行循环。事件处理程序应对UI进行简单的更新然后退出,以允许Delphi反映屏幕上的UI更新。
将您的WARE循环从按键事件中移出,最好是转到背景线程,交替地进入应用程序。了)。
“穷人”的解决方案(不是建议的解决方案,但如果所有其他选项都太复杂,则可以使用)是
在段循环(如果您在循环中更改UI元素)或在此之前或在此之前 呼叫开始循环(如果不是)。
但是请注意,尽管“ application..processmessages”代码正在运行,但所有其他事件处理程序都可以运行,因此,如果您(F.Ex.在当前运行的计时器事件完成之前再次致电(导致无限递归循环,并最终导致程序的堆叠溢出和终止)。
You shouldn't do a WHILE loop inside an event handler. The event handlers should do a simple update to UI and then exit, to allow Delphi to reflect the UI update on screen.
Move your WHILE loop out of the KeyPress event, preferably into a background thread, alternately into the Application.OnIdle event (where you then perform ONE iteration of the WHILE loop within the OnIdle event and turn off processing when the WHILE condition isn't valid anymore).
The "poor man"'s solution (and not one that is recommended, but can be used if all other options are too complicated) is to call
either within the WHILE loop (if you are altering UI elements in the loop) or just before starting the WHILE loop (if you're not).
Note, however, that while the "Application.Processmessages" code is running, all other event handlers are capable of being run, so if you, f.ex., run an Application.ProcessMessages within a Timer Event, the Timer Event may be called again before the currently running Timer Event has finished running (leading to an infinite recursive loop and eventually a stack overflow and termination of your program).