.NET CF在基础控件上绘制时的双缓冲
我对一些完全由用户绘制的.NET Compact Framework控件使用了双缓冲,但我很难弄清楚如何对从另一个控件继承并在其上绘制的控件使用双缓冲 我有一个基于DataGrid的控件,它在标题上绘制 我的OnPaint方法是:.NET CF在基础控件上绘制时的双缓冲,.net,compact-framework,double-buffering,.net,Compact Framework,Double Buffering,我对一些完全由用户绘制的.NET Compact Framework控件使用了双缓冲,但我很难弄清楚如何对从另一个控件继承并在其上绘制的控件使用双缓冲 我有一个基于DataGrid的控件,它在标题上绘制 我的OnPaint方法是: Protected Overrides Sub OnPaint(ByVal pe as System.Windows.Forms.PaintEventArgs) MyBase.OnPaint(pe) CustomPaintHeaders(pe.Grap
Protected Overrides Sub OnPaint(ByVal pe as System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pe)
CustomPaintHeaders(pe.Graphics)
End Sub
CustomPaintHeaders只是使用一些自定义图形在基本DataGrid头的顶部绘制。有时,我看到基本的DataGrid标题被绘制的地方会闪烁,但顶部没有我自定义绘制的东西
是否可以使用双缓冲,并将MyBase.OnPaint完成的绘制应用于缓冲区图像
编辑:正如我在评论中提到的,我能够使用以下代码进行双缓冲:
Protected Overrides Sub OnPaint(ByVal pe as System.Windows.Forms.PaintEventArgs)
Using currentRender as Bitmap = New Bitmap(Me.Width, Me.Height)
Using gr as Graphics = Graphics.FromImage(currentRender)
CustomPaintHeaders(gr)
CustomPaintRows(gr)
End Using
End Using
End Sub
Private Sub CustomPaintHeaders(ByVal graphics as Graphics)
'Custom drawing stuff in place of DataGrid column headers
End Sub
'TEMP - draws rectangle in place of grid rows
Private Sub CustomPaintRows(ByVal graphics as Graphics)
graphics.DrawRectangle(New Pen(Me.ForeColor), 0, 20, Me.Width, Me.Height)
End Sub
这可以在不闪烁的情况下正常工作,但我希望避免实现CustomPaintRows,让DataGrid的OnPaint为我处理该部分,然后使用CustomPaintHeaders方法绘制其标题。CF中的双缓冲是一个手动过程,所以我假设您的基类包含一个它正在绘制的图像或位图?这完全取决于你的绘画方式,但你可以保护图像,也可以做一些更复杂的事情,比如:
protected virtual void OnPaint(graphics bufferGraphics) { }
void OnPaint(PaintEventArgs pe)
{
var buffer = new Bitmap(this.Width, this.Height);
var bufferGraphics = Graphics.FromImage(buffer);
// do base painting here
bufferGraphics.DrawString(....);
// etc.
// let any child paint into the buffer
OnPaint(bufferGraphics);
// paint the buffer to the screen
pe.Graphics.DrawImage(buffer, 0, 0);
}
然后在您的子对象中,只需覆盖新的OnPaint,并对传入的图形对象执行所需操作,该对象将绘制到缓冲区而不是屏幕上
如果希望子控件能够完全覆盖基础绘制,只需将基础绘制逻辑移动到虚拟方法中。My control继承System.Windows.Forms.DataGrid。我不确定DataGrid是如何绘制的。我的控件继承System.Windows.Forms.DataGrid。我不确定DataGrid是如何工作的,它正在绘制。当使用我的CustomPaintHeaders方法和CustomPaintRows方法时,我可以成功地进行双缓冲,就像上面所做的那样,使用一个新位图并使用其中的图形对象,而CustomPaintRows方法目前只绘制一个白色矩形来代替网格。有没有办法让DataGrid的普通OnPaint绘制成为缓冲区图像?所以我可以直接调用它和CustomPaintHeaders,而不必实际实现CustomPaintRow。您已经看过一些专门用于格式化CF数据网格的资源了吗?是的,我实际上使用了该页面来帮助我根据不同的条件使某些整行的颜色不同。没有弄清楚如何将绘画从MyBase.OnPaint应用到缓冲区,但我决定用简单的方法解决我的问题-使用双缓冲区进行自定义标题绘画,并将RowHeadersVisible设置为False,并使行绘制从自定义标题下方开始。