使用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);
        }

 

转载于:https://www.cnblogs.com/huangzhen22/p/5327811.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值