什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [英] What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))

查看:92
本文介绍了什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使程序停止并且点击停止调试后项目返回到设计模式,你能相信MessageBox可以显示吗?



我无法' t相信直到我尝试将执行MessageBox.Show(...)的命令插入到方法Paint(...)的主体中。



这是我的意思想知道何时调用此方法。我意识到每当重绘DataGridView时都会调用它,即使项目处于设计模式时也是如此。



你能解释一下它是如何工作的吗?据我所知,当某些事件发生时,将直接或间接调用该方法。但是我没有在属性窗口中看到任何与该方法相关联的事件,即使有一个事件(例如Paint事件),如何在所有调试或编译或运行停止时调用它?是的,如果你喜欢我,你不能继续设计和编码你的项目,因为每当包含datagridview的主窗体重新绘制或重新绘制名为Paint的playful方法时,它将显示一个消息框,单击OK按钮将重新重新绘制datagridview,并会出现一个新的消息框,这会产生一个调用Paint的循环。

是否有任何其他方法与该Paint相同?



非常感谢!

Can you believe the MessageBox can show even when the program stopped and the project returned to the design mode after clicking on "Stop debugging"?

I couldn't believe that until I tried inserting a command executing MessageBox.Show(...) into the body of the method Paint(...).

That's for I want to know when this method is called. I realized that it will be called whenever the DataGridView is redrawn, even when the project in design mode.

Could you please explain me how it works? As far as I know, the method will be called directly or indirectly when some event occurs. But I don't see any events associating with that method in the properties window, and even when there's one event for it (for example Paint event) how can it be called when all debugging or compiling or running stopped? Yes, if you do like me, you can't continue designing and coding your project because whenever the main form containing the datagridview with that "playful" method called Paint is redrawn or repainted, it will show a message box, clicking on the OK button will repaint the datagridview anew and a new message box will appear, that makes a loop of calling Paint.
Is there any other method of the same kind to that Paint?

Thank you so much!

推荐答案

将一个MsgBox放在Paint事件处理程序中是一个可怕的想法!消息框被解除后,猜猜会发生什么? Paint事件可能会再次发生!好吧,就是如果消息框覆盖了Paint处理程序代码所服务的控件/表单的任何部分。



将它替换为Debug.WriteLine或其他所以你可以在调试器中监视它而不会搞砸你的绘图代码。



代码实际上从未停止过。例如,当您创建自定义控件并且它正在设计器中显示时,即使您尚未在调试器中启动应用程序,您的控件代码实际上也是RUNNING。想一想。如果没有运行后面的代码,设计如何显示你的控件?
Putting a MsgBox in the Paint event handler is a HORRIBLE idea! When the messagebox is dismissed, guess what happens? The Paint event is probably going to fire again! Well, that is if the messagebox covered any part of the control/form your Paint handler code is servicing.

Replace it with Debug.WriteLine or something else so you can monitor it in the debugger without screwing up your painting code.

The code is never actually stopped. For example, when you create a custom control and it's being displayed in the designer, your controls code is actually RUNNING, even if you haven't started your app in the debugger. Think about it. How else is the designed going to show your control if it doesn't run the code behind it?


Dave是对的,但你需要一些解释。

是的,你需要了解它来编写其他程序。



机制并不简单。首先,响应Windows消息 WM_PAINT ,触发 OnPaint 。触发此消息是一件非常复杂的事情。您可以看到,当任何其他窗口覆盖的窗口的任何部分暴露出先前覆盖的表面的一部分时,将触发您的方法。所以它发生在一些窗口的运动上,在Alt + TAB上,在刷新&很多;案例。



现在,如何自动触发?这对于任何类型的动画都非常重要。不,这不是刷新或其他任何东西。最终,这只是一件事: Control.Invalidate 。您可以通过更改 OnPaint 使用的实例数据以及调用 Invalidate 来为渲染图片设置动画(或以其他方式修改) 。为了获得更好的性能,您可以使用Invalidate with parameter:a Rectangle Region 。最强大的东西是 Region :它支持所有类型的set-theory操作来构建渲染矩形的任何子集。



现在, WM_PAINT 有什么特别之处?这个事件的处理非常狡猾。它没有通过常规的Windows消息队列。相反,它是高度优化的。想象一下,在重新渲染(通过 OnPaint )尚未完成之前,你有两个无效的一个接一个到来,第二个被调用。你认为两次调用 OnPaint 吗?没门!消息将在优化过程中合并为一个,并且无效区域将被一起进行OR运算。有趣的机制,不是吗?



有些情况下,这种技术应该在应用程序级别上重现。使用 OnPaint 无效您无需担心。



-SA
Dave is right, but you need some explanation.
Yes, you need to understand it to write other programs.

The mechanism is not simple at all. First of all, OnPaint is triggered in response of the Windows message WM_PAINT. The triggering of this message is quite a complex thing. You can see that you method will be fired when any part of your window covered by any other window exposes part of the previously covered surface. So it happens on motion of some windows, on Alt+TAB, on refresh &many; cases.

Now, how to trigger it automatically? This is very important for any kind of animation. No, this is not Refresh or anything. Ultimately, this is only one thing: Control.Invalidate. You animate (or otherwise modify) the rendered picture by changing some instance data which is used by OnPaint and the call Invalidate. For better performance, you can use Invalidate with parameter: a Rectangle or Region. Most powerfull thing is Region: it supports all kind of set-theory operations to build any subset of the rendering rectangle.

Now, what's so special about WM_PAINT? The processing of this event is very cunning. It does not go through a regular Windows message queue. Instead, it is highly optimized. Imagine you have two invalidates coming one after another, with second one called before your re-rendering (through OnPaint) is not yet complete. Do you think OnPaint is called twice? No way! The messages will be merged together in one in the optimization process, and there invalidated regions will be ORed together. Interesting mechanism, isn't it?

There are cases when this technique should be reproduces on application level. With OnPaint and Invalidate you don't have to worry about it.

—SA


这篇关于什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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