我有几个自定义的(winforms)组件,使用GDI+绘制到屏幕上。
为了防止重绘时出现闪烁,我决定启用双缓冲,所以我在构造函数中添加了一行代码:
public ColourWheel()
{
InitializeComponent();
this.DoubleBuffered = true;
}
这个代码在组件(ColorWheel)上运行良好。 当我将相同的代码添加到其他两个(结构类似)组件的构造函数中时,我会遇到一些奇怪的症状:
- 当我尝试运行包含该组件的窗体时,会出现参数异常
Application.Run(new Form());
。 - 如果我切换到设计模式,我会得到一个关于组件有未处理的异常与参数有关的错误。
值得一提的是,我也尝试了一些其他双缓冲技术。
是什么导致双缓冲可以在一个组件上工作,而在其他组件上不起作用?
编辑:这里是运行时症状的异常细节:
System.ArgumentException未被处理。消息=参数无效。源=System.Drawing。堆栈跟踪: 在System.Drawing.Graphics.GetHdc() 在System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC,BufferedGraphics buffer) 在System.Drawing.BufferedGraphics.Render() 在System.Windows.Forms.Control.WmPaint(Message& m) 在System.Windows.Forms.Control.WndProc(Message& m) 在System.Windows.Forms.ScrollableControl.WndProc(Message& m) 在System.Windows.Forms.UserControl.WndProc(Message& m) 在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData) 在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context) 在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context) 在System.Windows.Forms.Application.Run(Form mainForm) 在TestForm.Program.Main()中D:\ Documents and Settings \ Tom Wright \ My Documents \ Visual Studio 2010 \ Projects \ ColourPicker \ TestForm \ Program.cs:第18行 在System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args) 在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean ignoreSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state) 在System.Threading.ThreadHelper.ThreadStart() InnerException:
编辑2:导致问题的两个组件之一(更复杂的那个)的OnPaint处理程序:
private void ValueSlider_Paint(object sender, PaintEventArgs e)
{
using (Graphics g = e.Graphics)
{
g.DrawImage(this.gradientImage, new Rectangle(0, 0, paintArea.Width, paintArea.Height));
if (this.showmarker)
{
ColourHandler.HSV alt = ColourHandler.RGBtoHSV(new ColourHandler.RGB(this.SelectedColour.R, this.SelectedColour.G, this.SelectedColour.B));
alt.Saturation = 0;
alt.value = 255 - alt.value;
using (Pen pen = new Pen(ColourHandler.HSVtoColour(alt)))
{
pen.Width = (float)MARKERWIDTH;
g.DrawRectangle(pen, 0 - pen.Width, this.brightnessPoint.Y - MARKERWIDTH, this.paintArea.Width + (pen.Width * 2), MARKERWIDTH * 2);
}
}
}
}