C# 如何防止在更新单个ListViewItem时在ListView中闪烁';s文本?

C# 如何防止在更新单个ListViewItem时在ListView中闪烁';s文本?,c#,.net,winforms,listview,C#,.net,Winforms,Listview,我只想更新ListViewItem的文本,而不显示任何闪烁 这是我的更新代码(多次调用): 我见过一些涉及覆盖组件的WndProc(): 他们说它解决了问题,但就我而言,它没有解决问题。我相信这是因为我在每个项目上都使用图标。如果您只想更新文本,只需直接设置更改的子项目的文本,而不是更新整个ListViewItem(您没有说明如何进行更新) 您显示的覆盖相当于简单地覆盖OnPaintBackground,这将是执行该任务的“更正确”的托管方式,对单个项目没有帮助 如果您仍然有问题,我们需要澄清您

我只想更新ListViewItem的文本,而不显示任何闪烁

这是我的更新代码(多次调用):

我见过一些涉及覆盖组件的
WndProc():


他们说它解决了问题,但就我而言,它没有解决问题。我相信这是因为我在每个项目上都使用图标。

如果您只想更新文本,只需直接设置更改的子项目的文本,而不是更新整个ListViewItem(您没有说明如何进行更新)

您显示的覆盖相当于简单地覆盖OnPaintBackground,这将是执行该任务的“更正确”的托管方式,对单个项目没有帮助


如果您仍然有问题,我们需要澄清您实际尝试了什么。

这是一个不确定的结果,但您可以尝试对控件进行双缓冲

SetStyle(
  ControlStyles.AllPaintingInWmPaint |
  ControlStyles.UserPaint |
  ControlStyles.DoubleBuffer, true)

CommonControls 6(XP或更新版本)中的ListView支持双缓冲。幸运的是,.NET在系统上封装了最新的通用控件。要启用双缓冲,请向ListView控件发送适当的Windows消息

详情如下:

为了结束这个问题,在为表单中的每个ListView或任何其他ListView派生控件加载表单时,应该调用一个帮助器类。感谢“Brian Gillespie”给出了解决方案

public enum ListViewExtendedStyles
{
    /// <summary>
    /// LVS_EX_GRIDLINES
    /// </summary>
    GridLines = 0x00000001,
    /// <summary>
    /// LVS_EX_SUBITEMIMAGES
    /// </summary>
    SubItemImages = 0x00000002,
    /// <summary>
    /// LVS_EX_CHECKBOXES
    /// </summary>
    CheckBoxes = 0x00000004,
    /// <summary>
    /// LVS_EX_TRACKSELECT
    /// </summary>
    TrackSelect = 0x00000008,
    /// <summary>
    /// LVS_EX_HEADERDRAGDROP
    /// </summary>
    HeaderDragDrop = 0x00000010,
    /// <summary>
    /// LVS_EX_FULLROWSELECT
    /// </summary>
    FullRowSelect = 0x00000020,
    /// <summary>
    /// LVS_EX_ONECLICKACTIVATE
    /// </summary>
    OneClickActivate = 0x00000040,
    /// <summary>
    /// LVS_EX_TWOCLICKACTIVATE
    /// </summary>
    TwoClickActivate = 0x00000080,
    /// <summary>
    /// LVS_EX_FLATSB
    /// </summary>
    FlatsB = 0x00000100,
    /// <summary>
    /// LVS_EX_REGIONAL
    /// </summary>
    Regional = 0x00000200,
    /// <summary>
    /// LVS_EX_INFOTIP
    /// </summary>
    InfoTip = 0x00000400,
    /// <summary>
    /// LVS_EX_UNDERLINEHOT
    /// </summary>
    UnderlineHot = 0x00000800,
    /// <summary>
    /// LVS_EX_UNDERLINECOLD
    /// </summary>
    UnderlineCold = 0x00001000,
    /// <summary>
    /// LVS_EX_MULTIWORKAREAS
    /// </summary>
    MultilWorkAreas = 0x00002000,
    /// <summary>
    /// LVS_EX_LABELTIP
    /// </summary>
    LabelTip = 0x00004000,
    /// <summary>
    /// LVS_EX_BORDERSELECT
    /// </summary>
    BorderSelect = 0x00008000,
    /// <summary>
    /// LVS_EX_DOUBLEBUFFER
    /// </summary>
    DoubleBuffer = 0x00010000,
    /// <summary>
    /// LVS_EX_HIDELABELS
    /// </summary>
    HideLabels = 0x00020000,
    /// <summary>
    /// LVS_EX_SINGLEROW
    /// </summary>
    SingleRow = 0x00040000,
    /// <summary>
    /// LVS_EX_SNAPTOGRID
    /// </summary>
    SnapToGrid = 0x00080000,
    /// <summary>
    /// LVS_EX_SIMPLESELECT
    /// </summary>
    SimpleSelect = 0x00100000
}

public enum ListViewMessages
{
    First = 0x1000,
    SetExtendedStyle = (First + 54),
    GetExtendedStyle = (First + 55),
}

/// <summary>
/// Contains helper methods to change extended styles on ListView, including enabling double buffering.
/// Based on Giovanni Montrone's article on <see cref="http://www.codeproject.com/KB/list/listviewxp.aspx"/>
/// </summary>
public class ListViewHelper
{
    private ListViewHelper()
    {
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int SendMessage(IntPtr handle, int messg, int wparam, int lparam);

    public static void SetExtendedStyle(Control control, ListViewExtendedStyles exStyle)
    {
        ListViewExtendedStyles styles;
        styles = (ListViewExtendedStyles)SendMessage(control.Handle, (int)ListViewMessages.GetExtendedStyle, 0, 0);
        styles |= exStyle;
        SendMessage(control.Handle, (int)ListViewMessages.SetExtendedStyle, 0, (int)styles);
    }

    public static void EnableDoubleBuffer(Control control)
    {
        ListViewExtendedStyles styles;
        // read current style
        styles = (ListViewExtendedStyles)SendMessage(control.Handle, (int)ListViewMessages.GetExtendedStyle, 0, 0);
        // enable double buffer and border select
        styles |= ListViewExtendedStyles.DoubleBuffer | ListViewExtendedStyles.BorderSelect;
        // write new style
        SendMessage(control.Handle, (int)ListViewMessages.SetExtendedStyle, 0, (int)styles);
    }
    public static void DisableDoubleBuffer(Control control)
    {
        ListViewExtendedStyles styles;
        // read current style
        styles = (ListViewExtendedStyles)SendMessage(control.Handle, (int)ListViewMessages.GetExtendedStyle, 0, 0);
        // disable double buffer and border select
        styles -= styles & ListViewExtendedStyles.DoubleBuffer;
        styles -= styles & ListViewExtendedStyles.BorderSelect;
        // write new style
        SendMessage(control.Handle, (int)ListViewMessages.SetExtendedStyle, 0, (int)styles);
    }
}
公共枚举ListViewExtendedStyles { /// ///LVS_EX_网格线 /// 网格线=0x00000001, /// ///LVS_EX_子项图像 /// 子项图像=0x00000002, /// ///LVS_EX_复选框 /// 复选框=0x00000004, /// ///LVS_EX_轨迹选择 /// TrackSelect=0x00000008, /// ///LVS_EX_HEADERDRAGDROP /// HeaderDragDrop=0x00000010, /// ///LVS_EX_FULLROWSELECT /// FullRowSelect=0x00000020, /// ///LVS_EX_One单击激活 /// OneClickActivate=0x00000040, /// ///LVS_EX_Two单击激活 /// 两次单击激活=0x00000080, /// ///LVS_EX_FLATSB /// FlatsB=0x00000100, /// ///LVS_前_地区 /// 区域=0x00000200, /// ///LVS_EX_信息提示 /// InfoTip=0x00000400, /// ///LVS_EX_UNDERLINEHOT /// 下划线HOT=0x00000800, /// ///LVS_EX_UNDERLINECOLD /// 下划线COLD=0x00001000, /// ///LVS_EX_多工作区 /// 多工作区=0x000020000, /// ///LVS_EX_LABELTIP /// LabelTip=0x00004000, /// ///LVS_EX_BORDERSELECT /// BorderSelect=0x00008000, /// ///LVS_EX_双缓冲区 /// DoubleBuffer=0x00010000, /// ///LVS_EX_HIDELABELS /// HideLabels=0x00020000, /// ///LVS_EX_SINGLEROW /// 单行=0x00040000, /// ///LVS_EX_SNAPTOGRID /// SnapToGrid=0x00080000, /// ///LVS_EX_SIMPLESELECT /// SimpleSelect=0x00100000 } 公共枚举ListViewMessages { 第一个=0x1000, SetExtendedStyle=(第一个+54), GetExtendedStyle=(第一个+55), } /// ///包含用于更改ListView上扩展样式的帮助器方法,包括启用双缓冲。 ///根据Giovanni Montrone关于 /// 公共类ListViewHelper { 私有ListViewHelper() { } [DllImport(“user32.dll”,CharSet=CharSet.Auto)] 私有静态外部int SendMessage(IntPtr句柄、int messg、int wparam、int lparam); 公共静态void SetExtendedStyle(控件控件,ListViewExtendedStyles exStyle) { ListViewExtendedStyles; styles=(ListViewExtendedStyles)SendMessage(control.Handle,(int)ListViewMessages.GetExtendedStyle,0,0); 样式|=exStyle; SendMessage(control.Handle,(int)ListViewMessages.SetExtendedStyle,0,(int)styles); } 公共静态无效启用DoubleBuffer(控制) { ListViewExtendedStyles; //阅读当前样式 styles=(ListViewExtendedStyles)SendMessage(control.Handle,(int)ListViewMessages.GetExtendedStyle,0,0); //启用双缓冲区和边框选择 styles |=ListViewExtendedStyles.DoubleBuffer | ListViewExtendedStyles.BorderSelect; //写新文体 SendMessage(control.Handle,(int)ListViewMessages.SetExtendedStyle,0,(int)styles); } 公共静态无效禁用DoubleBuffer(控制) { ListViewExtendedStyles; //阅读当前样式 styles=(ListViewExtendedStyles)SendMessage(control.Handle,(int)ListViewMessages.GetExtendedStyle,0,0); //禁用双缓冲区和边框选择 样式-=样式和ListViewExtendedStyles.DoubleBuffer; 样式-=样式和列表视图ExtendedStyles.BorderSelect; //写新文体 SendMessage(control.Handle,(int)ListViewMessages.SetExtendedStyle,0,(int)styles); } }
在.NET Winforms 2.0中,存在一个名为DoubleBuffered的受保护属性

通过从ListView继承,可以将此受保护属性设置为true。这将启用双缓冲,而无需调用SendMessage

设置DoubleBuffered属性与设置以下样式相同:

listview.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
这将有助于:

class DoubleBufferedListView : System.Windows.Forms.ListView
{
    public DoubleBufferedListView()
        :base()
    {
        this.DoubleBuffered = true;
    }
}
在设置任何列表视图项之前调用ListView上的BeginUpdate()方法,然后仅在添加所有项之后调用EndUpdate()


这将停止闪烁。

接受的答案是可行的,但相当长,并且从控件(如其他答案中所述)中派生出来的只是启用双缓冲也有点过头了。但幸运的是,我们有反射,如果愿意的话,还可以调用内部方法(但请确保您所做的!)

将此方法封装到扩展me中
listview.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
class DoubleBufferedListView : System.Windows.Forms.ListView
{
    public DoubleBufferedListView()
        :base()
    {
        this.DoubleBuffered = true;
    }
}
public static class ControlExtensions
{
    public static void DoubleBuffering(this Control control, bool enable)
    {
        var method = typeof(Control).GetMethod("SetStyle", BindingFlags.Instance | BindingFlags.NonPublic);
        method.Invoke(control, new object[] { ControlStyles.OptimizedDoubleBuffer, enable });
    }
}
InitializeComponent();

myListView.DoubleBuffering(true); //after the InitializeComponent();
public static void DoubleBuffered(this Control control, bool enable)
{
    var doubleBufferPropertyInfo = control.GetType().GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
    doubleBufferPropertyInfo.SetValue(control, enable, null);
}