在ListView控件中闪烁(OwnerDraw,Virtual)

时间:2012-05-07 14:54:56

标签: c# winforms listview flicker ownerdrawn

此问题可能会被视为 Flickering in listview with ownerdraw and virtualmode 的后续行动。

我在ListView中有一个Virtual mode控件,我尝试执行自定义绘图。项目渲染通过以下方法覆盖完成:

protected override void OnDrawItem(DrawListViewItemEventArgs eventArgs) 

如引用的问题所述,自定义绘图会在鼠标悬停事件上引入闪烁。调试器告诉我这是由于过量自定义绘制事件而发生的。


现在 - 所引用问题的接受答案告诉我们:

  

这是.NET的ListView中的一个错误,你无法绕过它   双缓冲。

  • 那么,这些信息有多可靠?这真的是一个错误吗?或者我们可能只是试图切断部分消息并希望它不会改变可见行为吗?

  • 如果我拥有ListViewVirtual Mode,的所有者绘图例程,我可以取消这些Custom Draw事件并仅在{{1}中执行绘图或者,也许,这在某些情况下是不正确的?

  • WM_PAINT控件能够在System.Windows.Forms中完成所有绘画而不改变其初始行为的前提条件是什么?

3 个答案:

答案 0 :(得分:10)

至少对于OnDrawItem的双缓冲,有一个错误是错误的,但它有点愚蠢:你可以设置一个受保护的属性,但你需要覆盖ListView。我创建了这种类:

public class MyListView : ListView
{
    public MyListView()
        : base()
    {
        DoubleBuffered = true;
    }
}

然后在我的MyForm.Designer.cs文件中,我使用以下行更改ListView的实例化:

private ListView myListView;

this.myListView = new MyListView();

OnDrawItem就像魅力一样!

答案 1 :(得分:4)

This Answer in Here相似,但不确定,

我认为ListView.BeginUpdate()ListView.EndUpdate()会解决问题。

MSDN Thread about This

也许以这种方式:

protected override void OnDrawItem(DrawListViewItemEventArgs eventArgs)
{
    ListView.BeginUpdate();
    //Do Works Here
    ListView.EndUpdate();
}

更新

另一个替代方案可能是在BackgroundWorker中使用新线程来更新ListView ... 我在我的应用程序中将此与BeginUpdate() / EndUpDate()一起实现,发现它比仅BeginUpdate() / EndUpDate() ...

更快

更新

我找到了另一个有效的Solution at SO,一个由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);
    }
}

答案 2 :(得分:4)

我在任何自定义渲染事件处理程序(DrawItem,DrawSubItem)中都看到了ListView控件的这个闪烁问题。我尝试了BeginUpdate()/ EndUpdate()和双缓冲但没有成功。我认为.NET会向所有列触发额外的WM_PAINT到自定义绘制列的右侧

但是我发现这个解决方法是单个自定义渲染列ListView。它运作得很好!

  1. 在列标题编辑器中,将自定义绘制列设置为最后一列
  2. 更改所需位置的“DisplayIndex”
  3. 这可以解决鼠标悬停或运行时渲染中的闪烁问题。