选定 ListView 项目周围的矩形 [英] Rectangle around selected ListView item

查看:28
本文介绍了选定 ListView 项目周围的矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 ListView 中围绕所选项目绘制一个矩形,因为在某处阅读 Microsoft 建议不要更改所述项目的突出显示颜色".但是,我正在使用 selectedIndexChanged 事件,当绘制实际的 listviewitem 时,我的矩形消失了.有根据的猜测会表明我的矩形是在它后面还是在重绘时被清除了?所以我的问题是,实际绘制矩形使其可见的最佳时间是什么时候?到目前为止,我的代码可以在下面看到:

I would like to draw a rectangle around the selected item in a ListView, due to reading somewhere that Microsoft recommends against changing the 'highlighted colour' of said item. However, I'm using the selectedIndexChanged event and when the actual listviewitem is drawn my rectangle disappears. An educated guess would suggest my rectangle is either behind it or has being cleared when it has being redrawn? So my question is, when would be the best time to actually draw the rectangle as so it is visible? my code so far can be seen below:

 void lvMain_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lvMain.SelectedItems.Count > 0)
        {
            if (lastSelectedItem == null) // First time called
            {
                lastSelectedItem = (sender as System.Windows.Forms.ListView).SelectedItems[0];
                DrawHighlightRectanlge(lastSelectedItem);
            }
            else
            {
                // TODO: Remove previous highlight
                lastSelectedItem = (sender as System.Windows.Forms.ListView).SelectedItems[0];
                DrawHighlightRectanlge(lastSelectedItem);
            }
        }
    }

    internal void DrawHighlightRectanlge(System.Windows.Forms.ListViewItem item)
    {
        using (Graphics g = item.ListView.CreateGraphics())
        {                
            g.DrawRectangle(new Pen(Color.Red), new Rectangle(item.Position.X, item.Position.Y, item.Bounds.Width, item.Bounds.Height));
        }
    }

TIA

推荐答案

这是所有者绘制的 ListView 的一个非常基本的版本.将 OwnerDraw 属性设置为 true 并对 DrawItem 事件进行编码,可能像这样:

Here is a very basic version for an owner-drawn ListView. Set the OwnerDraw property to true and code the DrawItem event, maybe like this:

private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
    e.DrawBackground();
    e.DrawText();

    if (e.Item.Selected)
    {
        Rectangle R = e.Bounds;  
        R.Inflate(-1, -1);
        using (Pen pen = new Pen(Color.Red, 1.5f))
        e.Graphics.DrawRectangle(pen, R);
    }
}

为了在Details View 中工作,我将矩形缩小了一点,但您应该尝试使其适合您的需求和幻想..!

I make the rectangle a little smaller for it to work in Details View, but you should play around to make it suit your needs and fancy..!

注意:如果您有 ColumnHeaders,您还需要以最简单的形式编写 DrawColumnHeader 事件,如下所示:

Note: If you have ColumnHeaders you also need to code the DrawColumnHeader event, in its simplest form like this:

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

如果你有子项,你需要有一个 DrawSubItem 事件,至少像这样:

And if you have SubItems you need to have a DrawSubItem event, again at least like this:

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    e.DrawDefault = true;
}

很明显,如果您还希望在此处绘制矩形,则需要为此事件编写更多代码.但是DrawBackgroundDrawText 的默认功能在这里也可用.

Obviously you need to write more code to this event, if you want your rectangle to be drawn here as well. But the default function of DrawBackground and DrawText are available here as well.

这篇关于选定 ListView 项目周围的矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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