我想要的只是更新ListViewItem的文本,而不会看到任何闪烁.
这是我的更新代码(多次调用):
listView.BeginUpdate();
listViewItem.SubItems[0].Text = state.ToString(); // update the state
listViewItem.SubItems[1].Text = progress.ToString(); // update the progress
listView.EndUpdate();
Run Code Online (Sandbox Code Playgroud)
我见过一些涉及覆盖组件的解决方案 WndProc():
protected override void WndProc(ref Message m)
{
if (m.Msg == (int)WM.WM_ERASEBKGND)
{
m.Msg = (int)IntPtr.Zero;
}
base.WndProc(ref m);
}
Run Code Online (Sandbox Code Playgroud)
他们说这解决了问题,但在我的情况下却没有.我相信这是因为我在每个项目上使用图标.
我正在使用listview控件并设置以下参数:
this.listView1.BackColor = System.Drawing.Color.Gainsboro;
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2});
this.listView1.FullRowSelect = true;
this.listView1.HideSelection = false;
this.listView1.Location = new System.Drawing.Point(67, 192);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(438, 236);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.DrawColumnHeader += new System.Windows.Forms.DrawListViewColumnHeaderEventHandler(this.listView1_DrawColumnHeader);
this.listView1.RetrieveVirtualItem += new System.Windows.Forms.RetrieveVirtualItemEventHandler(this.listView1_RetrieveVirtualItem);
this.listView1.DrawSubItem += new System.Windows.Forms.DrawListViewSubItemEventHandler(this.listView1_DrawSubItem);
Run Code Online (Sandbox Code Playgroud)
提供两行随机文本.拥有者绘图很简单:
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
if (e.ColumnIndex == 0)
{
e.DrawBackground();
e.DrawText();
}
else
e.DrawDefault = true;
//Console.WriteLine("{0}\t\tBounds:{1}\tItem:{2}\tSubitem:{3}", (i++).ToString(), e.Bounds.ToString(), e.Item, e.SubItem);
}
Run Code Online (Sandbox Code Playgroud)
问题是:当我将鼠标悬停在listview的内容上时,我会看到第一列的闪烁.调试显示DrawSubItem在鼠标悬停时不断调用.
是bug吗?如何避免这种行为?