I've used double-buffering for some .NET Compact Framework controls that are entirely user-drawn, but I'm having trouble figuring out how I can use double-buffering for a control that inherits from another control and paints over it.
I have a control based on DataGrid, that paints over the headers.
My OnPaint method is:
Protected Overrides Sub OnPaint(ByVal pe as System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pe)
CustomPaintHeaders(pe.Graphics)
End Sub
CustomPaintHeaders just paints on top of the basic DataGrid headers with some custom drawing. Occassionally, I get flickering where I see the basic DataGrid headers get painted, but without my custom-drawn stuff on top.
Is it possible to use double-buffering, and have the painting that's done by MyBase.OnPaint applied to the buffer image?
Edit: As mentioned in my comment, I am able to do double buffering using this code:
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
And this works fine without flickering, but I'd like to avoid having to implement CustomPaintRows, and just let DataGrid's OnPaint handle that part for me, and then draw over it's headers with my CustomPaintHeaders method.