首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用使用OnDrawItem视图的OwnerDrawn清单视图正确覆盖LargeIcon

无法使用使用OnDrawItem视图的OwnerDrawn清单视图正确覆盖LargeIcon
EN

Stack Overflow用户
提问于 2014-06-10 06:35:20
回答 1查看 874关注 0票数 1

我正在尝试用使用LargeIcon视图的自定义Listview控件来完成。我正在尝试自定义在OnDrawItem事件中绘制项。

到目前为止,我有以下代码:

代码语言:javascript
复制
Protected Overrides Sub OnDrawItem(e As DrawListViewItemEventArgs)
    Dim flags As TextFormatFlags
    Dim subColour As Color = Color.Black
    Dim subBackColour As Color = Color.Empty

    Try
        If Not (e.State And ListViewItemStates.Selected) = 0 Then
            'Draw the background for a selected item.
            e.Graphics.FillRectangle(System.Drawing.SystemBrushes.Highlight, e.Bounds)
            e.DrawFocusRectangle()
        Else
            'Draw the background for an unselected item.
            e.Graphics.FillRectangle(System.Drawing.SystemBrushes.Control, e.Bounds)
        End If

        e.DrawBackground()

        'Draw the Icons
        e.Graphics.SmoothingMode = SmoothingMode.HighQuality
        e.Item.ImageList.Draw(e.Graphics, New Point(20, 22), 0)
        e.Graphics.ResetTransform()
        e.DrawFocusRectangle()

        'Draw the Text
        flags = TextFormatFlags.HorizontalCenter Or TextFormatFlags.Bottom
        Dim rec As New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width + 10, e.Bounds.Height + 10)
        TextRenderer.DrawText(e.Graphics, e.Item.Text, Me.Font, rec, subColour, subBackColour, flags)

        MyBase.OnDrawItem(e)

    Catch ex As Exception
        ErrorTrap(ex, "ListView_Stores: OnDrawItem()")
    End Try
End Sub

然而,当我运行我的代码时,它正确地绘制了我的文本和图标,但我似乎无法按照下面的图片使突出显示的项正确:

它没有正确地用任何颜色高亮(只是一个虚线的正方形),它甚至没有突出显示对象的整个边界--它在文本的一半处突出显示。

想知道是否有人能帮我,或者至少给我指明正确的方向。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-11 08:18:28

好的,有了大量的研究、试验和错误的,通过在我的自定义类中重写ListItem的OnDraw事件,我成功地实现了我所追求的目标。我不确定这是否是正确的(或首选的)方法,但我对结果感到满意。

最后,我使用了ColorMatrix方法将“蓝色”高亮颜色覆盖到选定的项上。然后,当未选择时,我只是将我的ColorMatrix设置为空

我的新修订代码:

代码语言:javascript
复制
Protected Overrides Sub OnDrawItem(e As DrawListViewItemEventArgs)
    Dim storeName_flags As New StringFormat
    Dim storeCode_flags As New StringFormat
    Dim matrixItems As Single()() = { _
           New Single() {0, 0, 0, 0, 0}, _
            New Single() {0, 0.6F, 0, 0, 0}, _
             New Single() {0, 0, 3, 0, 0}, _
              New Single() {0, 0, 0, 1, 0}, _
               New Single() {0, 0, 0, 0, 1}}
    Dim colorMatrix As ColorMatrix = New ColorMatrix(matrixItems)
    Dim imgattr As ImageAttributes = New ImageAttributes
    Dim bmp As Bitmap = New Bitmap(My.Resources.Store_Good)

    Try
        'Get StoreName and StoreNum from original e.Item.Text
        Dim StoreDetail As String() = e.Item.Text.Split(New Char() {"|"c})
        Dim StoreName As String = StoreDetail(0)
        Dim StoreNum As String = StoreDetail(1)

        'Declare Image Rectangle as the max size of the bitmap
        Dim Image_Width As Integer = bmp.Width
        Dim Image_Height As Integer = bmp.Height
        Dim imgRect As New Rectangle(e.Bounds.X + ((e.Bounds.Width - Image_Width) / 2), e.Bounds.Y, Image_Width, Image_Height)

        'Declare Text Rectangle 
        Dim textSize As SizeF = New SizeF(e.Graphics.MeasureString(StoreName, Me.Font, 100))
        Dim textRect As New Rectangle(e.Bounds.X + ((e.Bounds.Width - textSize.Width) / 2), e.Bounds.Bottom - textSize.Height, textSize.Width + 1, textSize.Height)

        e.Graphics.SmoothingMode = SmoothingMode.HighQuality

        If e.Item.Selected Then
            'Set the Image to use the 'blue' color matrix and highlight the text
            imgattr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
            e.Graphics.FillRectangle(System.Drawing.SystemBrushes.Highlight, textRect)
        Else
            'Turn off the color matrix and draw the default background
            imgattr = Nothing
            e.DrawBackground()
        End If

        'Draw the Image
        e.Graphics.DrawImage(bmp, imgRect, 0, 0, Image_Width, Image_Height, GraphicsUnit.Pixel, imgattr)
        storeCode_flags.Alignment = StringAlignment.Center
        e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
        Dim rect2 As New Rectangle(e.Bounds.X + ((e.Bounds.Width - Image_Width) / 2) + 1, e.Bounds.Y + 15, Image_Width, Image_Height)
        e.Graphics.DrawString(StoreNum, New Font(CustomFnt.Families(0), 24, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.Black, rect2, storeCode_flags)

        'Draw the Text
        storeName_flags.Alignment = StringAlignment.Center
        storeName_flags.LineAlignment = StringAlignment.Far
        storeName_flags.FormatFlags = StringFormatFlags.FitBlackBox
        e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
        e.Graphics.DrawString(StoreName, Me.Font, Brushes.Black, textRect, storeName_flags)

        bmp.Dispose()
        MyBase.OnDrawItem(e)

    Catch ex As Exception
        ErrorTrap(ex, "ListView_Stores: OnDrawItem()")
    End Try
End Sub

现在不是这样:

我明白了:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24134409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档