C# winform中的Paint事件为什么执行不止一次啊

roobin2008 2012-07-19 04:46:04
窗口时默认最大化启动的,我在程序的override void OnPaint()事件中加了一个控制台打印语句,然后发现在窗口启动的时候Paint事件好像执行了6次,因为打印语句打印了6次,在窗口化时执行了5次,并且都会提示:A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.Drawing.dll 异常。

请问一下,paint事件是不是在每次绘制的时候不是只执行一次啊?
...全文
1236 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
tigerleq 2012-07-20
  • 打赏
  • 举报
回复
你 眼睛 能 看到 屏幕 里的所有 东西,只要发生 改变,必然 重新 绘制
roobin2008 2012-07-20
  • 打赏
  • 举报
回复
经过试验我OnPaint会被执行很多次,我也试验过了SizeChanged和Resize事件,也是会在窗口尺寸发生变化时执行不止一次,我想问的是这样的原因或者说是内部过程是怎样的
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

有没有这方面的详细的描述或者说明啊
[/Quote]
http://msdn.microsoft.com/zh-cn/library/system.windows.forms.control.paint.aspx
roobin2008 2012-07-19
  • 打赏
  • 举报
回复
有没有这方面的详细的描述或者说明啊
晚安苏州 2012-07-19
  • 打赏
  • 举报
回复
窗口拖动,被遮蔽,改变大小等都会产生消息,系统将消息传给程序,让程序对界面需要重绘部分进行重绘。
Mr_graceless 2012-07-19
  • 打赏
  • 举报
回复
最大化,最小化,改变窗口大小,都会执行的
qldsrx 2012-07-19
  • 打赏
  • 举报
回复
Paint事件只能用来添加自定义的界面绘制功能,其它功能代码不要在里面写,任何界面上的小刷新都会触发它。
烈火蜓蜻 2012-07-19
  • 打赏
  • 举报
回复
不要去了解他应该执行多少次,凡是可能改变外观的消息,都会触发Paint事件,所以别在意他执行了多少次。
roobin2008 2012-07-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
顾名思义,在绘制窗口的时候就会发生
[/Quote]

那就是说每次显示实际上是发生了多次绘制事件?
roobin2008 2012-07-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
不要认为首次显示窗体的时候OnPaint只执行一次。
[/Quote]

这个是为什么啊,能说的详细点吗?
还有,如果OnPaint执行了不止一次,那么OnPaint里是不是就不能执行会对程序以后的处理有影响的操作,比方说修改一个全局变量?
cnfixit 2012-07-19
  • 打赏
  • 举报
回复
顾名思义,在绘制窗口的时候就会发生
roobin2008 2012-07-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
窗体显示的时候会执行次,你操作了窗体也会执行
[/Quote]
这个操作是指什么,点击之类的吗,在我没点击什么的情况下就是执行了多次
isjoe 2012-07-19
  • 打赏
  • 举报
回复
不要认为首次显示窗体的时候OnPaint只执行一次。

luhuiming741cs 2012-07-19
  • 打赏
  • 举报
回复
窗体显示的时候会执行次,你操作了窗体也会执行
这篇文章我们重点需要实现的是(3)、(4)两项功能,下面我们来介绍具体实现的方法。 第一步,实现ImageComboBoxItem类。 要实现显示图标,当然要给每个项添加与图标相关的信息了,ImageComboBoxItem类应该包括以下内容:文本(Text)、缩进的级别(Level)、图标的索引(ImageIndex、ImageKey),用户数据(Tag)。ImageComboBoxItem类实现了ISerializable接口,实现自定义序列化。ImageComboBoxItem类的类视图如下: 图3 ImageComboxItem类视图 ImageComboBoxItem类的代码如下: [Serializable] [DefaultProperty("Text")] [TypeConverter( typeof(ExpandableObjectConverter))] public class ImageComboBoxItem : IDisposable, ISerializable ...{ Fields#region Fields private ImageComboBox _imageComboBox; private string _text = "ImageComboBoxItem"; private ImageComboBoxItemImageIndexer _imageIndexer; private object _tag; private int _level; #endregion Constructors#region Constructors public ImageComboBoxItem() ...{ } public ImageComboBoxItem(string text) : this(text, -1, 0) ...{ } public ImageComboBoxItem( string text, int imageIndex) : this(text, imageIndex, 0) ...{ } public ImageComboBoxItem( string text, string imageKey) : this(text, imageKey, 0) ...{ } public ImageComboBoxItem( string text, int imageIndex, int level) : this() ...{ _text = text; ImageIndexer.Index = imageIndex; _level = level; } public ImageComboBoxItem( string text, string imageKey, int level) : this() ...{ _text = text; ImageIndexer.Key = imageKey; _level = level; } protected ImageComboBoxItem( SerializationInfo info, StreamingContext context) : this() ...{ Deserialize(info, context); } #endregion Properties#region Properties [Localizable(true)]

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧