欢迎来到天天文库
浏览记录
ID:37709219
大小:165.00 KB
页数:9页
时间:2019-05-29
《C#使用BufferedGraphics实现GDI+双缓冲绘图》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、使用双缓冲的图形可以减少或消除重绘显示图面时产生的闪烁。使用双缓冲时,更新的图形首先被绘制到内存的缓冲区中,然后,此缓冲区的内容被迅速写入某些或所有显示的图面中。显示图形的重写相对简短,这通常可以减少或消除有时在更新图形时出现的闪烁。 使用C#GDI+绘图,实现双缓冲绘图有几种方法,在这篇文章中,将介绍其中的一种——使用BufferedGraphics实现GDI+双缓冲绘图。 下面的代码示例演示如何使用 BufferedGraphics 对象绘制以下图形,这些图形使用几种类型的缓冲实现。单击窗体将启动或停止一个计时器,该计时器将引起绘制
2、更新。绘制更新使您可以观察双缓冲的效果。右击窗体将循环使用下列绘制模式:·对于 Form,直接绘制到 Handle。·对使用 OptimizedDoubleBuffer 控件样式的 OnPaint 方法进行重写,以进行绘制·对于不使用 OptimizedDoubleBuffer 控件样式的窗体方法,重写 OnPaint 方法以进行绘制。 在每种模式下都将绘制文本,以标识当前模式并描述按下每个鼠标按钮时发生的行为。usingSystem;usingSystem.ComponentModel;usingSystem.Drawing;usingSys
3、tem.Windows.Forms;namespaceBufferingExample{publicclassBufferingExample:Form{privateBufferedGraphicsContextcontext;privateBufferedGraphicsgrafx;privatebytebufferingMode;privatestring[]bufferingModeStrings={"DrawtoFormwithoutOptimizedDoubleBufferringcontrolstyle","DrawtoFormusi
4、ngOptimizedDoubleBufferingcontrolstyle","DrawtoHDCforform"};privateSystem.Windows.Forms.Timertimer1;privatebytecount;publicBufferingExample():base(){//ConfiguretheFormforthisexample.this.Text="Userdoublebuffering";this.MouseDown+=newMouseEventHandler(this.MouseDownHandler);thi
5、s.Resize+=newEventHandler(this.OnResize);this.SetStyle(ControlStyles.AllPaintingInWmPaint
6、ControlStyles.UserPaint,true);//Configureatimertodrawgraphicsupdates.timer1=newSystem.Windows.Forms.Timer();timer1.Interval=200;timer1.Tick+=newEventHandler(this.OnTimer);bufferingMode=2;
7、count=0;//RetrievestheBufferedGraphicsContextforthe//currentapplicationdomain.context=BufferedGraphicsManager.Current;//Setsthemaximumsizefortheprimarygraphicsbuffer//ofthebufferedgraphicscontextfortheapplication//domain.Anyallocationrequestsforabufferlarger//thanthiswillcreat
8、eatemporarybufferedgraphics//contexttohostthegraphicsbuffer.context.MaximumBuffer=newSize(this.Width+1,this.Height+1);//Allocatesagraphicsbufferthesizeofthisform//usingthepixelformatoftheGraphicscreatedby//theForm.CreateGraphics()method,whichreturnsa//Graphicsobjectthatmatches
9、thepixelformatoftheform.grafx=context.Allocate(this.CreateGra
此文档下载收益归作者所有