在按时间加载表单后绘制矩形 [英] draw rectangle after form loaded by time

查看:85
本文介绍了在按时间加载表单后绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以在表单加载后按时间更改矩形位置?
谢谢.

is there any way to change rectangle position by time after form loaded ?
thanks

推荐答案

是.您的矩形必须在重写的方法Control.OnPaint或事件Control.Paint的处理程序中以某种控件(可能是表单)的形式呈现.这些方法中的每一个都有一个参数eventArgs,它有一个System.Drawing.Graphics的实例.使用此实例,使用System.Drawing.Graphics.DrawRectangle绘制矩形.

现在,矩形的位置应该是一个变量,而不是一个常数.确定您的控件或表单的字段.修改此Position时,需要触发在新位置重新绘制矩形.这是通过方法Control.Invalidate(不是Refresh或其他方法)完成的.为了提高性能,可以使用参数(RectangleRegion)调用Invalidate以仅使部分渲染场景无效.

不要使用PaintBoxImage:这是一个常见错误.您可以使用它,但是完全没有帮助.不要创建Graphics的实例-这是另一个常见错误.事件参数参数中已经为您提供了该实例.要进行平滑更改,您需要使用双缓冲选项.

现在,我不知道您所说的按时间"是什么意思,但我可以猜想这是动画的时间.

为此,您需要创建一些线程,并通过System.Threading.Thread.Sleep稍微延迟地更改循环中的矩形位置.由于实时性可能不那么准确,因此您可能希望将无效时的实时性作为System.DateTime.Now来获取.现在,您不能从线程中调用Invalidate.您永远不能从非UI线程调用任何UI方法或属性.相反,您需要使用DispatcherControlInvokeBeginInvoke方法.
有关其工作原理的更多信息和解释,请参见我过去的回答:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [
Yes. Your rectangle must be rendered in some control (it could be your form) in the overridden method Control.OnPaint or the handler of the event Control.Paint. Each of these method has a parameter eventArgs, it has an instance of the System.Drawing.Graphics. Use this instance to draw the rectangle, using System.Drawing.Graphics.DrawRectangle.

Now, the position of the rectangle should be a variable, not a constant. Make if a field of your control or form. When you modify this Position, you need to trigger re-drawing of the rectangle in a new position. This is done via the method Control.Invalidate (not Refresh or anything else). To improve performance, you can call Invalidate with parameter (Rectangle or Region) to invalidate only a part of the rendered scene.

Do not use PaintBox or Image: this is a common mistake. You can use it, but it won''t help at all. Do not create an instance of Graphics — this is another common mistake. The instance is already provided for you in the event argument parameter. To make smooth change you need to use double buffering option.

Now, I don''t know what do you mean "by time", but I can guess this is some time of animation.

To do this, you need to create some thread, change the rectangle position in a loop with some delay via System.Threading.Thread.Sleep. As the real time can be not that accurate, you may want to get the real time at the moment of invalidation as System.DateTime.Now. Now, you cannot call Invalidate from the thread. You can never call any UI methods or properties from a non-UI thread. Instead, you need to use Invoke or BeginInvoke method of Dispatcher or Control.
For further information and explanations of how it works see my past Answers here:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

For my recommendation of thread use, see my collection of referenced to my past Answers here:
How to get a keydown event to operate on a different thread in vb.net[^].

You could also use timers, but I don''t recommend it, as it may cause more troubles. Threads is more robust and easier to implement correctly. You better use my recommendations for best practices though.

—SA


这篇关于在按时间加载表单后绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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