【问题标题】:add icon to listbox vb.net将图标添加到列表框 vb.net
【发布时间】:2011-03-17 11:37:06
【问题描述】:

请,

如何在左侧的 listbox_DrawItem 事件中绘制图像

我已经阅读了this code,但它对我没有帮助

Dim targetsize As New Size(16, 16)
Dim img As Image = Nothing
img = My.Resources._error
e.Graphics.DrawImage(img, targetsize)
e.Graphics.DrawString(lsbLog.Items(e.Index).ToString(), _
                              e.Font, mybrush, e.Bounds, StringFormat.GenericDefault)

这是我当前的代码

编辑

我在你的代码中添加了一些其他代码,我得到一个乱码

这是 DrawItem 事件中代码的一部分

'//Here it draws the border depeding on it's state (the listbox item)
        e.Graphics.DrawRectangle(myPen, e.Bounds.X + 16, e.Bounds.Y, _
                                 e.Bounds.Width - 16, e.Bounds.Height)
        Using b As New SolidBrush(e.ForeColor)
            e.Graphics.DrawString(lsbLog.GetItemText(lsbLog.Items(e.Index)), e.Font, b, e.Bounds)
        End Using
        e.Graphics.DrawImage(img, New Rectangle(e.Bounds.Width - 15, e.Bounds.Y, 12, 12))
        '// Draw the current item text based on the current 
        '// Font and the custom brush settings.
        e.Graphics.DrawString(lsbLog.Items(e.Index).ToString(), e.Font, mybrush, _
                               New Rectangle(e.Bounds.X - 20, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), _
                               StringFormat.GenericDefault)

这是 MeasureItem 事件中的代码

 Private Sub lsbLog_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles lsbLog.MeasureItem
        Dim g As Graphics = e.Graphics
        'We will get the size of the string which we are about to draw,
        'so that we could set the ItemHeight and ItemWidth property
        Dim size As SizeF = g.MeasureString(lsbLog.Items.Item(e.Index).ToString, Me.Font, _
        lsbLog.Width - (5 + SystemInformation.VerticalScrollBarWidth))
        e.ItemHeight = CInt(size.Height) + 5
        e.ItemWidth = CInt(size.Width) + 5
    End Sub

我得到一个乱码的文本和图像

【问题讨论】:

  • 您检查过这是否有帮助? codeproject.com/KB/combobox/glistbox.aspx
  • @Simen 人评论那篇文章非常糟糕且充满漏洞
  • 你想达到什么目的?如果想让图片左对齐,为什么要用Width - 15作为X坐标呢?你应该在这里使用e.Bounds.X。为什么要在列表框outside 开始文本(即您将X 设置为e.Bounds.X - 20)?不应该是e.Bounds.X + 20吗?
  • PS:你会在事件处理程序的开头调用 DrawBackground 吗?

标签: vb.net winforms image listbox draw


【解决方案1】:

我注意到了两点:

  • 您是否将DrawMode 设置为DrawMode.OwnerDrawFixedDrawMode.OwnerDrawVariable,如stated in the documentation

  • 您似乎是直接在图像上绘制文本。为什么在 DrawString 中使用e.Bounds 而不是从右侧开始的矩形?例如。类似:

    Dim rect As New Rectangle(e.Bounds.X + 16, e.Bounds.Y, _
                              e.Bounds.Width - 16, e.Bounds.Height)
    ' use rect instead of e.Bounds in DrawString
    

顺便说一句,您不应该忘记调用DrawBackgroundDrawFocusRectange,如示例in the documentation 所示。

【讨论】:

  • 是的,我将绘制模式设置为DrawMode.OwnerDrawVariable。你能给我e.bounds的替代方案吗?
  • @Smith:我会使用 OwnerDrawFixed,因为您不会更改项目的高度。我添加了一个关于如何调整字符串的矩形边界的示例。顺便说一句,作为调试提示:如果您只是注释掉 DrawString 语句,它是否正确绘制图像?
【解决方案2】:

试试这个相关的帖子,你只需要编辑一点代码,根据字体大小,我只需要 8.25pt 字体大小,当字体大小更大时图标会乱,它不会排列在字体高度中心

Highlighting Listbox Item when Mouseover on Item

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多