C# 只在告诉图片框/面板刷新时刷新它们?

发布于 2024-10-13 03:12:52 字数 258 浏览 9 评论 0原文

我正在 C# Windows 窗体应用程序中制作炸弹人游戏。它有 300 多个放置在面板上的图片框(墙)。炸弹人本人的画框也在那个面板上。

当轰炸机图片框的位置发生变化时,面板上的所有控件都会自动刷新。因为该面板上有很多控件,并且图片框的位置每秒更改多次,所以当我尝试移动时,程序会变得缓慢。

我想控制面板(及其控件)的刷新事件,因为我认为当仅需要刷新的图片框以编程方式刷新时,我的问题就解决了。

我希望有人能帮助我解决这个问题!

路德.

I'm making a bomberman game in a C# windows form application. It has over 300 pictureboxes (walls) that are placed on a panel. The picturebox of bomberman himself is also on that panel.

When the location of bombermans picturebox changes, all the controls on the panel are automatically refreshed. Because there are so many controls on that panel, and because the location of the picturebox changes multiple times per second, the program becomes laggy when I try to move.

I want to have control over the refresh event of the panel (and it's controls), because I think my problem is solved when only the pictureboxes that need to be refreshed, are refreshed programmatically.

I hope someone can help me out with this!

Ruud.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耀眼的星火 2024-10-20 03:12:52

如果移动子项,则必须刷新父项,因为它可能需要绘制子项之前所在的区域。这也意味着(父母的)所有孩子都会刷新。

另外,使用这么多控件可能不是一个好主意。我建议您保留描述墙壁的数据结构,然后使用它在面板(或您的自定义控件)中绘制它。您可以通过在面板/父级别捕获鼠标/键盘事件来编写自己的点击测试逻辑(在墙边界内单击鼠标或键盘)。通过正确的组织数据结构,命中测试可以非常有效。

If you move the child, the parent has to be refreshed because it may need to draw the area where child was located previously. It would also mean that all children (of parent) would get refreshed.

OTH, using so many controls may not be a good idea. I would suggest you to keep data structures describing walls and then use it to draw it within Panel (or your custom control). You can write your own logic for hit testing (mouse or keyboard click within wall boundary) by capturing mouse/keyboard events at Panel/Parent level. With correct organization data structure, hit testing can be very efficient.

用心笑 2024-10-20 03:12:52

你正在尝试绘制整个形状,这肯定需要时间。如果您只想更改表单的一部分(在您的情况下是将炸弹人移动到新位置),则只需使要重新绘制的区域无效,然后将其传递给 Invalidate 方法。

做一些类似的事情。

//Invalidate previous position of bomberman
Rectangle invalid = new Rectangle(picturebox1.Location.x,picturebox1.Location.y,picturebox1.Width,picturebox1.Height);
Invalidate(invalid);
//Add code to move your picture box and then call above two lines again
invalid = new Rectangle(picturebox1.Location.x,picturebox1.Location.y,picturebox1.Width,picturebox1.Height);
Invalidate(invalid);

请注意,但类似的抛光代码也可以工作...

这是一个示例链接以供参考。 http://msdn.microsoft.com/en-us/library/ms229628.aspx

You are trying to paint the whole form which will surely take time . If you want to change only a part of the form, which in your case is Moving the bomberman to a new position, Only invalidate the area you want to repaint and then pass it to the Invalidate method.

Do something similar to this.

//Invalidate previous position of bomberman
Rectangle invalid = new Rectangle(picturebox1.Location.x,picturebox1.Location.y,picturebox1.Width,picturebox1.Height);
Invalidate(invalid);
//Add code to move your picture box and then call above two lines again
invalid = new Rectangle(picturebox1.Location.x,picturebox1.Location.y,picturebox1.Width,picturebox1.Height);
Invalidate(invalid);

Note sure but somthing similar polished code would work...

Here is a link to an example for reference. http://msdn.microsoft.com/en-us/library/ms229628.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文