如何在自定义tabcontrol上绘制ImageList图像

时间:2016-01-15 19:41:48

标签: vb.net tabcontrol imagelist

您好我有一个自定义标签控件,可以在下面看到onpaint代码:

Dim B As New Bitmap(Width, Height)
    Dim G As Graphics = Graphics.FromImage(B)

    G.Clear(Color.FromArgb(46, 46, 46))

    For i = 0 To TabCount - 1


        Dim TabRectangle As Rectangle = GetTabRect(i)

        '27, 80, 116
        Dim brush = New SolidBrush(Color.FromArgb(255, CByte(46), CByte(46), CByte(46)))
        Dim brush2 = New SolidBrush(Color.FromArgb(255, CByte(30), CByte(30), CByte(30)))

        Dim fontbursh = Brushes.White
        Dim thefont = New Drawing.Font("Microsoft Sans Serif", 12, FontStyle.Regular)
        If i = SelectedIndex Then
            fontbursh = Brushes.White
            G.FillRectangle(brush2, TabRectangle)
            thefont = New Drawing.Font("Microsoft Sans Serif", 12, FontStyle.Regular)
            G.DrawString(TabPages(i).Text, thefont, Brushes.White, TabRectangle, New StringFormat With {.Alignment = StringAlignment.Far, .LineAlignment = StringAlignment.Center})
        Else
            fontbursh = Brushes.Gray
            G.FillRectangle(brush, TabRectangle)
            G.DrawString(TabPages(i).Text, thefont, Brushes.Gray, TabRectangle, New StringFormat With {.Alignment = StringAlignment.Far, .LineAlignment = StringAlignment.Center})

        End If

    Next

    e.Graphics.DrawImage(B.Clone, 0, 0)
    G.Dispose() : B.Dispose()

    MyBase.OnPaint(e)
End Sub

如何将指定的图像索引从图像列表绘制到这些标签上?

1 个答案:

答案 0 :(得分:2)

我能够使用以下代码绘制图像:

 If (TabPages(i).ImageIndex >= 0) AndAlso (ImageList IsNot Nothing) AndAlso (Main.ImageList1.Images(TabPages(i).ImageIndex) IsNot Nothing) Then


            Dim img As Image = Main.ImageList1.Images(TabPages(i).ImageIndex)



            Dim rimage As New Rectangle(TabRectangle.X + 16, TabRectangle.Y, img.Width, img.Height)

            ' adjust rectangles


            rimage.Y += (TabRectangle.Height - img.Height) / 2


            ' draw icon
            G.DrawImage(img, rimage)


        End If