• 使用OwnerDraw改变ListView的ColumnHeader


    当我使用了OwnerDraw改变ListView的ColumnHeader的颜色之后,发现行无法显示内容了.其原因就是OwnerDraw属性设置为True造成了,也就是说,这个属性需要我们重画ListView中的各种内容,我们先是使用DrawColumnHeader消息重画了ColumnHeader,使其改变了颜色,但没有重画Item和SubItem,也就是行,因此行中的数据无法显示了,所以我们还要使用DrawItem和DrawSubItem两个消息生成函数.

    listViewNoFlickerWei.OwnerDraw = true;
    listViewNoFlickerWei.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(listViewNoFlickerWei_DrawColumnHeader);
    listViewNoFlickerWei.DrawItem += new DrawListViewItemEventHandler(listViewNoFlickerWei_DrawItem);
    listViewNoFlickerWei.DrawSubItem += new DrawListViewSubItemEventHandler(listViewNoFlickerWei_DrawSubItem);

    void listViewNoFlickerWei_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
            {
                using (StringFormat sf = new StringFormat())
                {
                    // Store the column text alignment, letting it default
                    // to Left if it has not been set to Center or Right.
                    switch (e.Header.TextAlign)
                    {
                        case HorizontalAlignment.Center:
                            sf.Alignment = StringAlignment.Center;
                            break;
                        case HorizontalAlignment.Right:
                            sf.Alignment = StringAlignment.Far;
                            break;
                    }
    
                    // Draw the standard header background.
                    e.DrawBackground();
    
                    // Draw the header text.
                    using (Font headerFont =
                                new Font("Helvetica", 10, FontStyle.Bold))
                    {
                        e.Graphics.DrawString(e.Header.Text, headerFont,
                            Brushes.Black, e.Bounds, sf);
                    }
                }
                return;
            }
            void listViewNoFlickerWei_DrawItem(object sender, DrawListViewItemEventArgs e)
            {
                if ((e.State & ListViewItemStates.Selected) != 0)
                {
                    // Draw the background and focus rectangle for a selected item.
                    e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds);
                    e.DrawFocusRectangle();
                }
                else
                {
                    Point tPoint = new Point();
                    SolidBrush tFrontBrush = new SolidBrush(Color.Black);
                    Font tFont = new Font("宋体", 9, FontStyle.Regular);
                    tPoint.X = e.Bounds.X + 3;
                    tPoint.Y = e.Bounds.Y + 2;
                    e.Graphics.DrawString(e.Item.Text, tFont, tFrontBrush, tPoint);
                }
            }
            void listViewNoFlickerWei_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
            {
                Point tPoint = new Point();
                SolidBrush tFrontBrush = new SolidBrush(Color.White);
                Font tFont = new Font("宋体", 9, FontStyle.Regular);
                tPoint.X = e.Bounds.X + 3;
                tPoint.Y = e.Bounds.Y + 2;
                e.Graphics.DrawString(e.SubItem.Text, tFont, tFrontBrush, tPoint);
            }
  • 相关阅读:
    HashTable源码浅析(基于jdk1.8.0_231)
    LinkedHashMap源码浅析(基于jdk1.8.0_231)
    SortedSet接口源码浅析(基于jdk1.8.0_231)
    NavigableSet接口源码浅析(基于jdk1.8.0_231)
    TreeSet源码浅析(基于jdk1.8.0_231)
    TreeMap源码浅析(基于jdk1.8.0_231)
    Map接口源码解析(基于jdk1.8.0_231)
    Arrays工具类源码详解(基于jdk1.8.0_231)
    Collections源码详解(基于jdk1.8.0_231)
    BitSet源码详解 (基于jdk1.8.0.231)
  • 原文地址:https://www.cnblogs.com/huangzhen22/p/5327811.html
Copyright © 2020-2023  润新知