从类实例

时间:2016-08-24 12:45:16

标签: vb.net graphics

我对winforms中的Graphics类没有多少经验。 我只是在草绘阶段(也是我添加的代码)。 我的问题是我正在尝试创建一个面板:clockPanel上面有一些图形,没有抛出异常,但面板(我在UI中可以看到)上没有图形。试图寻找例子但我无法在我的代码中找到错误或遗漏的东西。对于那些有图形经验的人来说,这可能很简单。 谢谢你的时间考虑。

VB代码: 通过实例将'clockpanel'面板添加到其他面板('secondaryPannel')到GoalsClock类

Public Class ManagersTab
...
  Public Sub BuiledDashBoard()
...
  Dim p As GoalsClock = New GoalsClock(100, 100, 0.8)
        p.Create()
        p.clockPanel.Location = New Point(200, 100)
        secondaryPannel.Controls.Add(p.clockPanel)
...
  End Sub
...
End Class

Create()方法是最相关的部分:

Class GoalsClock

    Private Gclock As Graphics
    Private clockWidth As Int16
    Private clockHeight As Int16
    Private xPos As Int16
    Private yPos As Int16

    Public clockPanel As Panel
    Private panelColor As Color

    Private PercentTextColor As Color
    ' rectangles to store squares
    Protected OuterRect As Rectangle
    Protected InnerRect As Rectangle
    Protected InnerStringBrush As Brush
    Protected InnerStringColor As Color
    Protected InnerStringFontSize As Byte

    ' inner square
    Private InnerSquarePen As Pen
    Private InnerSquarePen_Color As Color
    Private InnerSquarePen_Width As Byte
    ' outer square
    Private OuterSquarePen As Pen
    Private OuterSquarePen_Color As Color
    Private OuterSquarePen_Width As Byte

    Private _PercentOfGoals As Single ' to calculate the goals deg arc
    Public Property PercentOfGoals() As Single
        Get
            Return _PercentOfGoals * 100
        End Get
        Private Set(ByVal value As Single)
            If value <= 1.0F Then
                _PercentOfGoals = value
            Else
                value = 0
            End If
        End Set
    End Property

    Sub New(ByVal clockWidth As Int16, ByVal clockHeight As Int16, ByVal GoalsPercent As Single)
        Me.clockWidth = clockWidth
        Me.clockHeight = clockHeight
        PercentOfGoals = GoalsPercent


        ' values for test
        xPos = 0
        yPos = 0
        InnerStringFontSize = 12
        OuterSquarePen = New Pen(Color.Gray)
        InnerSquarePen = New Pen(Color.Cyan)
        OuterSquarePen_Width = 23
        InnerSquarePen_Width = 15
    End Sub

    ''' <summary>
    ''' 
    '''  create graphics of the goals clock on clockPanel
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Create()
        ' panel
        clockPanel = New Panel()
        clockPanel.Size = New Size(clockWidth, clockHeight)
        clockPanel.BackColor = Color.Beige
        Gclock = clockPanel.CreateGraphics()
        ' create outer rectangle
        OuterRect = New Rectangle(xPos, yPos, clockWidth, clockHeight)
        ' create inner rectangle
        Dim w, h, x, y As Integer
        getInnerRectSizeAndLocation(w, h, x, y)
        InnerRect = New Rectangle(x, y, w, h)
        ' draw goals string inside inner rect
        InnerStringBrush = Brushes.Cyan
        Gclock.DrawString(getPercentString(), New Font("ARIAL", InnerStringFontSize, FontStyle.Bold), InnerStringBrush, InnerRect)

        ' create outer square
        OuterSquarePen = New Pen(OuterSquarePen_Color, OuterSquarePen_Width)
        Gclock.DrawArc(OuterSquarePen, OuterRect, 1.0F, 360.0F)

        ' create inner square
        InnerSquarePen = New Pen(InnerSquarePen_Color, InnerSquarePen_Width)
        Dim sweepAngle As Short = getSweepAngleFromGoalsPercent()
        Gclock.DrawArc(InnerSquarePen, OuterRect, -90.0F, sweepAngle)

    End Sub

    Private Sub getInnerRectSizeAndLocation(ByRef w As Integer, ByRef h As Integer, ByRef x As Integer, ByRef y As Integer)
        ' values for test
        w = 40
        h = 40
        x = 64
        y = 65
    End Sub

    Private Function getPercentString() As String
        Return PercentOfGoals.ToString() & "%"
    End Function

    Private Function getSweepAngleFromGoalsPercent() As Single
        ' value for test
        Return 0.0F
    End Function


End Class

1 个答案:

答案 0 :(得分:1)

您必须订阅小组Paint event并在那里执行所有绘图。 AddHandler statement用于动态订阅事件。

<HTML> <HEAD> <TITLE>Unauthorized</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <H1>Unauthorized</H1> <H2>Error 401</H2> </BODY> </HTML> 课程不会存储您绘制的内容的任何信息,因此当您的面板重新绘制时,您之前绘制的所有内容都将消失,除非您再次绘制它。这就是Graphics事件发挥作用的地方:每次重新绘制面板时都会引发它,在Paint中传递Graphics类的实例,这样你就可以将你的东西绘制到小组再次。

PaintEventArgs

正如您也可能已经看到我在Public Sub Create() ' panel clockPanel = New Panel() clockPanel.Size = New Size(clockWidth, clockHeight) clockPanel.BackColor = Color.Beige ' subscribe to the panel's paint event AddHandler clockPanel.Paint, AddressOf clockPanel_Paint End Sub Private Sub clockPanel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Dim Gclock As Graphics = e.Graphics 'Local variable only, as the Graphics object might change. ' create outer rectangle OuterRect = New Rectangle(xPos, yPos, clockWidth, clockHeight) ' create inner rectangle Dim w, h, x, y As Integer getInnerRectSizeAndLocation(w, h, x, y) InnerRect = New Rectangle(x, y, w, h) ' draw goals string inside inner rect InnerStringBrush = Brushes.Cyan Gclock.DrawString(getPercentString(), New Font("ARIAL", InnerStringFontSize, FontStyle.Bold), InnerStringBrush, InnerRect) ' create outer square OuterSquarePen = New Pen(OuterSquarePen_Color, OuterSquarePen_Width) Gclock.DrawArc(OuterSquarePen, OuterRect, 1.0F, 360.0F) ' create inner square InnerSquarePen = New Pen(InnerSquarePen_Color, InnerSquarePen_Width) Dim sweepAngle As Short = getSweepAngleFromGoalsPercent() Gclock.DrawArc(InnerSquarePen, OuterRect, -90.0F, sweepAngle) End Sub 事件中不断重新声明新的Gclock变量。这是因为用于绘制面板的Paint实例可能会发生变化,因此您不应该将其存储时间超过Graphics事件持续时间(因此我强烈建议您删除声明在你班上的顶部)。