How can I set the protected DoubleBuffered
property of the controls on a form that are suffering from flicker?
Here's a more generic version of Dummy's solution.
We can use reflection to get at the protected DoubleBuffered property, and then it can be set to true.
Note: You should pay your developer taxes and not use double-buffering if the user is running in a terminal services session (e.g. Remote Desktop) This helper method will not turn on double buffering if the person is running in remote desktop.
public static void SetDoubleBuffered(System.Windows.Forms.Control c)
{
//Taxes: Remote Desktop Connection and painting
//http://blogs.msdn.com/oldnewthing/archive/2006/01/03/508694.aspx
if (System.Windows.Forms.SystemInformation.TerminalServerSession)
return;
System.Reflection.PropertyInfo aProp =
typeof(System.Windows.Forms.Control).GetProperty(
"DoubleBuffered",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
aProp.SetValue(c, true, null);
}
-
Funny, I'd think that it's even more important to double-buffer when remoting, so that you avoid needlessly sending a bunch of repaints over the wire? – Robert Jeppesen Jun 11 '09 at 14:50
-
14It's exactly what you don't want. In a terminal session the GDI system can send commands (draw line here, draw circle here, fill here, etc). Double buffering is accomplished by you drawing everyting onto a bitmap and then using GDI to paint your entire form as a bitmap. Sending an uncompressed bitmap over the wire is MUCH slower than sending the origianl GDI commands. – Ian Boyd Jun 12 '09 at 18:50
-
Doesn't help prevent an auto-sized TextBox from flickering on resize... In fact nothing I have tried so far does. – Roman Starkov Aug 26 '09 at 8:57
-
3@Boris That's because the Windows
TEXTBOX
control doesn't obey any laws of painting. – Ian Boyd Nov 30 '12 at 18:51 -
2@romkyns, If you need a double-buffered
TextBox
, use aRichTextBox
withDetectUrls
set to False. If you want it to be editable, strip the formatting using the EM_SETCHARFORMAT and EM_SETPARAFORMAT messages (example code is here). – alldayremix Jun 27 '13 at 22:16
Check this thread
Repeating the core of that answer, you can turn on the WS_EX_COMPOSITED style flag on the window to get both the form and all of its controls double-buffered. The style flag is available since XP. It doesn't make painting faster but the entire window is drawn in an off-screen buffer and blitted to the screen in one whack. Making it look instant to the user's eyes without visible painting artifacts. It is not entirely trouble-free, some visual styles renderers can glitch on it, particularly TabControl when its has too many tabs. YMMV.
Paste this code into your form class:
protected override CreateParams CreateParams {
get {
var cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
The big difference between this technique and Winform's double-buffering support is that Winform's version only works on one control at at time. You will still see each individual control paint itself. Which can look like a flicker effect as well, particularly if the unpainted control rectangle contrasts badly with the window's background.
System.Reflection.PropertyInfo aProp = typeof(System.Windows.Forms.Control)
.GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
aProp.SetValue(ListView1, true, null);
Ian has some more information about using this on a terminal server.
-
It's not really for on a terminal server, since none of my customers run the software from a remote desktop session. But you should pay your developer taxes, and i didn't want to put out code that didn't already include the taxes. – Ian Boyd Sep 19 '08 at 19:38
public void EnableDoubleBuffering()
{
this.SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint,
true);
this.UpdateStyles();
}
-
nice one....! But i wonder why it still flickering when i tried to make visible of a form become false? – gumuruh Jun 25 '20 at 23:46
One way is to extend the specific control you want to double buffer and set the DoubleBuffered property inside the control's ctor.
For instance:
class Foo : Panel
{
public Foo() { DoubleBuffered = true; }
}
-
I'm working with an owner drawn ListView in a derived class. This solved the problem perfectly! – Brad Bruce Feb 27 '10 at 12:00
-
I'm don't know if there are any downsides, but this worked perfectly. My custom control previously inherited from "Control", and I just changed it to inherit from "Panel" instead, and set DoubleBuffered to true. A two line change. Motion is smooth... and the speed to draw a single update seems much faster as well. – Mark T Jul 7 '17 at 5:57
Extension method to turn double buffering on or off for controls
public static class ControlExtentions
{
/// <summary>
/// Turn on or off control double buffering (Dirty hack!)
/// </summary>
/// <param name="control">Control to operate</param>
/// <param name="setting">true to turn on double buffering</param>
public static void MakeDoubleBuffered(this Control control, bool setting)
{
Type controlType = control.GetType();
PropertyInfo pi = controlType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(control, setting, null);
}
}
Usage (for example how to make DataGridView DoubleBuffered):
DataGridView _grid = new DataGridView();
// ...
_grid.MakeDoubleBuffered(true);
nobugz gets the credit for the method in his link, I'm just reposting. Add this override to the Form:
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}
This worked best for me, on Windows 7 I was getting large black blocks appearing when I resize a control heavy form. The control now bounce instead! But it's better.
-
This is the same as @Hans Passant's answer, but +1 for putting the code here. – Shaul Behr Feb 24 '11 at 17:39
Before you try double buffering, see if SuspendLayout()/ResumeLayout() solve your problem.
-
3Suspend/ResumeLayout doesn't solve the problem of flicker when painting. – Ian Boyd Oct 29 '09 at 17:20
This caused me a lot of grief for two days with a third party control until I tracked it down.
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}
I recently had a lot of holes (droppings) when re-sizing / redrawing a control containing several other controls.
I tried WS_EX_COMPOSITED and WM_SETREDRAW but nothing worked until I used this:
private void myPanel_SizeChanged(object sender, EventArgs e)
{
Application.DoEvents();
}
Just wanted to pass it on.
vb.net version of this fine solution....:
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or &H2000000
Return cp
End Get
End Property
-
is this applicable for double buffering all element controls in that frame only? – gumuruh Jun 25 '20 at 23:41
You can also inherit the controls into your own classes, and set the property in there. This method is also nice if you tend to be doing a lot of set up that is the same on all of the controls.
I have found that simply setting the DoubleBuffered setting on the form automatically sets all the properties listed here.
-
1yeah I know... I've lost that code and haven't been able to find it anywhere else on t'internet... sorry! – ljs Jan 3 '10 at 16:54