C# 自定义控件绘图 未处理 System.AccessViolationException异常

来源:互联网 发布:淘宝商城男装裤子 编辑:程序博客网 时间:2024/04/30 05:56

在自定义一个控件,绘图(DrawLine)的时候出现了System.AccessViolationException异常异常,如下

未处理 System.AccessViolationException  Message="尝试读取或写入受保护的内存。这通常指示其他内存已损坏。"  Source="System.Drawing"  StackTrace:       在 System.Drawing.SafeNativeMethods.Gdip.GdipDrawLineI(HandleRef graphics, HandleRef pen, Int32 x1, Int32 y1, Int32 x2, Int32 y2)       在 System.Drawing.Graphics.DrawLine(Pen pen, Int32 x1, Int32 y1, Int32 x2, Int32 y2)       在 SinUI.MultiWaveView.DrawOneWave(Graphics g, Pen pen, Int32 stix, Int32[] values) 位置 D:\MyDoc\WorkDir\VS\同步走行V2\MonitorCtl\MultiWaveView.cs:行号 101       在 SinUI.MultiWaveView.OnPaint(PaintEventArgs e) 位置 D:\MyDoc\WorkDir\VS\同步走行V2\MonitorCtl\MultiWaveView.cs:行号 115       在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)       在 System.Windows.Forms.Control.WmPaint(Message& m)       在 System.Windows.Forms.Control.WndProc(Message& m)       在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)       在 System.Windows.Forms.ContainerControl.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(Int32 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)       在 MonitorCtl.Program.Main() 位置 D:\MyDoc\WorkDir\VS\同步走行V2\MonitorCtl\Program.cs:行号 17       在 System.AppDomain._nExecuteAssembly(Assembly 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)       在 System.Threading.ThreadHelper.ThreadStart()

搞了半天,后来发现是没有开双缓冲的问题,OnLoad的时候打开控件的DoubleBuffered,或者直接设置其属性为true吧。

DoubleBuffered = true;


0 0
原创粉丝点击