我如何通过左侧绘制图像中listbox_DrawItem事件
我已经读过throught 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)
这是我现在的代码
编辑
我添加了你的c颂与其他一些代码,我得到一个乱码
这是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
代码我得到一个加标签的文字和图像
您检查了这是否有帮助吗? http://www.codeproject.com/KB/combobox/glistbox.aspx – 2011-03-17 12:03:22
@Simen人对这篇文章评论为非常差,并且bug填充 – Smith 2011-03-17 12:28:03
你想达到什么目的?如果你想让图像左对齐,为什么使用'Width - 15'作为X坐标?你应该在这里使用'e.Bounds.X'。为什么你在列表框的外部开始文本*(即你把X设置为'e.Bounds.X - 20')?不应该是'e.Bounds.X + 20'吗? – Heinzi 2011-03-17 14:56:54