在ownerdrawfixed中为标签添加图标

时间:2016-03-28 14:14:39

标签: vb.net winforms

尝试为此获得解决方案。在这里,我只是在TabControl中向标签添加图标。 DrawMode更改为 OwnerDrawFixed

以下是我在TabControl的 DrawITem 事件中尝试过的内容:

Dim tab_rect As Rectangle = TabControl1.GetTabRect(e.Index)
Dim TAB_MARGIN As Integer = 3

Dim layout_rect As New RectangleF( _
       tab_rect.Left + TAB_MARGIN, _
       tab_rect.Y + TAB_MARGIN, _
       tab_rect.Width - 4 * TAB_MARGIN, _
       tab_rect.Height - 2 * TAB_MARGIN)

为了添加图标,我使用:

e.Graphics.DrawImage(My.Resources.airline, layout_rect.Right - 90, layout_rect.Left)

但是,代码工作正常并添加了图像。 但是当我切换到另一个标签时,一个标签的图像会消失。为什么会发生这种情况呢?

欢迎任何出于同一目的的替代想法。

好的,为了排除故障,我正在为TabControl_DrawItem事件添加整个代码。这是一个:

Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem

    'Firstly we'll define some parameters.
    Dim CurrentTab As TabPage = TabControl1.TabPages(e.Index)
    Dim ItemRect As Rectangle = TabControl1.GetTabRect(e.Index)
    Dim FillBrush As New SolidBrush(Color.FromArgb(224, 229, 249))
    Dim TextBrush As New SolidBrush(Color.Black)
    Dim sf As New StringFormat
    sf.Alignment = StringAlignment.Far
    sf.LineAlignment = StringAlignment.Center

    'If we are currently painting the   TabItem we'll 
    'change the brush colors and inflate the rectangle.
    If CBool(e.State And DrawItemState.Selected) Then
        FillBrush.Color = Color.FromArgb(224, 229, 249)
        TextBrush.Color = Color.Black
        ItemRect.Inflate(2, 2)
    End If

    'Set up rotation for left and right aligned tabs
    If TabControl1.Alignment = TabAlignment.Left Or TabControl1.Alignment = TabAlignment.Right Then
        Dim RotateAngle As Single = 90
        If TabControl1.Alignment = TabAlignment.Left Then RotateAngle = 270
        Dim cp As New PointF(ItemRect.Left + (ItemRect.Width \ 2), ItemRect.Top + (ItemRect.Height \ 2))
        e.Graphics.TranslateTransform(cp.X, cp.Y)
        e.Graphics.RotateTransform(RotateAngle)
        ItemRect = New Rectangle(-(ItemRect.Height \ 2), -(ItemRect.Width \ 2), ItemRect.Height, ItemRect.Width)
    End If

    'Next we'll paint the TabItem with our Fill Brush
    e.Graphics.FillRectangle(FillBrush, ItemRect)

    Dim tab_rect As Rectangle = TabControl1.GetTabRect(e.Index)

    Dim layout_rect As New RectangleF( _
       tab_rect.Left + TAB_MARGIN, _
      tab_rect.Y + TAB_MARGIN, _
     tab_rect.Width - 4 * TAB_MARGIN, _
    tab_rect.Height - 2 * TAB_MARGIN)
 e.Graphics.DrawString(CurrentTab.Text, e.Font, TextBrush, RectangleF.op_Implicit(ItemRect), sf)
    '



'Here I add Images for each tab header
e.Graphics.DrawImage(My.Resources.airline, layout_rect.Left, layout_rect.Left, 20, 20)



    e.Graphics.DrawImage(My.Resources.hotel, layout_rect.Right + 15, layout_rect.Left - 1, 20, 20)



    e.Graphics.DrawImage(My.Resources.insurance, layout_rect.Right + 82, layout_rect.Left - 1, 20, 20)
    e.Graphics.DrawImage(My.Resources.rail, layout_rect.Right + 177, layout_rect.Left - 2, 20, 20)
    e.Graphics.DrawImage(My.Resources.tour, layout_rect.Right + 240, layout_rect.Left - 2, 20, 20)
    '
    'End If


    'Reset any Graphics rotation
    e.Graphics.ResetTransform()

    'Finally, we should Dispose of our brushes.
    FillBrush.Dispose()
    TextBrush.Dispose()

End Sub

0 个答案:

没有答案