重写WinForm ListView控件上的Drawitem事件 [英] Overriding the Drawitem event on a WinForm ListView control

查看:1279
本文介绍了重写WinForm ListView控件上的Drawitem事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望失去焦点时,ListView的选定项保持清晰可见(在Windows 7上为暗灰色).我确实将HideSelection属性设置为False.

I'd like my ListView's selected item to remain clearly visible when focus is lost (it is a dim grey on Windows 7 ). I did set the HideSelection property to False.

我想为List View做某人为TreeView控件所做的事情,即,重写Drawedode事件:

I'd like to do for the List View what someone has done here for the TreeView control, namely, override the Drawnode event:

C#WinForms何时突出显示treenode Treeview没有焦点

我想我需要将OwnerDraw属性设置为True来覆盖DrawItem事件,但是我不确定在此事件中我需要做什么....:-)

I presume I need to set the OwnerDraw property to True override the DrawItem event, but I am not sure what I need to do in this event.... :-)

推荐答案

您需要类似的内容:

private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
    e.DrawDefault = true;
}

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    const int TEXT_OFFSET = 1;    // I don't know why the text is located at 1px to the right. Maybe it's only for me.

    ListView listView = (ListView)sender;

    // Check if e.Item is selected and the ListView has a focus.
    if (!listView.Focused && e.Item.Selected)
    {
        Rectangle rowBounds = e.SubItem.Bounds;
        Rectangle labelBounds = e.Item.GetBounds(ItemBoundsPortion.Label);
        int leftMargin = labelBounds.Left - TEXT_OFFSET;
        Rectangle bounds = new Rectangle(rowBounds.Left + leftMargin, rowBounds.Top, e.ColumnIndex == 0 ? labelBounds.Width : (rowBounds.Width - leftMargin - TEXT_OFFSET), rowBounds.Height);
        TextFormatFlags align;
        switch (listView.Columns[e.ColumnIndex].TextAlign)
        {
            case HorizontalAlignment.Right:
                align = TextFormatFlags.Right;
                break;
            case HorizontalAlignment.Center:
                align = TextFormatFlags.HorizontalCenter;
                break;
            default:
                align = TextFormatFlags.Left;
                break;
        }
        TextRenderer.DrawText(e.Graphics, e.SubItem.Text, listView.Font, bounds, SystemColors.HighlightText,
            align | TextFormatFlags.SingleLine | TextFormatFlags.GlyphOverhangPadding | TextFormatFlags.VerticalCenter | TextFormatFlags.WordEllipsis);
    }
    else
        e.DrawDefault = true;
}

private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
    ListView listView = (ListView)sender;

    // Check if e.Item is selected and the ListView has a focus.
    if (!listView.Focused && e.Item.Selected)
    {
        Rectangle rowBounds = e.Bounds;
        int leftMargin = e.Item.GetBounds(ItemBoundsPortion.Label).Left;
        Rectangle bounds = new Rectangle(leftMargin, rowBounds.Top, rowBounds.Width - leftMargin, rowBounds.Height);
        e.Graphics.FillRectangle(SystemBrushes.Highlight, bounds);
    }
    else
        e.DrawDefault = true;
}

编辑:对View = View.DetailsFullRowSelect = true进行了改进.
考虑了列的不同对齐方式,并添加了自动省略号标志.

Improved for View = View.Details and FullRowSelect = true.
Different alignment types of column are taken into account, and also auto-ellipsis flag was added.

这篇关于重写WinForm ListView控件上的Drawitem事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆