如何在组合框项目前放置小图片?

时间:2012-02-14 07:40:55

标签: vb.net visual-studio

我需要你的一些时间来帮助我,我正在创建一个项目,我需要在组合框项目前设置像32X32或16X16像素的小图片。 提前谢谢。

1 个答案:

答案 0 :(得分:3)

此链接可以为您提供帮助:http://www.codeproject.com/Articles/10670/Image-ComboBox-Control

编辑,要在VB中执行此操作,请将您的组合设置为ownerdraw,然后:

protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)

    e.DrawBackground()
    e.DrawFocusRectangle()
    Dim item As New ComboBoxIconItem
    Dim imageSize As New Size
    imageSize = ListaImg1.ImageSize
    Dim bounds As New Rectangle
    bounds = e.Bounds
    Try
        item = Me.Items(e.Index)
        If (item.ImageIndex <> -1) Then
            Me.ImageList.Draw(e.Graphics, bounds.Left, _
                bounds.Top, item.ImageIndex)
            e.Graphics.DrawString(item.Text, e.Font, _
                New SolidBrush(e.ForeColor), bounds.Left + _
                imageSize.Width, bounds.Top)
        Else
            e.Graphics.DrawString(item.Text, e.Font, _
                New SolidBrush(e.ForeColor), bounds.Left, _
                bounds.Top)
        End If
        Catch ex As Exception
        If (e.Index <> -1) Then
            e.Graphics.DrawString(Items(e.Index).ToString(), e.Font, _
                New SolidBrush(e.ForeColor), bounds.Left, bounds.Top)
        Else
            e.Graphics.DrawString(Text, e.Font, _
                New SolidBrush(e.ForeColor), bounds.Left, bounds.Top)
        End If
    End Try
    MyBase.OnDrawItem(e)
End Sub