2016-08-24 5 views
0

winformsのGraphicsクラスで多くの経験がありません。 私はそれをスケッチする段階にあるだけです(これも私が追加したコードです)。 私の問題は、パネルを作成しようとしていることです:clockPanelいくつかのグラフィックスがありますが、例外はスローされませんが、パネルにはグラフィックが表示されません。例を探すために試してみましたが、私のコードで見逃したミスや何かを見つけることができません。おそらく、グラフィックを経験したあなたのための簡単なものです。 お時間をいただきありがとうございます。クラスインスタンスからグラフィックスでコントロールオブジェクトを取得する

VBコード:GoalsClockクラスのインスタンスを介して他のパンネル( 'secondaryPannel')を 'clockpanel' パネルを追加

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 
+2

Gclock変数を削除します。グラフィックオブジェクトは決して保管しないでください。それは寿命が限られていることを意味します。決してCreateGraphicsメソッドを使用しないでください。これらのグラフィックスは一時的なものであり、フォームなどを最小限に抑えれば簡単に消去されます。 – LarsTech

+0

@LarsTech:まず最初に指摘したことがあります。 :) –

+1

CreateGraphicsの三角形で失われた別の船員。あなたが最初からWinformsのプログラミングを学んでいる限り、WPFを最初に検討してください。それはあなたの考え方とより互換性があります。 –

答えて

1

パネルのPaint eventに登録して、すべての図面を実行する必要があります。 AddHandler statementは、イベントを動的に購読するために使用されます。

Graphicsクラスには描画する情報が格納されないため、パネルを再描画すると、再度描画しない限り、以前に描画したものはすべて消えてしまいます。ここではPaintイベントが発生します。パネルが再描画されるたびに発生し、PaintEventArgsGraphicsクラスのインスタンスを渡します。これにより、パネルにアイテムを再度描画できます。あなたも、私は常にPaintイベントで新しいGclock変数を再宣言しています見てきたかもしれませんが

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 

。これは、パネルを描画するために使用されるGraphicsインスタンスが変更される可能性があるためです。Paintイベントが持続する時間より長く保存しないでください(クラスの先頭にある宣言を削除することを強くお勧めします)。

+0

ありがとうございます。あなたは書いたものについて何も情報を保存しないので、パネルを再描画したときに、前回描画したものはすべて、再び描画しない限り消えてしまいます。だから、私はそれが作成され、その動作後にパラメータとしてCreate()メソッドにclockPannelオブジェクトを渡すことを試みた。今私は私がグラフィックス... tnxに既存のオブジェクトを渡すペイントイベントVSを使用する必要があるかどうかを判断する必要があります。 – jonathana

+0

@jonathana:あなたは本当にあなたがコントロールを描画するはずの方法であるので、Paintイベントを使用する必要があります。 LarsTechがすでに指摘しているように、 'CreateGraphics()'は使わないでください。 –

+0

私はイベントを使用します。ありがとうございました!! – jonathana

関連する問題