我如何通過左側繪製圖像中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