Windows窗体上的图形绘制问题 [英] Drawing Graphics on a Windows Form Problems

查看:71
本文介绍了Windows窗体上的图形绘制问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上绘制了2张图形.

1个图形是一个随机生成的星场,这些星会移动,并在计时器上的每个刻度上更新(50毫秒)

一旦绘制/更新了星星,便在该图像上覆盖了一个座舱,以呈现出在空间中移动的外观.

我遇到的问题是,星星被拉到循环的上一个迭代的驾驶舱上,从而暗示星星在船上.

我不知道如何解决这个问题,也不会提出任何建议.我试过让它在更新每颗星星后重新绘制驾驶舱,这只会使更新星星花费的时间太长.

一种选择是杀死每颗恒星,直到其到达驾驶舱窗口"的边缘,我担心这会导致同样的延迟问题,因为由于驾驶舱窗口甚至没有成形,它必须检查许多条件.

一位朋友建议使用双重缓冲-在VS中,我只是遇到过启用或禁用双重缓冲的功能.

任何帮助将不胜感激,特别是在是否要使用双缓冲方面.

如果需要的话,我可以提供代码,因为它太长了,使帖子看起来很混乱.

预先谢谢您.

I have 2 graphics I draw onto a form.

1 Graphic is a Randomly Generated Star Field, these stars move, and are updated every tick on a timer(50ms)

Once the stars have been drawn/updated a cockpit is overlayed onto this image to give the appearance of moving through space.

The problem I am having is that the stars are drawn onto the cockpit of the previous iteration of the loop giving the allusion that the stars are in the ship.

I have not a clue how to solve this and would love any advice. I have tried making it redraw the cockpit after it updates each star, this just makes updating the stars take too long.

One option is to kill each star once it reaches the edge of the cockpit''s "window" I fear though that this would cause an equal problem with delay as it would have to check many conditons as the cockpit window is not even in shape.

A friend has suggested double buffering - I have only ever came across this as an option in VS to Enable or Disable.

Any help would be appreciated, especially in regards to if Double Buffering is the way to go.

I can provide the code if need be it is just obnoxiously long and makes the post look quite messy.

Thank you in advance.

推荐答案

请查看我过去的回答:
在mdi子表单之间画线 [在面板上捕获图形 [ Paint是一种哪种好玩的方法? (DataGridViewImageCell.Paint(...)) [ Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
Please see my past answers:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

The point is: you always paint in Paint event or in overridden OnPaint, and in timer handle you only call Invalidate (if you use timers at all, I would rather recommend to use a thread instead, with the same rendering technique). Only this way, you get all control over rendering.

Also, never use System.Windows.Forms.Timer. It''s only good when you have no requirements on the accuracy at all, for example, if you don''t care if you wait twice or more as long. Yes, this is that bad.

And it can be much better to use a thread with Sleep instead of timer. If you want time, you can poll the value of System.DateTime.Now or Stopwatch every time you are about invalidation of the rendered scene. In can give you much better accuracy then a timer, but with timer, you can get additional hassles. Did you ever tried to resolve the situations when a timer event happens when the handler invoked by the previous timer tick is not yet completed? Timers are hard to deal with.

With other timers, the problem is that you the event is invoked in some other thread. So, anyway, you would need to use invocation mechanism. 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


让您的飞船没有透明区域.在绘画事件中都绘制,而不是使用随机计时器.这样,您就可以完全控制它们的绘制顺序
Make your spaceship with no areas of transparency. Draw both in your paint event, not with random timers. That way, you have complete control over the order they are drawn in


这篇关于Windows窗体上的图形绘制问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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