ListViewのダブルバッファ
.NetのListViewを使っていてまず気づくのが、項目の追加時やスクロール時に妙にちらつくこと。どうやらデフォルトでダブルバッファがOffになっているらしい。
DoubleBufferdプロパティをtrueに設定すれば良い・・・ということは予想できるが、このプロパティはprotectedになっている。何故publicじゃないんだろう?
解決策は以下のコード。SetListViewDoubleBuffered()にリストビューオブジェクトを渡せばOK。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace Siki { [ComVisibleAttribute(false)] public class Styles { private const int LVM_FIRST = 0x1000; private const int LVM_SETEXTENDEDLISTVIEWSTYLE = (LVM_FIRST + 54); private const int LVM_GETEXTENDEDLISTVIEWSTYLE = (LVM_FIRST + 55); private const int LVS_EX_DOUBLEBUFFER = 0x00010000; [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam); public static void SetListViewDoubleBuffered(ListView listview) { int styles = (int)SendMessage(listview.Handle, (int)LVM_GETEXTENDEDLISTVIEWSTYLE, 0, (IntPtr)0); styles |= LVS_EX_DOUBLEBUFFER; SendMessage(listview.Handle, (int)LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (IntPtr)styles); } } } |
SendMessage()でウィンドウスタイルを直接変更してます。
64bitアプリケーションでも問題ないようです。

