2013-12-20 112 views
3

我設法自定義帶有圖像的正常列表框,當在ownerdrawn中選擇項目時更改文本和背景顏色,我現在想要實現的是在項目上繪製自定義突出顯示顏色當鼠標懸停是在列表框上的項目,是可能或不...,我提供我下面的示例代碼是什麼我走了這麼遠..鼠標懸停在項目上時突出顯示列表框項目

If e.Index = -1 Then Exit Sub 
     Dim listBox As ListBox = CType(sender, ListBox) 
     e.DrawBackground() 
     Dim isItemSelected As Boolean = ((e.State And DrawItemState.Selected) = DrawItemState.Selected) 
     If e.Index >= 0 AndAlso e.Index < listBox.Items.Count Then 
      Dim textSize As SizeF = e.Graphics.MeasureString(listBox.Items(e.Index).ToString(), listBox.Font) 
      Dim itemImage As Image = My.Resources.FolderHorizontal 
      'set background and text color 
      Dim backgroundColorBrush As New SolidBrush(If((isItemSelected), Color.CornflowerBlue, Color.White)) 
      Dim itemTextColorBrush As Color = If((isItemSelected), Color.White, Color.Black) 

      e.Graphics.FillRectangle(backgroundColorBrush, e.Bounds) 
      'draw the item image 
      e.Graphics.SmoothingMode = SmoothingMode.HighQuality 
      e.Graphics.DrawImage(itemImage, e.Bounds.X + 2, _ 
           e.Bounds.Y + (e.Bounds.Height - textSize.Height)/2, _ 
           itemImage.Width, itemImage.Height) 
      'draw the item text 
      Dim x, y As Single 
      Dim h As Single = textSize.Height 
      Dim rect As Rectangle = e.Bounds 
      rect.X += listBox.ItemHeight 
      rect.Width -= listBox.ItemHeight 

      x = rect.X - 3 
      y = rect.Y + (rect.Height - h)/2 

      Dim itemText As String = listBox.Items(e.Index).ToString() 
      TextRenderer.DrawText(e.Graphics, itemText, e.Font, _ 
            New Rectangle(x, y, ClientRectangle.Width, ClientRectangle.Height), _ 
            itemTextColorBrush, TextFormatFlags.Default) 
      'clean up 
      backgroundColorBrush.Dispose() 

     End If 
     e.DrawFocusRectangle() 

回答

2

可以使用IndexFromPoint做這樣的事情:

Dim mouseIndex As Integer = -1 

Private Sub ListBox1_MouseMove(sender As Object, e As MouseEventArgs) _ 
           Handles ListBox1.MouseMove 
    Dim index As Integer = ListBox1.IndexFromPoint(e.Location) 
    If index <> mouseIndex Then 
    If mouseIndex > -1 Then 
     Dim oldIndex As Integer = mouseIndex 
     mouseIndex = -1 
     If oldIndex <= ListBox1.Items.Count - 1 Then 
     ListBox1.Invalidate(ListBox1.GetItemRectangle(oldIndex)) 
     End If 
    End If 
    mouseIndex = index 
    If mouseIndex > -1 Then 
     ListBox1.Invalidate(ListBox1.GetItemRectangle(mouseIndex)) 
    End If 
    End If 
End Sub 

然後在您的圖紙代碼中:

If mouseIndex > -1 AndAlso mouseIndex = e.Index Then 
    backgroundColorBrush = New SolidBrush(Color.DarkMagenta) 
End If 
+0

感謝,它的工作的......有一點小問題,我該如何最小化閃爍,我認爲它重繪每次移動時鼠標放在物品上,或者有另一種方式做...但除此之外,它的效果很好 – mercenary

+0

@mercenary ListBox控件相當無法控制地閃爍着快樂,但我更新了該帖子以減少其中的一些內容。考慮使用ListView控件。 – LarsTech

+0

謝謝,我在想使用listview,也許在下一個版本的工具,它的文件夾圖標轉換器使用列表框一次容納多個文件夾... – mercenary

1

我會告訴你如何做到這一點。所有專家說,它的複雜,不能用列表框來完成......我能做到這一點在5分鐘內

我創建的列表框中的名稱是listPOSSIBILITIES

1)創建一個變量,它是全局的表單

Dim HOVERTIME As Boolean = True 

2)創建MouseEnter事件

私人小組listPOSSIBILITIES_MouseEnter(發送者爲對象,例如作爲system.EventArgs)把手listPOSSIBILITIES.MouseEnter

HOVERTIME = True 

結束子

3)創建鼠標離開事件

私人小組listPOSSIBILITIES_MouseLeave(發送者爲對象,例如作爲System.EventArgs)把手listPOSSIBILITIES.MouseLeave

HOVERTIME = False 

結束子

4)創建MouseMove事件

私人小組listPOSSIBILITIES_MouseMove(發送者爲對象,例如作爲System.Windows.Forms.MouseEventArgs)把手listPOSSIBILITIES.MouseMove

Dim mypoint As Point 
    mypoint = listPOSSIBILITIES.PointToClient(Cursor.Position) 
    Dim myindex As Integer = listPOSSIBILITIES.IndexFromPoint(mypoint) 
    If myindex < 0 Then Exit Sub 
    listPOSSIBILITIES.SelectedIndex = myindex 

結束子

5)創建MouseClick事件

私人小組listPOSSIBILITIES_MouseClick( sender As Object,e As System.Windows.Forms.MouseEventArgs)Handles listPOSSIBILITIES.MouseClick

HOVERTIME = False 

結束子

6)創建SelectedIndexChanged事件

私人小組listPOSSIBILITIES_SelectedIndexChanged(發件人爲System.Object的,例如作爲System.EventArgs)把手listPOSSIBILITIES。的SelectedIndexChanged

If HOVERTIME Then Exit Sub 

    'put the rest of your code after this above If statement 

這工作,因爲鼠標點擊事件在SelectIndexChanged事件之前引發

相關問題