Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2015
    Messaggi
    15

    [VB.NET] Come allineare al centro le TabPages di un TabControl?

    Salve a tutti!
    Sto realizzando un menù stile "ribbon" partendo da un TabControl, ma non riesco ad allineare al centro le schede. "Graficamente" riesco a disegnarle al centro dall'evento OnPaint, ma dal punto di vista funzionale, non avendole spostate, è come se fossero allineate a sinistra.
    screen1.jpg
    Ho aggiunto sotto un TabControl standard con le stesse dimensioni e potete vedere come le schede siano disallineate.
    Esiste una proprietà\metodo che mi permette di spostare e allineare al centro le schede come ho fatto io?
    Ho già tentato modificando Size, Location e SetBounds di ogni tabPage dall'evento OnPaint, ma non cambia nulla.
    Se può essere utile, questo è il codice dell'evento OnPaint:
    codice:
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    
            e.Graphics.Clear(Color.FromArgb(49, 49, 49))
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit
    
            Try : For Each T As TabPage In TabPages
                    T.BackColor = Color.FromArgb(49, 49, 49)
                Next : Catch : End Try
    
            Dim headerRect As New Rectangle(New Point(0, 0), New Point(Me.Width, ItemSize.Height))
            e.Graphics.DrawLine(New Pen(Color.FromArgb(35, 35, 35)), New Point(0, ItemSize.Height + 1), New Point(Me.Width, ItemSize.Height + 1))
            Dim headerColorBlend As New ColorBlend(2)
            headerColorBlend.Colors = New Color() {Color.FromArgb(60, 101, 101, 101), Color.FromArgb(49, 49, 49)}
            headerColorBlend.Positions = New Single() {0.0, 1.0}
            Dim headerBrush As New LinearGradientBrush(headerRect, Color.Black, Color.Black, 90.0F)
            headerBrush.InterpolationColors = headerColorBlend
            e.Graphics.FillRectangle(headerBrush, headerRect)
    
    
            For i = 0 To TabCount - 1
    
                Dim x2 As Rectangle = New Rectangle(New Point(GetTabPositionX(i), GetTabRect(i).Location.Y - 2), New Size(GetTabRect(i).Width, GetTabRect(i).Height))
                Dim textrectangle As New Rectangle(x2.Location.X, x2.Location.Y, x2.Width, x2.Height)
    
                If i = SelectedIndex Then
                    e.Graphics.FillRectangle(Brushes.CadetBlue, New Rectangle(New Point(x2.X + 1, x2.Y + x2.Height - 1), New Point(GetTabRect(i).Width - 1, 2)))
                    e.Graphics.DrawString(TabPages(i).Text, Font, Brushes.Gainsboro, textrectangle, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                    e.Graphics.DrawLine(New Pen(Color.FromArgb(49, 49, 49)), New Point(x2.X, x2.Y + x2.Height + 1), New Point(x2.X + x2.Width - 1, x2.Y + x2.Height + 1))
                Else
                    e.Graphics.DrawString(TabPages(i).Text, Font, Brushes.Gray, textrectangle, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
                End If
    
                Dim selTabPath As New GraphicsPath
                selTabPath = RoundRect(New Rectangle(New Point(GetTabPositionX(i) + 4, GetTabRect(i).Location.Y - 2), New Size(GetTabRect(i).Width - 8, GetTabRect(i).Height - 2)), 2)
                If i = _IndexOver Then
                    'e.Graphics.FillPath(New LinearGradientBrush(x2, Color.FromArgb(25, 200, 200, 200), Color.FromArgb(100, 49, 49, 49), 90.0F), selTabPath)
                    e.Graphics.DrawPath(New Pen(Color.FromArgb(80, 100, 100, 100)), selTabPath)
                End If
    
            Next
    
        End Sub
    E questa invece è la funzione che, dato l'indice della TabPage, ritorna la coordinata di X per poter centrare le schede nel controllo:
    codice:
    Private Function GetTabPositionX(ByVal tabIndex As Integer) As Integer
            Dim firstTabX As Integer = Nothing
            Dim tabPagesWidth As Integer = Nothing
            Dim pages As Integer = Nothing
            For i = 0 To TabCount - 1
                tabPagesWidth = tabPagesWidth + GetTabRect(i).Width
            Next
    
            For i = 0 To tabIndex - 1
                pages = pages + GetTabRect(i).Width
            Next
    
            If tabIndex > 0 Then
                firstTabX = (Me.Width \ 2) - (tabPagesWidth \ 2) + pages
            Else
                firstTabX = (Me.Width \ 2) - (tabPagesWidth \ 2)
            End If
    
    
            Return firstTabX
        End Function
    Grazie in anticipo!

  2. #2
    Utente di HTML.it
    Registrato dal
    Feb 2015
    Messaggi
    15
    Nessuno?

Tag per questa discussione

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.