【发布时间】:2014-07-29 14:38:26
【问题描述】:
我正在尝试为 C# 中的 ListView 提供自定义边框颜色。我对 ListView 进行了子类化并重写了 WndProc 方法来处理 WM_NCPAINT 消息以提供自定义边框颜色。
public class SampleListView : ListView
{
public SampleListView ()
{
InitializeComponent();
View = Details.Details;
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; /*WS_EX_COMPOSITED*/
return cp;
}
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case: WM_NCPAINT
base.WndProc(ref m);
IntPtr hDc=Win32.GetWindowDC(m.Hwnd);
Graphics g = Graphics.FromHwnd(hDc);
RECT windowRect= new RECT();
Win32.GetWindoRect(m.Hwnd, ref windowRect);
ControlPaint.DrawBorder(g, windowRect, Color.YellowGreen, ButtonBorderStyle.Solid);
g.Dispose();
break;
default:
base.WndProc(ref m);
break;
}
}
}
将 SampleListView 添加到表单并启动应用程序。 SPY++ 在 SampleListView 中显示了一个无限的 WM_PAINT 循环。这是bug??? 我正在使用 WS_EX_COMPOSITED 样式来避免闪烁。如何避免这种无限的 WM_PAINT 循环?
【问题讨论】:
-
该样式标志仅适用于顶级窗口。避免 ListView 闪烁已经很简单了,只需将其 DoubleBuffered 属性设置为 true。