Getting listview doublebuffered and exporting OnPaint method problem


Jon Abaunza

I wanted to make the listview double buffered because it would be a
listview inside a panel that would have more controls (The panel would
have scroll). As the scroll is controled by the outside panel, it
invalidates the whole listview when scrolling and causes flickering.
In order to avoid it i tried to make the listview doublebuffered by
subclassing it and saving the generated image in a offscreen image. The
code works ok but i cannot avoid that it tries to get repainted all the
time. How could i avoid it? And apart from that the next times that it
tries to (m.WParam = grBuffer.GetHdc();) it gets always an
ArgumentException event if in the first loop everything worked alright
and even screened correctly the data in the device.

public event PaintEventHandler Paint;

//When it's assigned the subclass I create the graphics objects so that
it's not so slow

Bitmap offScreenImage = new Bitmap(control.Width, control.Height);
Graphics grBuffer = Graphics.FromImage(offScreenImage);
Graphics gr = Graphics.FromHdc(NativeMethods.GetDC(Handle));

//WndProc of the subclass of the listview(control)
protected override void WndProc(ref Message m)
case WM_PAINT:

m.WParam = grBuffer.GetHdc();

base.DefWndProc(ref m);
if (Paint != null)
{ //I will buid up the PaintEventHandler structure
PaintEventArgs pArgs = new PaintEventArgs(grBuffer, new
Rectangle(0, 0,offScreenImage.Width, offScreenImage.Height));
//I launch the event paint letting paint in the offscreen
Paint(control, pArgs);
//I paint the offscreenImage to screen
gr.DrawImage(offScreenImage, control.Left,control.Top);

//I try to validate the listview so that it does not call
painting for ever but i don't get it
RECT rect = null;
NativeMethods.RedrawWindow(Handle,ref rect, IntPtr.Zero,


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question