究竟双缓冲区是怎么使用的 才能够防止一会白一会有画的闪烁?
请高手给我个最简单的例子好吗
请高手给我个最简单的例子好吗
解决方案 »
- 100分求解,如何获取一下字符串内的值
- [ 200 分 在 线 等 ] C# WinForm : 关 于 键 盘 事 件 的 处 理 ( 获 取 按 键 信 息 )
- 怎样根据xsd文件生成xml?
- 求remoting在access数据库中的远程查询
- 这2条语句是否一样
- 在datagrid裡加入其它控制項如檢查列表框後怎麼對其進行處理,能給個例子和說明嗎?謝謝!
- 急!急!急!哪位好心人帮帮小弟啊!!!!万分感谢!!!!
- :)
- 请教一个关于多人开发中代码规范和风格统一的问题
- 协议打包,解包的问题 socket
- 请高手解释一下C#中Marshal是什么东西?qqchen79(知秋一叶 [MS MVP])引出的问题!
- C#中怎样可以做出可停靠的浮动窗口?
using System.Drawing;
using System.Windows.Forms;class Test : Form
{
private Bitmap bufferBmp; //缓冲区Bitmap
public Test()
{
bufferBmp = new Bitmap(this.Width,this.Height); //这里忽略了在窗体大小变化时,要相应的改变BufferBmp的大小,你可以自己加上去 ^-^
this.Paint+= new PaintEventHandler(Test_OnPaint);
} private void Test_OnPaint(object sender,PaintEventArgs e)
{
Graphics g = Graphics.FromImage(bufferBmp); //这个Graphics是来自bufferBmp的,用它画图是画在bufferBmp上的
//Do sth using g as you wish...
g.Dispose(); //这时,绘画已完成,释放掉g
Graphics grfx = e.Graphics; //这个Graphics是来自Screen的,用它画图都直接画到屏幕上
grfx.DrawImage(0,0,bufferBmp); //将bufferBmp中的内容画到屏幕上
}
}这样做的好处就是你可以一次性的将要画的都画出来,然后一起显示给用户。如果不这样而直接画,假如先画二十条直线,再画十个椭圆,那么使用一个不是太快的机器的用户就会看到这些是以一个先后次序画出来的,效果不太好;如果图像是时常更新的,就会有闪烁。还有一点就是,往内存中的缓冲区画是比向显示器画要快的,因而用双缓冲还可以提高速度。
ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfsystemdrawinggraphicsclassfromimagetopic.htm以下为其中的一段例程
public void FromImageImage(PaintEventArgs e){// Create image.Image imageFile = Image.FromFile("SampImag.jpg");// Create graphics object for alteration.Graphics newGraphics = Graphics.FromImage(imageFile);// Alter image.newGraphics.FillRectangle(new SolidBrush(Color.Black), 100, 50, 100, 100);// Draw image to screen.e.Graphics.DrawImage(imageFile, new PointF(0.0F, 0.0F));// Release graphics object.newGraphics.Dispose();}
工作过程就是先利用一个Image对象imageFile作为内存的画图底板,画好后用e.Graphics.DrawImage(imageFile, new PointF(0.0F, 0.0F));
快速画到前台,绝对不会闪烁。
不过Tigatron好像在最后一句犯了错误grfx.DrawImage(0,0,bufferBmp);
应该是grfx.DrawImage(bufferBmp,0,0);
呵呵,人总会犯错的,没事没事,^_^感谢各位帮我这个菜鸟
this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);
this.SetStyle(ControlStyles.UserPaint,true);
this.SetStyle(ControlStyles.DoubleBuffer,true);
不过这样会屏蔽WM_ERASEBKGND消息