TabControl OnPaint calling

本文关键字:calling OnPaint TabControl | 更新日期: 2023-09-27 18:27:15

我有一个自定义的TabControl控件。这是OnPaint方法:

Protected Overrides Sub OnPaint(e As PaintEventArgs)
        G = e.Graphics
        G.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
        G.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
        MyBase.OnPaint(e)
        G.Clear(ColorFromHex("#343843"))
        For I As Integer = 0 To TabPages.Count - 1
            Rect = GetTabRect(I)
            Dim Data As AccountData = DirectCast(TabPages(I).Tag, AccountData)
            If Data IsNot Nothing Then
                TabPages(I).ImageIndex = Data.Icon / 2
            End If
            If SelectedIndex = I Then
                Using B1 As New SolidBrush(ColorFromHex("#3A3E49"))
                    G.FillRectangle(B1, New Rectangle(Rect.X - 4, Rect.Y + 1, Rect.Width + 6, Rect.Height))
                End Using
            End If
            Using B1 As New SolidBrush(ColorFromHex("#737A8A"))
                If UpperText Then
                    Using F1 As New Font("Segoe UI", 7.75, FontStyle.Bold)
                        G.DrawString(TabPages(I).Text.ToUpper, F1, B1, New Point(Rect.X + 70, Rect.Y + 10))
                        If Data Is Nothing Then
                            G.DrawString("Idle", F1, B1, New Point(Rect.X + 70, Rect.Y + 20))
                        Else
                            G.DrawString(Data.Status, F1, B1, New Point(Rect.X + 70, Rect.Y + 20))
                            G.DrawString("Lv. " & Data.Level, F1, B1, New Point(Rect.X + 70, Rect.Y + 30))
                            G.DrawString(Data.IP & " IP / " & Data.RP & " RP", F1, B1, New Point(Rect.X + 70, Rect.Y + 40))
                        End If
                    End Using
                Else
                    Using F1 As New Font("Segoe UI semibold", 9)
                        G.DrawString(TabPages(I).Text, F1, B1, New Point(Rect.X + 50, Rect.Y + 11))
                    End Using
                End If
            End Using
            If Not I = 0 Then
                Using P1 As New Pen(ColorFromHex("#3B3D49")), P2 As New Pen(ColorFromHex("#2F323C"))
                    G.DrawLine(P1, New Point(Rect.X - 4, Rect.Y + 1), New Point(Rect.Width + 4, Rect.Y + 1))
                    G.DrawLine(P2, New Point(Rect.X - 4, Rect.Y + 2), New Point(Rect.Width + 4, Rect.Y + 2))
                End Using
            End If
            If Not IsNothing(ImageList) Then
                If Not TabPages(I).ImageIndex < 0 Then
                    G.DrawImage(ImageList.Images(TabPages(I).ImageIndex), New Rectangle(Rect.X + 10, Rect.Y + 10, 48, 48))
                End If
            End If
        Next
    End Sub

正如您所看到的,它检查标签。如果标记不为null,它将绘制Status属性、Level属性和IP/RP属性。每个标签都与玩家相关,因此当玩家登录时,标签被设置为适当的AccountData值,然后我调用以下命令来更新它:

public void UpdateAccountData(int index, AccountData data)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(() => this.UpdateAccountData(index, data)));
                return;
            }
            this.aetherTabControl1.TabPages[index].Tag = data;
            this.aetherTabControl1.TabPages[index].Invalidate(true);
            this.aetherTabControl1.TabPages[index].Update();
            this.aetherTabControl1.TabPages[index].Refresh();
        }

我知道这很糟糕,但我不知道每次更新AccountData的属性时如何重新绘制它。有没有更好的方法可以做到这一点,而不是每次都重新粉刷?

谢谢。

TabControl OnPaint calling

是的,有更好的方法
方法1:您的AccountData属性通过标签、文本框或其他控件反映在屏幕上,对吗?!?如果在AccountData更新时重置这些控件的值,它们将自动重新绘制。。。

方法2:坚持"自己重新绘制"的方法,你可以缓冲一堆东西,比如Brushers、Colors,也许还可以动态地翻转精灵中的图像,所以在重新绘制的最后,你只需要绘制精灵的不同部分,而不是整个。。。