1
我想完成我使用LargeIcon視圖的自定義Listview控件。我試圖在OnDrawItem事件中自定義繪製Item。無法正確覆蓋OnDrawItem與OwnerDrawn ListView使用LargeIcon視圖
到目前爲止,我有以下代碼:
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
然而,當我跑我的代碼,它吸引我的文字和圖標正確,但我似乎無法獲得該項目按我的圖片突出正確如下:
它不與任何顏色(只是一個虛方塊)正確地強調和它甚至沒有突出顯示對象的整個邊界 - 它扒通過文字一半。
想知道是否有人可以協助或至少指出我在正確的方向。 感謝
你只是得到了(括號ses)錯誤。你想'如果不是(... = 0)那麼' –
感謝漢斯,但這似乎沒有太大的區別。我發佈了一個半低的答案,似乎突出顯示工作,但不是100% – Riples