vb.net列表显示图标 文字前面一个图标
答案:2 悬赏:80 手机版
解决时间 2021-03-13 08:36
- 提问者网友:我的未来我做主
- 2021-03-12 19:06
vb.net列表显示图标 文字前面一个图标
最佳答案
- 五星知识达人网友:酒安江南
- 2021-03-12 19:12
Public Class Form1
Private Imgfile As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListBox1.DrawMode = DrawMode.OwnerDrawVariable
Imgfile = "X:\Users\......\Pictures\ssm.png"‘显示为listbox的每一行文字前面的图标文件,换一张你自己的图片。
End Sub
Private Sub ListBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ListBox1.DrawItem
e.DrawBackground()
Dim rect As Rectangle = New Rectangle(2, e.Bounds.Y + 2, e.Bounds.Height, e.Bounds.Height - 4)
e.Graphics.DrawImage(New Bitmap(Imgfile), rect)
e.Graphics.DrawString(sender.Items(e.Index), sender.Font, New SolidBrush(sender.ForeColor), _
New RectangleF(e.Bounds.X + rect.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
e.DrawFocusRectangle()
End Sub
End Class追问图标不同呢,要循环吗?帮我写一下吧,list2为对应的图标文件地址追答Using itemimg As New Bitmap(ListBox2.Items(e.Index)) '第一张图片对应listbox第一行,第二张对应第二行......
e.Graphics.DrawImage(itemimg, rect)
End Using
Using solid As New SolidBrush(sender.ForeColor)
e.Graphics.DrawString(sender.Items(e.Index), sender.Font, solid, _
New RectangleF(e.Bounds.X + rect.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
End Using
替换第一次回答中的drawimage和drawstring语句。
源图片要尽可能小,否则占用内存会很大,尤其是在项数多的情况下。追问谢谢你!竟然有人编写这么多代码给我,感动!我周六测试一下再采纳
Private Imgfile As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListBox1.DrawMode = DrawMode.OwnerDrawVariable
Imgfile = "X:\Users\......\Pictures\ssm.png"‘显示为listbox的每一行文字前面的图标文件,换一张你自己的图片。
End Sub
Private Sub ListBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ListBox1.DrawItem
e.DrawBackground()
Dim rect As Rectangle = New Rectangle(2, e.Bounds.Y + 2, e.Bounds.Height, e.Bounds.Height - 4)
e.Graphics.DrawImage(New Bitmap(Imgfile), rect)
e.Graphics.DrawString(sender.Items(e.Index), sender.Font, New SolidBrush(sender.ForeColor), _
New RectangleF(e.Bounds.X + rect.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
e.DrawFocusRectangle()
End Sub
End Class追问图标不同呢,要循环吗?帮我写一下吧,list2为对应的图标文件地址追答Using itemimg As New Bitmap(ListBox2.Items(e.Index)) '第一张图片对应listbox第一行,第二张对应第二行......
e.Graphics.DrawImage(itemimg, rect)
End Using
Using solid As New SolidBrush(sender.ForeColor)
e.Graphics.DrawString(sender.Items(e.Index), sender.Font, solid, _
New RectangleF(e.Bounds.X + rect.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
End Using
替换第一次回答中的drawimage和drawstring语句。
源图片要尽可能小,否则占用内存会很大,尤其是在项数多的情况下。追问谢谢你!竟然有人编写这么多代码给我,感动!我周六测试一下再采纳
全部回答
- 1楼网友:猎心人
- 2021-03-12 20:12
Public Class Form1
Private Imgfile As String
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
ListBox1.DrawMode = DrawMode.OwnerDrawVariable
Imgfile = "X:\Users\......\Pictures\ssm.png" '显示为listbox的每一行文字前面的图标文件,换一张你自己的图片。
End Sub
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles ListBox1.DrawItem
e.DrawBackground()
Dim rect As Rectangle = New Rectangle(2, e.Bounds.Y + 2, e.Bounds.Height, e.Bounds.Height - 4)
Using itemimg As New Bitmap(ListBox2.Items(e.Index)) '第一张图片对应listbox第一行,第二张对应第二行......
e.Graphics.DrawImage(itemimg, rect)
End Using
Using solid As New SolidBrush(sender.ForeColor)
e.Graphics.DrawString(sender.Items(e.Index), sender.Font, solid, _
New RectangleF(e.Bounds.X + rect.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
End Using
e.DrawFocusRectangle()
End Sub
End Class
不对,VB2005
Private Imgfile As String
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
ListBox1.DrawMode = DrawMode.OwnerDrawVariable
Imgfile = "X:\Users\......\Pictures\ssm.png" '显示为listbox的每一行文字前面的图标文件,换一张你自己的图片。
End Sub
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles ListBox1.DrawItem
e.DrawBackground()
Dim rect As Rectangle = New Rectangle(2, e.Bounds.Y + 2, e.Bounds.Height, e.Bounds.Height - 4)
Using itemimg As New Bitmap(ListBox2.Items(e.Index)) '第一张图片对应listbox第一行,第二张对应第二行......
e.Graphics.DrawImage(itemimg, rect)
End Using
Using solid As New SolidBrush(sender.ForeColor)
e.Graphics.DrawString(sender.Items(e.Index), sender.Font, solid, _
New RectangleF(e.Bounds.X + rect.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
End Using
e.DrawFocusRectangle()
End Sub
End Class
不对,VB2005
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯