2

我正在制作一个所有者绘制的列表视图,其中我在列表视图中绘制形状。我已经使用Listview_DrawItem事件做到了这一点。我的问题是,当我运行应用程序时,我无法选择在列表视图中绘制的形状。

private void AddItem(ListView lvw, string Shape_name, Color Shape_color)
    {
        // Make the item.
        ListViewItem item = new ListViewItem(Shape_name);

        // Save the Shape object  in the Tag property.
        Shapes myShape = new Shapes(Shape_name,Shape_color);

        item.Tag = myShape;
        item.SubItems[0].Name ="ShapeName";

        // Add subitems so they can draw.
        item.SubItems.Add("ShapeColor");

        // Add the item to the ListView.
        lvw.Items.Add(item);
    }


    // Draw the item. In this case, the Shape_name's logo.
    private void lvwServers_DrawItem(object sender, DrawListViewItemEventArgs e)
    {

        // Get the ListView item and the Shapes object.
        ListViewItem item = e.Item;

        Shapes myShape = item.Tag as Shapes;

        // Clear.
        e.DrawBackground();

        // Smoothing mode for blur free drawing
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

        Rectangle rect = new Rectangle(e.Bounds.Left + 10, e.Bounds.Top + 10, 41, 41);


        using (SolidBrush br = new SolidBrush(myShape.ShapeColor))
        {

            e.Graphics.FillRectangle(br, rect);
        }

        e.Graphics.DrawRectangle(Pens.Black, rect);

        e.Graphics.ResetTransform();
        e.DrawFocusRectangle();

另外,我无法更改e.bound属性值。

4

1 回答 1

2

您可以将 DrawItem 事件处理程序中的绘图代码基于 DrawListViewItemEventArgs 参数中可用的 e.State 的值。

bool isSelected = e.State == ListViewItemStates.Selected;

这样,您可以决定在选择项目时要更改已绘制项目的哪些元素。

于 2012-09-12T05:40:50.470 回答