471,357 Members | 1,122 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

double buffering a windows form

I have read plenty about applying double buffering for animation and self
drawn forms. Is there a way to apply it to a form with many standard
controls on it (textboxes, labels etc) ?? I have tried using
myForm.SetStyles(ControlStyles.DoubleBuffer, True) however it has made no
impact on the drawing operation. Any clarification would be greatly
appreciated

-Alex
Jul 21 '05 #1
3 2780
http://www.bobpowell.net/doublebuffer.htm

Double buffering will only affect the Form.OnPaint method, which you
will have to override if you use the techniques described in the link
above. The controls on your form all have their own OnPaint methods,
which have nothing to do with the form's double buffering. You would
have to implement double buffering on each individual control...

Jul 21 '05 #2
I tried implementing double buffering on a single control without success.
Any idea what im missing here? The datagrid no longer paints at all, its
just a black box.

The form event
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
If m_buffer Is Nothing Then
m_buffer = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
dg.Buffer = m_buffer
End If

Dim g As Graphics = Graphics.FromImage(m_buffer)
g.Clear(SystemColors.Control)
g.Dispose()

'Copy the back buffer to the screen

e.Graphics.DrawImageUnscaled(m_buffer, 0, 0)
End Sub

Protected Overrides Sub OnPaintBackground(ByVal pevent As
PaintEventArgs)
'Don't allow the background to paint
End Sub

Protected Overrides Sub OnSizeChanged(ByVal e As EventArgs)
If Not (m_buffer Is Nothing) Then
m_buffer.Dispose()
m_buffer = Nothing
End If

MyBase.OnSizeChanged(e)
End Sub
The derived class
Public Class BufferedDataGrid
Inherits DataGrid

Private m_buffer As Bitmap

Public Property Buffer() As Bitmap
Get
Return m_buffer
End Get
Set(ByVal Value As Bitmap)
m_buffer = Value
End Set
End Property

Public Sub New()
SetStyle(ControlStyles.AllPaintingInWmPaint Or
ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
End Sub

Protected Overrides Sub OnPaint(ByVal pe As
System.Windows.Forms.PaintEventArgs)
If m_buffer Is Nothing Then Return
Dim peArgs As New PaintEventArgs(Graphics.FromImage(m_buffer),
pe.ClipRectangle)
MyBase.OnPaint(peArgs)
End Sub

End Class

"Jamin Guy" <ja******@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
http://www.bobpowell.net/doublebuffer.htm

Double buffering will only affect the Form.OnPaint method, which you
will have to override if you use the techniques described in the link
above. The controls on your form all have their own OnPaint methods,
which have nothing to do with the form's double buffering. You would
have to implement double buffering on each individual control...

Jul 21 '05 #3
I'm not sure if you can make a custom drawn DataGrid, but here is an
example of a custom StatusBar that I double-buffered.

public class StatusBarEx : StatusBar
{
private int xBuffer = 4, yBuffer = 4;

public StatusBarEx()
{
InitializeComponent();

//
// TODO: Add constructor logic here
//

this.SetStyle(ControlStyles.DoubleBuffer |ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw, true);
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
Rectangle drawingRect = new Rectangle(new Point(xBuffer, yBuffer),
new Size(this.ClientRectangle.Width - xBuffer,
this.ClientRectangle.Height - yBuffer));
e.Graphics.DrawString(this.Text, this.Font, new
SolidBrush(Color.DimGray), drawingRect);
}

private void InitializeComponent()
{
//
// StatusBarEx
//
this.Font = new System.Drawing.Font("Arial", 9.75F,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));

}

protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged (e);

this.Refresh();
}

Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jason | last post: by
1 post views Thread by Kuba Florczyk | last post: by
2 posts views Thread by Millennium Falcon | last post: by
2 posts views Thread by Dan Neely | last post: by
3 posts views Thread by Alex Glass | last post: by
7 posts views Thread by Rain | last post: by
1 post views Thread by TyBreaker | last post: by
3 posts views Thread by ssoffline | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.