如何绘制在窗体上移动的一系列对象 [英] How do I draw a sequence of objects moving on windows form

查看:73
本文介绍了如何绘制在窗体上移动的一系列对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我已经在c#中创建了在表单中水平移动的对象。现在我必须绘制多个(不是我以前做过的一个对象)随机生成的对象并在整个表单中按顺序移动。



这是我的代码移动object

Hi,

I''ve created the object that moves horizontally across the form in c#. Now I have to draw multiple(not one object as i have done before) objects generated randomly and moving in sequence across the form.

Here is my code that moves the object

private Rectangle Enemy1 = new Rectangle(0, 150, 40, 40);

//code below paints the object on the form
 e.Graphics.DrawRectangle(Pens.Red, Enemy1);

//part below moves the object on the form
                int EX1 = Enemy1.Location.X;
                int EY1 = Enemy1.Location.Y;
                Enemy1.Location = new Point(EX1 += 10, EY1 += 0);

推荐答案

您没有显示太多进展。您需要在重写的方法 System.Windows.Forms.Control.OnPaint 中或在事件的处理程序中执行所有渲染 System.Windows .Forms.Control.Paint 。让我们假设您的绘图取决于存储为表单类成员的一些数据。如果要移动多个对象,它可以是对象的集合,每个对象具有坐标,以及绘制所需的其他属性。如何移动它们?您需要更新数据并调用 System.Control.Invalidate



这在我过去的答案中有所描述:

在mdi子表单之间绘制线条 [ ^ ],

捕获在面板上绘图 [ ^ ],

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

如何加速我的vb.net应用程序? [ ^ ]。



重要的是,上面引用的最后一个答案描述了使用线程进行渲染。为了描述一些渲染场景,你需要有一个单独的线程,它实际编码改变对象位置,添加新对象和使场景无效的场景,具有一定的延迟(使用 System.Threading。 Thread.sleep代码)。所有问题都减少到了这个线程和你的UI线程之间的通信问题。



你不能从非UI线程调用任何与UI相关的东西。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA
You did not show much progress. You need to do all of you rendering in a overridden method System.Windows.Forms.Control.OnPaint or in the handler of the event System.Windows.Forms.Control.Paint. Let''s assume your drawing depends on some data stored as a member of the form class. If you want to move several objects, it could be a collection of objects, with coordinate of each, as well as other properties needed for drawing. How to move them? You need to update the data and call System.Control.Invalidate.

This is described in my past answers:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^].

Importantly, the last of the answers referenced above describe the use of rendering with threads. In order to describe some rendering scenario, you need to have a separate thread which actually codes the scenario of changing the locations of objects, adding new objects and invalidating the scene, with certain delays (using System.Threading.Thread.Sleep). The all the problem is reduced to the problem of communications between this thread and your UI thread.

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


我得到了解决方案。我使用了List集合。
I got the solution for this. I used List collection.


这篇关于如何绘制在窗体上移动的一系列对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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