简体   繁体   English

PictureBox绘制事件执行两次

[英]PictureBox paint event executing twice

I added a picture box to a WinForms form and added a paint event. 我在WinForms窗体中添加了一个图片框,并添加了一个paint事件。 When I debug it, I see that the paint event is called twice. 当我调试它时,我看到paint事件被调用了两次。

How can I fix it? 我该如何解决?

The picture box control is a relatively loose wrapper around Windows paint cycles and the WM_PAINT event. 图片框控件是围绕Windows绘制周期和WM_PAINT事件的一个相对松散的包装。

The WM_PAINT event is a queued message that the system places in your applications message queue whenever it deems that a window's drawing surface is invalid. WM_PAINT事件是排队的消息,只要系统认为窗口的绘图表面无效,系统就会将其放入应用程序消息队列中。 This can happen for many reasons: 发生这种情况的原因有很多:

  • Another window is dragged over the top of your window (although modern windows use buffering to alleviate this). 另一个窗口被拖到窗口顶部(尽管现代窗口使用缓冲来减轻这种情况)。
  • You invalidate the window by calling Invalidate which ultimately calls the Win32 InvalidateRect function. 您可以通过调用Invalidate来使窗口无效,该Invalidate最终将调用Win32 InvalidateRect函数。
  • System wide theme preferences changed. 系统范围的主题首选项已更改。
  • Etc. etc. 等等

So, the natural conclusion of this is that your picture box paint handler needs to be written under the assumption that it will be called multiple times. 因此,自然的结论是您的图片框绘画处理程序需要在被多次调用的假设下编写。

The paint event is fired on every redraw which is quite often. 每次重新绘制时都会触发一次paint事件。 The code in your paint event handler should be able to cope with it being called n-times. 绘制事件处理程序中的代码应能够应对它,称为n次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM