zoukankan      html  css  js  c++  java
  • 使用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);
            }
  • 相关阅读:
    回顾python,就当做笔记了
    测试知识回顾
    转发 Python接口自动化
    性能测试脚本调优
    java
    新的一年,希望自己有所提升,在这简单的记录,自己的学习。
    navicat 连接 mysql 出现Client does not support authentication protocol requested by server解决方案
    tomcat context配置
    tomcat host 配置
    flyway使用
  • 原文地址:https://www.cnblogs.com/huangzhen22/p/5327811.html
Copyright © 2011-2022 走看看