클래스 인스턴스에서 Graphics로 제어 개체 가져 오기

홍옥

나는 winforms의 Graphics 클래스에 대한 경험이 많지 않습니다. 나는 그것의 스케치 단계 (또한 내가 추가 한 코드)에 있습니다. 내 문제는 내가 패널을 만들려고한다는 것 clockPanel입니다. 일부 그래픽을 사용하면 예외가 발생하지 않지만 패널 (UI에서 볼 수 있듯이)에는 그래픽이 없습니다. 예제를 찾으려고 노력했지만 실수 나 코드에서 놓친 것을 찾을 수 없습니다. 그래픽 경험이있는 분들에게는 쉬운 방법 일 것입니다. 시간 내 주셔서 감사합니다.

VB CODE : 인스턴스를 통해 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
비주얼 빈센트

패널의 Paint이벤트에 가입하고 거기에서 모든 그리기를 수행해야합니다. AddHandler명령문 은 이벤트를 동적으로 구독하는 데 사용됩니다.

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패널을 그리는 데 사용되는 인스턴스가 변경 될 수 있으므로 Paint이벤트가 지속되는 시간보다 더 오래 저장하지 않아야하기 때문입니다 (따라서 클래스 상단에서 선언을 제거하는 것이 좋습니다).

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

.NetCore에서 동적으로 클래스 인스턴스 가져 오기

분류에서Dev

다른 프로젝트에서 클래스 인스턴스 가져 오기

분류에서Dev

개체의 인스턴스에서 PHP 파일 가져 오기

분류에서Dev

Java 개체에서 WEKA 인스턴스 가져 오기

분류에서Dev

제네릭 유형 인스턴스를 가져 오지 않는 제네릭 메서드 내에서 구체적인 클래스 속성 읽기

분류에서Dev

주어진 클래스 경로에서 파일 가져 오기

분류에서Dev

목록 YII PHP에서 개체의 인덱스로 개체 가져 오기

분류에서Dev

C #의 다른 클래스에서 개체 ID 가져 오기

분류에서Dev

C ++ 클래스에서 빌드 된 개체 수 가져 오기

분류에서Dev

클래스 개체 유형에서 DbSet 이름 가져 오기

분류에서Dev

개체에서 하위 클래스 콘텐츠 가져 오기

분류에서Dev

String className에서 throwable 클래스 개체 가져 오기

분류에서Dev

Hibernate Inheritance-슈퍼 클래스 인스턴스 가져 오기 및 서브 클래스로 캐스팅

분류에서Dev

React Native Tabbar iOS 바인딩 문제 [컴포넌트 클래스 예상, 개체 개체 가져 오기]

분류에서Dev

주입 된 인스턴스 CDI에서 정확한 개체 가져 오기

분류에서Dev

정적 메서드에서 실제 제네릭 유형의 클래스 개체 가져 오기

분류에서Dev

정적 메서드에서 실제 제네릭 유형의 클래스 개체 가져 오기

분류에서Dev

PHP 배열에서 클래스의 새 인스턴스 가져 오기 (POST를 통해 제출)

분류에서Dev

WinForms 끌어서 놓기 (개체가 인스턴스로 설정되지 않음)

분류에서Dev

PHP 클래스 디자인-여러 개체 가져 오기

분류에서Dev

사용자 입력을 기반으로 Python의 클래스 개체에서 값 가져 오기

분류에서Dev

서비스 클래스로 Typescript 가져 오기 클래스

분류에서Dev

C ++ : 친구 클래스로 개체 인스턴스화 제한

분류에서Dev

기존 클래스의 새 인스턴스로 데이터 가져 오기

분류에서Dev

arcpy : 객체로 피쳐 클래스 가져 오기

분류에서Dev

제네릭을 사용하여 클래스의 인스턴스 가져 오기

분류에서Dev

NullReferenceException을 반환하는 appsettings.json에서 값 가져 오기 : 개체 참조가 개체의 인스턴스로 설정되지 않았습니다. .net 코어 3

분류에서Dev

클래스별로 객체 (배열)에서 요소 가져 오기

분류에서Dev

놓기 대상 개체의 인스턴스 가져 오기

Related 관련 기사

  1. 1

    .NetCore에서 동적으로 클래스 인스턴스 가져 오기

  2. 2

    다른 프로젝트에서 클래스 인스턴스 가져 오기

  3. 3

    개체의 인스턴스에서 PHP 파일 가져 오기

  4. 4

    Java 개체에서 WEKA 인스턴스 가져 오기

  5. 5

    제네릭 유형 인스턴스를 가져 오지 않는 제네릭 메서드 내에서 구체적인 클래스 속성 읽기

  6. 6

    주어진 클래스 경로에서 파일 가져 오기

  7. 7

    목록 YII PHP에서 개체의 인덱스로 개체 가져 오기

  8. 8

    C #의 다른 클래스에서 개체 ID 가져 오기

  9. 9

    C ++ 클래스에서 빌드 된 개체 수 가져 오기

  10. 10

    클래스 개체 유형에서 DbSet 이름 가져 오기

  11. 11

    개체에서 하위 클래스 콘텐츠 가져 오기

  12. 12

    String className에서 throwable 클래스 개체 가져 오기

  13. 13

    Hibernate Inheritance-슈퍼 클래스 인스턴스 가져 오기 및 서브 클래스로 캐스팅

  14. 14

    React Native Tabbar iOS 바인딩 문제 [컴포넌트 클래스 예상, 개체 개체 가져 오기]

  15. 15

    주입 된 인스턴스 CDI에서 정확한 개체 가져 오기

  16. 16

    정적 메서드에서 실제 제네릭 유형의 클래스 개체 가져 오기

  17. 17

    정적 메서드에서 실제 제네릭 유형의 클래스 개체 가져 오기

  18. 18

    PHP 배열에서 클래스의 새 인스턴스 가져 오기 (POST를 통해 제출)

  19. 19

    WinForms 끌어서 놓기 (개체가 인스턴스로 설정되지 않음)

  20. 20

    PHP 클래스 디자인-여러 개체 가져 오기

  21. 21

    사용자 입력을 기반으로 Python의 클래스 개체에서 값 가져 오기

  22. 22

    서비스 클래스로 Typescript 가져 오기 클래스

  23. 23

    C ++ : 친구 클래스로 개체 인스턴스화 제한

  24. 24

    기존 클래스의 새 인스턴스로 데이터 가져 오기

  25. 25

    arcpy : 객체로 피쳐 클래스 가져 오기

  26. 26

    제네릭을 사용하여 클래스의 인스턴스 가져 오기

  27. 27

    NullReferenceException을 반환하는 appsettings.json에서 값 가져 오기 : 개체 참조가 개체의 인스턴스로 설정되지 않았습니다. .net 코어 3

  28. 28

    클래스별로 객체 (배열)에서 요소 가져 오기

  29. 29

    놓기 대상 개체의 인스턴스 가져 오기

뜨겁다태그

보관