




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
使用双缓冲的图形可以减少或消除重绘显示图面时产生的闪烁。使用双缓冲时,更新的图形首先被绘制到内存的缓冲区中,然后,此缓冲区的内容被迅速写入某些或所有显示的图面中。显示图形的重写相对简短,这通常可以减少或消除有时在更新图形时出现的闪烁。 使用C# GDI+绘图,实现双缓冲绘图有几种方法,在这篇文章中,将介绍其中的一种使用BufferedGraphics实现GDI+双缓冲绘图。 下面的代码示例演示如何使用BufferedGraphics对象绘制以下图形,这些图形使用几种类型的缓冲实现。单击窗体将启动或停止一个计时器,该计时器将引起绘制更新。绘制更新使您可以观察双缓冲的效果。右击窗体将循环使用下列绘制模式: 对于Form,直接绘制到Handle。 对使用OptimizedDoubleBuffer控件样式的OnPaint方法进行重写,以进行绘制 对于不使用OptimizedDoubleBuffer控件样式的窗体方法,重写OnPaint方法以进行绘制。 在每种模式下都将绘制文本,以标识当前模式并描述按下每个鼠标按钮时发生的行为。using System;using System.ComponentModel;using System.Drawing;using System.Windows.Forms;namespace BufferingExample public class BufferingExample : Form private BufferedGraphicsContext context; private BufferedGraphics grafx; private byte bufferingMode; private string bufferingModeStrings = Draw to Form without OptimizedDoubleBufferring control style, Draw to Form using OptimizedDoubleBuffering control style, Draw to HDC for form ; private System.Windows.Forms.Timer timer1; private byte count; public BufferingExample() : base() / Configure the Form for this example. this.Text = User double buffering; this.MouseDown += new MouseEventHandler(this.MouseDownHandler); this.Resize += new EventHandler(this.OnResize); this.SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true ); / Configure a timer to draw graphics updates. timer1 = new System.Windows.Forms.Timer(); timer1.Interval = 200; timer1.Tick += new EventHandler(this.OnTimer); bufferingMode = 2; count = 0; / Retrieves the BufferedGraphicsContext for the / current application domain. context = BufferedGraphicsManager.Current; / Sets the maximum size for the primary graphics buffer / of the buffered graphics context for the application / domain. Any allocation requests for a buffer larger / than this will create a temporary buffered graphics / context to host the graphics buffer. context.MaximumBuffer = new Size(this.Width+1, this.Height+1); / Allocates a graphics buffer the size of this form / using the pixel format of the Graphics created by / the Form.CreateGraphics() method, which returns a / Graphics object that matches the pixel format of the form. grafx = context.Allocate(this.CreateGraphics(), new Rectangle( 0, 0, this.Width, this.Height ); / Draw the first frame to the buffer. DrawToBuffer(grafx.Graphics); private void MouseDownHandler(object sender, MouseEventArgs e) if( e.Button = MouseButtons.Right ) / Cycle the buffering mode. if( +bufferingMode 2 ) bufferingMode = 0; / If the previous buffering mode used / the OptimizedDoubleBuffering ControlStyle, / disable the control style. if( bufferingMode = 1 ) this.SetStyle( ControlStyles.OptimizedDoubleBuffer, true ); / If the current buffering mode uses / the OptimizedDoubleBuffering ControlStyle, / enabke the control style. if( bufferingMode = 2 ) this.SetStyle( ControlStyles.OptimizedDoubleBuffer, false ); / Cause the background to be cleared and redraw. count = 6; DrawToBuffer(grafx.Graphics); this.Refresh(); else / Toggle whether the redraw timer is active. if( timer1.Enabled ) timer1.Stop(); else timer1.Start(); private void OnTimer(object sender, EventArgs e) / Draw randomly positioned ellipses to the buffer. DrawToBuffer(grafx.Graphics); / If in bufferingMode 2, draw to the forms HDC. if( bufferingMode = 2 ) / Render the graphics buffer to the forms HDC. grafx.Render(Graphics.FromHwnd(this.Handle); / If in bufferingMode 0 or 1, draw in the paint method. else this.Refresh(); private void OnResize(object sender, EventArgs e) / Re-create the graphics buffer for a new window size. context.MaximumBuffer = new Size(this.Width+1, this.Height+1); if( grafx != null ) grafx.Dispose(); grafx = null; grafx = context.Allocate(this.CreateGraphics(), new Rectangle( 0, 0, this.Width, this.Height ); / Cause the background to be cleared and redraw. count = 6; DrawToBuffer(grafx.Graphics); this.Refresh(); private void DrawToBuffer(Graphics g) / Clear the graphics buffer every five updates. if( +count 5 ) count = 0; grafx.Graphics.FillRectangle(Brushes.Black, 0, 0, this.Width, this.Height); / Draw randomly positioned and colored ellipses. Random rnd = new Random(); for( int i=0; i20; i+ ) int px = rnd.Next(20,this.Width-40); int py = rnd.Next(20,this.Height-40); g.DrawEllipse(new Pen(Color.FromArgb(rnd.Next(0, 255), rnd.Next(0,255), rnd.Next(0,255), 1), px, py, px+rnd.Next(0, this.Width-px-20), py+rnd.Next(0, this.Height-py-20); / Draw information strings. g.DrawString(Buffering Mode: +bufferingModeStringsbufferingMode, new Font(Arial, 8), Brushes.White, 10, 10); g.DrawString(Right-click to cycle buffering mode, new Font(Arial, 8), Brushes.White, 10, 22); g.DrawString(Left-cli
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030中国制药行业清洁生产技术应用与减排效益分析报告
- 2025年宝鸡方塘高级中学教师招聘(34人)模拟试卷附答案详解
- 2025年智能制造与工业自动化
- 2025年智能音箱的用户行为与市场分析
- 2025年4月广东广州市天河区华港幼儿园编外聘用制专任教师招聘1人模拟试卷及参考答案详解1套
- 2025年福建省三明市尤溪县总医院招聘10人考前自测高频考点模拟试题及答案详解(新)
- 2025广西防城港市防城区政务服务监督管理办公室招聘1人考前自测高频考点模拟试题附答案详解(典型题)
- 2025广西河池市巴马瑶族自治县消防救援大队招录3人考前自测高频考点模拟试题含答案详解
- 2025广东深圳市九洲电器有限公司关IQC招聘1人考前自测高频考点模拟试题附答案详解(完整版)
- 2025年温州市法院系统招聘真题
- 青春期生殖健康教育
- 2025年BM²T电池管理技术白皮书-阳光电源
- 2024-2025学年下学期高二英语外研社版期中必刷常考题之被动语态
- 中医诊所招学徒合同标准文本
- 汉语言文学毕业论文-鲁迅小说中的知识分子形象
- 长期供应商供货合同书
- 如何缓解焦虑和压力
- 垃圾分类志愿服务
- ccusg重症超声培训班题库
- 冀教版八年级数学 13.4 三角形的尺规作图(学习、上课课件)
- 2024年锅炉操作工(技师)职业鉴定理论考试题库(含答案)
评论
0/150
提交评论