Skip to content

Instantly share code, notes, and snippets.

@MatthewKing
Created November 16, 2012 06:22
Show Gist options
  • Save MatthewKing/4084773 to your computer and use it in GitHub Desktop.
Save MatthewKing/4084773 to your computer and use it in GitHub Desktop.
Provides methods to (properly) enable and disable double buffering on a ListView control.
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
/// <summary>
/// Provides methods to (properly) enable and disable double buffering on a ListView control.
/// Based on Giovanni Montrone's article on CodeProject at
/// <see cref="http://www.codeproject.com/KB/list/listviewxp.aspx"/>,
/// and on the StackOverflow answer at
/// <see cref="http://stackoverflow.com/a/162770/286936"/>.
/// </summary>
public static class ListViewHelper
{
/// <summary>
/// Sends the specified message to a window or windows.
/// </summary>
/// <param name="hWnd">
/// A handle to the window whose window procedure will receive the message.
/// </param>
/// <param name="msg">The message to be sent.</param>
/// <param name="wParam">Additional message-specific information.</param>
/// <param name="lParam">Additional message-specific information.</param>
/// <returns>The result of the message processing.</returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(
IntPtr hWnd,
UInt32 msg,
IntPtr wParam,
IntPtr lParam);
/// <summary>
/// The LVM_SETEXTENDEDLISTVIEWSTYLE message.
/// </summary>
private const int SetExtendedStyle = 0x1036;
/// <summary>
/// The LVM_GETEXTENDEDLISTVIEWSTYLE message.
/// </summary>
private const int GetExtendedStyle = 0x1037;
/// <summary>
/// The LVS_EX_BORDERSELECT extended style.
/// </summary>
private const int BorderSelect = 0x00008000;
/// <summary>
/// The LVS_EX_DOUBLEBUFFER extended style.
/// </summary>
private const int DoubleBuffer = 0x00010000;
/// <summary>
/// Enables double buffering on the specified ListView.
/// </summary>
/// <param name="listView">A ListView instance.</param>
public static void EnableDoubleBuffering(ListView listView)
{
if (listView == null)
throw new ArgumentNullException(
"listView",
"listView should not be null.");
IntPtr handle = listView.Handle;
// Read the current style.
int styles;
styles = SendMessage(handle, GetExtendedStyle, IntPtr.Zero, IntPtr.Zero).ToInt32();
// Enable double buffering and border select.
styles |= DoubleBuffer | BorderSelect;
// Write the new style.
SendMessage(handle, SetExtendedStyle, IntPtr.Zero, (IntPtr)styles);
}
/// <summary>
/// Disables double buffering on the specified ListView.
/// </summary>
/// <param name="listView">A ListView instance.</param>
public static void DisableDoubleBuffering(ListView listView)
{
if (listView == null)
throw new ArgumentNullException(
"listView",
"listView should not be null.");
IntPtr handle = listView.Handle;
// Read the current style.
int styles;
styles = SendMessage(handle, GetExtendedStyle, IntPtr.Zero, IntPtr.Zero).ToInt32();
// Disable double buffering and border select.
styles -= styles & DoubleBuffer;
styles -= styles & BorderSelect;
// Write the new style.
SendMessage(handle, SetExtendedStyle, IntPtr.Zero, (IntPtr)styles);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment