[英].NET CF double-buffering when painting over base control
我已經對完全由用戶繪制的某些.NET Compact Framework控件使用了雙緩沖,但是在弄清楚如何對從另一個控件繼承並對其進行繪制的控件使用雙緩沖時遇到了麻煩。
我有一個基於DataGrid的控件,它可以繪制標題。
我的OnPaint方法是:
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中的雙緩沖是一個手動過程,因此我假設您的基類擁有要在其上繪制圖像或位圖的圖像? 這完全取決於您的繪畫方式,但是您可以使Image protected
,也可以做一些稍微復雜的事情,例如:
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並使用傳入的Graphics對象(該對象將繪制在緩沖區而不是屏幕上)進行所需的操作。
如果希望孩子能夠完全覆蓋基礎繪畫,只需將基礎繪畫邏輯移到虛擬方法中即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.