如何在 betterlistview 上使用这个覆盖?

How to use this overriding on betterlistview?

我一直在使用 BetterListView,我问开发人员是否可以更改选择的颜色。这是他们的回复:

you can customize selection using Owner Drawing. This requires subclassing BetterListView and drawing custom selection instead of the default one:

public class CustomListView : BetterListView
{
    protected override void OnDrawItem(BetterListViewDrawItemEventArgs eventArgs)
    {
        if ((eventArgs.ItemStateInfo.ItemState & BetterListViewItemState.Selected) == BetterListViewItemState.Selected)
        {
            // draw custom selection
            eventArgs.Graphics.FillRectangle(Brushes.Brown, eventArgs.ItemBounds.BoundsSelection);
        }

        eventArgs.DrawSelection = false; // disable drawing default selection

        // default drawing (image, text etc.)
        base.OnDrawItem(eventArgs);
    }
}

Please note the eventArgs.ItemStateInfo.ItemState is a flags enum, i.e. the item can be hot, focused and selected independently and drawing of the custom selection may need to include these states for better user experience. The default selection uses Windows Theme so it is displayed the same way as in Windows Explorer.

阅读本文后,我试图了解如何使用此覆盖,但我从未做过这样的事情。尽管阅读了很多有关 OwnerDrawing 和 Overriding 的内容,但我仍然感到困惑。

这是翻译成 VB.Net 的代码,并添加了另外两种选择类型:

Imports ComponentOwl.BetterListView

Public Class CustomBetterListview

    Inherits BetterListView
    Protected Overrides Sub OnDrawItem(eventArgs As BetterListViewDrawItemEventArgs)

        If (eventArgs.ItemStateInfo.ItemState And BetterListViewItemState.Selected) = BetterListViewItemState.Selected Then
            ' draw custom selection
            eventArgs.Graphics.FillRectangle(Brushes.Brown, eventArgs.ItemBounds.BoundsSelection)
        End If

        If (eventArgs.ItemStateInfo.ItemState And BetterListViewItemState.Focused) = BetterListViewItemState.Focused Then
            ' draw custom selection
            eventArgs.Graphics.FillRectangle(Brushes.Red, eventArgs.ItemBounds.BoundsSelection)
        End If

        If (eventArgs.ItemStateInfo.ItemState And BetterListViewItemState.Hot) = BetterListViewItemState.Hot Then
            ' draw custom selection
            eventArgs.Graphics.FillRectangle(Brushes.Blue, eventArgs.ItemBounds.BoundsSelection)
        End If

        eventArgs.DrawSelection = False
        ' disable drawing default selection
        ' default drawing (image, text etc.)
        MyBase.OnDrawItem(eventArgs)
    End Sub

End Class

我想我的方法是正确的,但我想不通……似乎我缺少一些基本知识。我可能应该提到我正在尝试覆盖在设计模式下放置在表单上的 BetterListViews,因为我怀疑这可能是个问题?

我搜索了一些有关添加自定义控件的信息,似乎重建项目会将控件置于工具箱的顶部。现在一切正常。

感谢您的帮助。