Winforms TabControl导致UserControl的虚拟Paint事件 [英] Winforms TabControl causing spurious Paint events for UserControl

查看:160
本文介绍了Winforms TabControl导致UserControl的虚拟Paint事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我们的项目,我们编写了一个WinForms UserControl进行图形化。



当我们的控件位于TabControl中时,我们看到一些奇怪的行为 - 我们的控件连续触发Paint事件,即使用户绝对没有任何活动。



我们只在TabControl中看到这一点。当我们对其他容器(如表格或拆分器)进行控制时,只有当您期望例如当控件首次显示时,等等。



任何人都可以提出为什么会发生这种情况?



从我们控件的Paint处理程序中的断点堆栈跟踪,如果有任何帮助。我们的控件是BaseGraphXY,它位于一个TabControl上,该对象位于一些嵌套的SplitContainers上。很抱歉,格式化 - 由于某些原因,无法让SO编辑器停止包装。

  OverlordFrontEnd.exe!OverlordFrontEnd。 MainForm.graphControl_Paint(object sender = BI_BaseGraphXY.BaseGraphXY},System.Windows.Forms.PaintEventArgs e = {ClipRectangle = {X = 0,Y = 0,Width = 1031,Height = 408}})行422 C#
(System.Windows.Forms.PaintEventArgs e)+ 0x73 bytes
BI_AppCore.dll! PaintEventArgs e = {ClipRectangle = {X = 0,Y = 0,Width = 1031,Height = 408}})行377 + 0xb bytes C#
System.Windows.Forms.dll!System.Windows.Forms.Control .PaintTransparentBackground(System.Windows.Forms.PaintEventArgs e,System.Drawing.Rectangle rectangle,System.Drawing.Region transparentRegion = null)+ 0x16c bytes
System.Windows.Forms.dll!System.Windows.Forms.Control .PaintBackground(System.Windows.Forms.PaintEventArgs e = {ClipRectangle = {X = 0,Y = 0,Widt h = 1029,Height = 406}},System.Drawing.Rectangle矩形,System.Drawing.Color backColor,System.Drawing.Point scrollOffset)+ 0xbc bytes
System.Windows.Forms.dll!System.Windows。 Forms.Control.PaintBackground(System.Windows.Forms.PaintEventArgs e,System.Drawing.Rectangle rectangle)+ 0x63 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.OnPaintBackground(System.Windows .Forms.PaintEventArgs pevent)+ 0x59 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.PaintWithErrorHandling(System.Windows.Forms.PaintEventArgs e = {ClipRectangle = {X = 0,Y = 0,Width = 1029,Height = 406}},short layer,bool disposeEventArgs = false)+ 0x74 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.WmPaint(ref System.Windows。 Forms.Message m)+ 0x1ba bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m)+ 0x33e bytes
System.Windows .Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMess age(ref System.Windows.Forms.Message m)+ 0x10 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m)+ 0x31 bytes
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd,int msg = 15,System.IntPtr wparam,System.IntPtr lparam)+ 0x5a bytes
[本机管理转换]
[管理到本地转换]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop (int dwComponentID,int reason = -1,int pvLoopData = 0)+ 0x24e bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason = -1,System。 Windows.Forms.ApplicationContext context = {Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.WinFormsAppContext})+ 0x177 bytes
System.Windows.Forms.dll!System.Windows.Forms.Applicat ion.ThreadContext.RunMessageLoop(int reason,System.Windows.Forms.ApplicationContext context)+ 0x61 bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.ApplicationContext上下文)+ 0x18 bytes
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()+ 0x81 bytes
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() + 0xef bytes
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(string [] commandLine)+ 0x2c0 bytes
OverlordFrontEnd.exe!OverlordFrontEnd.Program.Main()行36 + 0x10字节C#
[本机到托管转换]
[管理到本地转换]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile,System.Security.Policy.Evidence assemblySecurity,string [ ] args)+ 0x3a bytes
Microsoft.VisualStudio.HostingProc ess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()+ 0x2b bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state)+ 0x66 bytes
mscorlib.dll! System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,object state)+ 0x6f bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()+ 0x44 bytes $ b我的同事找到了一个解决方法:



图形控件的问题是BackColor属性默认为System.Drawing.Color.Transparent。这导致了一种消息的反馈循环。我想这应该是这样工作的?总之,我更改了构造函数,使用System.Drawing.SystemColors.ControlLightLight作为默认的BackColor属性。这似乎压制了额外的图画。


For our project, we've written a WinForms UserControl for graphing.

We're seeing some strange behavior when our control is sited in a TabControl - our control continuously fires Paint events, even when there is absolutely no activity by the user.

We only see this in the TabControl. When we site our control in other containers such as Forms or Splitters, Paint is only fired when you'd expect e.g. when the control is first displayed, etc.

Can anyone suggest why this might be happening?

Here's a stack trace from a breakpoint in our control's Paint handler, if that's any help. Our control is BaseGraphXY, which is sited on a TabControl, which is sited on some nested SplitContainers. Sorry about the formatting - couldn't get the SO editor to stop wrapping, for some reason.

OverlordFrontEnd.exe!OverlordFrontEnd.MainForm.graphControl_Paint(object sender =  BI_BaseGraphXY.BaseGraphXY}, System.Windows.Forms.PaintEventArgs e = {ClipRectangle = {X=0,Y=0,Width=1031,Height=408}}) Line 422 C#
System.Windows.Forms.dll!System.Windows.Forms.Control.OnPaint(System.Windows.Forms.PaintEventArgs e) + 0x73 bytes   
BI_AppCore.dll!BI_BaseGraphXY.BaseGraphXY.OnPaint(System.Windows.Forms.PaintEventArgs e = {ClipRectangle = {X=0,Y=0,Width=1031,Height=408}}) Line 377 + 0xb bytes   C#
System.Windows.Forms.dll!System.Windows.Forms.Control.PaintTransparentBackground(System.Windows.Forms.PaintEventArgs e, System.Drawing.Rectangle rectangle, System.Drawing.Region transparentRegion = null) + 0x16c bytes   
System.Windows.Forms.dll!System.Windows.Forms.Control.PaintBackground(System.Windows.Forms.PaintEventArgs e = {ClipRectangle = {X=0,Y=0,Width=1029,Height=406}}, System.Drawing.Rectangle rectangle, System.Drawing.Color backColor, System.Drawing.Point scrollOffset) + 0xbc bytes    
System.Windows.Forms.dll!System.Windows.Forms.Control.PaintBackground(System.Windows.Forms.PaintEventArgs e, System.Drawing.Rectangle rectangle) + 0x63 bytes   
System.Windows.Forms.dll!System.Windows.Forms.Control.OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent) + 0x59 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Control.PaintWithErrorHandling(System.Windows.Forms.PaintEventArgs e = {ClipRectangle = {X=0,Y=0,Width=1029,Height=406}}, short layer, bool disposeEventArgs = false) + 0x74 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Control.WmPaint(ref System.Windows.Forms.Message m) + 0x1ba bytes 
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) + 0x33e bytes 
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) + 0x10 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) + 0x31 bytes  
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, int msg = 15, System.IntPtr wparam, System.IntPtr lparam) + 0x5a bytes  
[Native to Managed Transition]  
[Managed to Native Transition]  
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(int dwComponentID, int reason = -1, int pvLoopData = 0) + 0x24e bytes 
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason = -1, System.Windows.Forms.ApplicationContext context = {Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.WinFormsAppContext}) + 0x177 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x61 bytes    
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.ApplicationContext context) + 0x18 bytes 
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() + 0x81 bytes    
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() + 0xef bytes   
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(string[] commandLine) + 0x2c0 bytes 
OverlordFrontEnd.exe!OverlordFrontEnd.Program.Main() Line 36 + 0x10 bytes   C#
[Native to Managed Transition]  
[Managed to Native Transition]  
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x3a bytes    
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x2b bytes  
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x66 bytes   
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x6f bytes    
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes   

解决方案

My co-worker has found a workaround for the problem:

The problem with the graph control was that the BackColor property was defaulting to System.Drawing.Color.Transparent. This caused a kind of a "feedback loop" of messages. I guess that it is supposed to work this way? Anway, I changed the constructor to use System.Drawing.SystemColors.ControlLightLight as the default BackColor property. This seems to suppress the extra graph painting.

这篇关于Winforms TabControl导致UserControl的虚拟Paint事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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