How to prevent flickering in ListView when updating a single ListViewItem's text?

前端 未结 10 2205
囚心锁ツ
囚心锁ツ 2020-11-27 04:56

All I want is to update an ListViewItem\'s text whithout seeing any flickering.

This is my code for updating (called several times):

listView.BeginUp         


        
10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 05:37

    To end this question, here is a helper class that should be called when the form is loading for each ListView or any other ListView's derived control in your form. Thanks to "Brian Gillespie" for giving the solution.

    public enum ListViewExtendedStyles
    {
        /// 
        /// LVS_EX_GRIDLINES
        /// 
        GridLines = 0x00000001,
        /// 
        /// LVS_EX_SUBITEMIMAGES
        /// 
        SubItemImages = 0x00000002,
        /// 
        /// LVS_EX_CHECKBOXES
        /// 
        CheckBoxes = 0x00000004,
        /// 
        /// LVS_EX_TRACKSELECT
        /// 
        TrackSelect = 0x00000008,
        /// 
        /// LVS_EX_HEADERDRAGDROP
        /// 
        HeaderDragDrop = 0x00000010,
        /// 
        /// LVS_EX_FULLROWSELECT
        /// 
        FullRowSelect = 0x00000020,
        /// 
        /// LVS_EX_ONECLICKACTIVATE
        /// 
        OneClickActivate = 0x00000040,
        /// 
        /// LVS_EX_TWOCLICKACTIVATE
        /// 
        TwoClickActivate = 0x00000080,
        /// 
        /// LVS_EX_FLATSB
        /// 
        FlatsB = 0x00000100,
        /// 
        /// LVS_EX_REGIONAL
        /// 
        Regional = 0x00000200,
        /// 
        /// LVS_EX_INFOTIP
        /// 
        InfoTip = 0x00000400,
        /// 
        /// LVS_EX_UNDERLINEHOT
        /// 
        UnderlineHot = 0x00000800,
        /// 
        /// LVS_EX_UNDERLINECOLD
        /// 
        UnderlineCold = 0x00001000,
        /// 
        /// LVS_EX_MULTIWORKAREAS
        /// 
        MultilWorkAreas = 0x00002000,
        /// 
        /// LVS_EX_LABELTIP
        /// 
        LabelTip = 0x00004000,
        /// 
        /// LVS_EX_BORDERSELECT
        /// 
        BorderSelect = 0x00008000,
        /// 
        /// LVS_EX_DOUBLEBUFFER
        /// 
        DoubleBuffer = 0x00010000,
        /// 
        /// LVS_EX_HIDELABELS
        /// 
        HideLabels = 0x00020000,
        /// 
        /// LVS_EX_SINGLEROW
        /// 
        SingleRow = 0x00040000,
        /// 
        /// LVS_EX_SNAPTOGRID
        /// 
        SnapToGrid = 0x00080000,
        /// 
        /// LVS_EX_SIMPLESELECT
        /// 
        SimpleSelect = 0x00100000
    }
    
    public enum ListViewMessages
    {
        First = 0x1000,
        SetExtendedStyle = (First + 54),
        GetExtendedStyle = (First + 55),
    }
    
    /// 
    /// Contains helper methods to change extended styles on ListView, including enabling double buffering.
    /// Based on Giovanni Montrone's article on 
    /// 
    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);
        }
    }
    

提交回复
热议问题