2014-06-10 61 views
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 

然而,當我跑我的代碼,它吸引我的文字和圖標正確,但我似乎無法獲得該項目按我的圖片突出正確如下:

enter image description here

它不與任何顏色(只是一個虛方塊)正確地強調和它甚至沒有突出顯示對象的整個邊界 - 它扒通過文字一半。

想知道是否有人可以協助或至少指出我在正確的方向。 感謝

+0

你只是得到了(括號ses)錯誤。你想'如果不是(... = 0)那麼' –

+0

感謝漢斯,但這似乎沒有太大的區別。我發佈了一個半低的答案,似乎突出顯示工作,但不是100% – Riples

回答

1

好了,隨着研究和反覆試驗一個LOT我已成功地實現正是我之後在我的自定義類中重寫列表項的OnDraw的事件。我不確定這是否是正確的(或首選)方法,但我對結果感到滿意。

我最終使用ColorMatrix方法將「藍色」突出顯示顏色疊加到我選擇的項目上。然後,未選擇的時候,我只是把我的嘉洛斯不了了之

我的新修改後的代碼:

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 

,而不是現在這樣:

enter image description here

我得到這個:

enter image description here