Winforms TabControl导致UserControl的虚假绘制事件

Winforms TabControl导致UserControl的虚假绘制事件,winforms,events,user-controls,tabcontrol,Winforms,Events,User Controls,Tabcontrol,对于我们的项目,我们编写了一个用于绘图的WinForms UserControl 当我们的控件位于TabControl中时,我们会看到一些奇怪的行为——我们的控件不断地触发绘制事件,即使用户完全没有任何活动 我们只在选项卡控件中看到这一点。当我们将控件放置在其他容器(如窗体或拆分器)中时,仅在您预期的情况下(例如控件首次显示时)才会触发绘制 有人能提出为什么会发生这种情况吗 如果有帮助的话,下面是控件的绘制处理程序中断点的堆栈跟踪。我们的控件是BaseGraphXY,它位于TabControl上

对于我们的项目,我们编写了一个用于绘图的WinForms UserControl

当我们的控件位于TabControl中时,我们会看到一些奇怪的行为——我们的控件不断地触发绘制事件,即使用户完全没有任何活动

我们只在选项卡控件中看到这一点。当我们将控件放置在其他容器(如窗体或拆分器)中时,仅在您预期的情况下(例如控件首次显示时)才会触发绘制

有人能提出为什么会发生这种情况吗

如果有帮助的话,下面是控件的绘制处理程序中断点的堆栈跟踪。我们的控件是BaseGraphXY,它位于TabControl上,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}}) 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   

Tab控件确实有一些怪癖,请尝试修补SetStyle:


Me.SetStyle(ControlStyles.UserPaint或ControlStyles.AllPaintingInWmPaint或ControlStyles.OptimizedDoubleBuffer或ControlStyles.DoubleBuffer或ControlStyles.Selective或ControlStyles.ResizerDraw,True)

我的同事找到了解决问题的方法:


图形控件的问题是BackColor属性默认为System.Drawing.Color.Transparent。这造成了一种信息的“反馈循环”。我想应该是这样的?另外,我将构造函数更改为使用System.Drawing.SystemColor.ControlLight作为默认的BackColor属性。这似乎抑制了额外的图形绘制。

堆栈跟踪完全正常,这是在控件上调用Invalidate并启用双缓冲时所期望的。我从没听说过TabControl会引发这种情况。你用的是SetStyle吗?不,我们没有。也许我们应该-谢谢你的提示。