我正在看这个网站http://www.bobpowell.net/doublebuffer.htm关于双缓冲。我喜欢它的想法,并希望在我的RichTextBox类上使用它,因为我试图在框中保留一些(10k +)行,每次滚动或更改字符时,将可查看的文本复制到字符串和位置在另一个rtb上是语法颜色。问题是,当我开始滚动时,我的rtb中存在非常糟糕的闪烁,其中包含所有文本。我想等到用户完成滚动(如果他们滚动多个页面)以重绘rtb中的文本。双缓冲有助于此吗?我如何从列出的网站实际“绘制”rtb中的文本,如此方法?
方法:
protected override void OnPaint(PaintEventArgs e)
{
if(_backBuffer==null)
{
_backBuffer=new Bitmap(this.ClientSize.Width,this.ClientSize.Height);
}
Graphics g=Graphics.FromImage(_backBuffer);
//Paint your graphics on g here
g.Dispose();
//Copy the back buffer to the screen
e.Graphics.DrawImageUnscaled(_backBuffer,0,0);
//base.OnPaint (e); //optional but not recommended
}
编辑: 您还必须将TextBox样式设置为此
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer, true);