Vb.net 图形用户界面不';我不能相应地行动

Vb.net 图形用户界面不';我不能相应地行动,vb.net,winforms,Vb.net,Winforms,我试图以图形的形式显示俯仰、横摇和偏航指示,所以我使用VisualBasic中的用户控件构建了一个GUI。这是我的GUI代码 Private g As Graphics Private _roll_angle As Double Public Property roll_angle() As Double Get Return _roll_angle End Get Set(ByVal value As Double) _roll_ang

我试图以图形的形式显示俯仰、横摇和偏航指示,所以我使用VisualBasic中的用户控件构建了一个GUI。这是我的GUI代码

Private g As Graphics

Private _roll_angle As Double
Public Property roll_angle() As Double
    Get
        Return _roll_angle
    End Get
    Set(ByVal value As Double)
        _roll_angle = value
        Invalidate()
    End Set
End Property
Private _pitch_angle As Double
Public Property pitch_angle() As Double
    Get
        Return _pitch_angle
    End Get
    Set(ByVal value As Double)
        _pitch_angle = value
        Invalidate()
    End Set
End Property


Private Sub ArtificialHorizon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Function pitch_to_pix(ByVal pitch As Double) As Integer
    Return pitch / 35.0 * Me.Height / 2
    'Return pitch / 45.0 * Me.Height / 2
End Function

Private Sub ArtificialHorizon_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    g = e.Graphics
    g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality

    g.Clear(Me.BackColor)
    Dim sin As Double = Math.Sin(roll_angle / 180 * 3.14)

    g.ResetTransform()
    '        g.FillRegion(Brushes.White, New Region(New Rectangle(0, 0, Me.Width, Me.Height)))

    ' rounded rectangle
    Dim path As New Drawing2D.GraphicsPath()
    Dim r As Single = 50
    path.AddArc(0, 0, r, r, 180, 90)
    path.AddArc(Me.Width - r, 0, r, r, 270, 90)
    path.AddArc(Me.Width - r, Me.Height - r, r, r, 0, 90)
    path.AddArc(0, Me.Height - r, r, r, 90, 90)
    'path.AddEllipse(0, 0, Me.Width, Me.Height)
    path.CloseFigure()
    g.SetClip(path)

    g.TranslateTransform(Me.Width / 2, Me.Height / 2)

    g.RotateTransform(roll_angle)
    g.TranslateTransform(0, pitch_to_pix(pitch_angle))

    ' chocolate
    Dim b As New System.Drawing.Drawing2D.LinearGradientBrush(New RectangleF(-Me.Width, 0, Me.Height * 2, Me.Width * 2), Color.FromArgb(255, 219, 140, 21), Color.Brown, Drawing2D.LinearGradientMode.Vertical)
    g.FillRectangle(b, New RectangleF(-Me.Width * 2, +1, Me.Height * 4, Me.Width * 4))

    g.RotateTransform(180)

    ' color.aqua
    b = New System.Drawing.Drawing2D.LinearGradientBrush(New RectangleF(-Me.Width, -1, Me.Height * 2, Me.Width * 2), Color.FromArgb(255, 28, 134, 186), Color.DarkBlue, Drawing2D.LinearGradientMode.Vertical)
    g.FillRectangle(b, New RectangleF(-Me.Width * 2, 0, Me.Height * 4, Me.Width * 4))




    g.ResetTransform()
    Dim w2 As Single = Me.Width / 2
    Dim s As Single = Me.Width / 38
    g.TranslateTransform(Me.Width / 2, Me.Height / 2)
    g.RotateTransform(45)
    g.TranslateTransform(-w2 + s, 0)
    g.DrawLine(New Pen(Color.White, 2), 0, 0, s * 2, 0)
    g.TranslateTransform(+w2 - s, 0)
    g.RotateTransform(15)
    g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 2, 0)
    g.RotateTransform(15)
    g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 2, 0)
    g.RotateTransform(15)
    g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 3, 0)
    'g.DrawString("0°", New System.Drawing.Font("sans-serif", 9), Brushes.White, -w2 + 40, -4)
    g.RotateTransform(15)
    g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 2, 0)
    g.RotateTransform(15)
    g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 2, 0)
    g.RotateTransform(15)
    g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 3, 0)
    'g.DrawString("+45°", New System.Drawing.Font("sans-serif", 9), Brushes.White, -w2 + 40, -4)


    g.ResetTransform()

    Dim length As Single = Me.Width / 4
    Dim notch As Single = Me.Width / 30
    g.TranslateTransform(Me.Width / 2, Me.Height / 2)
    g.DrawLine(New Pen(Color.White, 3), -length + notch * 2, 0, -notch, 0)
    g.DrawLine(New Pen(Color.White, 3), notch, 0, length - notch * 2, 0)
    g.DrawArc(New Pen(Color.White, 3), -notch, -notch, notch * 2, notch * 2, 180, -180)

    g.ResetTransform()

    ' driehoekje
    Dim ww As Single = Me.Width / 38
    g.TranslateTransform(Me.Width / 2, Me.Height / 2)
    g.RotateTransform(-90 + roll_angle)
    path = New Drawing2D.GraphicsPath()
    path.AddLine(w2 - ww * 3, 0, w2 - ww * 4, ww)
    path.AddLine(w2 - ww * 4, -ww, w2 - ww * 4, ww)
    path.AddLine(w2 - ww * 4, -ww, w2 - ww * 3, 0)
    g.FillRegion(Brushes.White, New Region(path))
    g.DrawLine(New Pen(Color.White, 1), w2 - ww * 3, 0, w2 - ww * 4, ww)
    g.DrawLine(New Pen(Color.White, 1), w2 - ww * 4, -ww, w2 - ww * 4, ww)
    g.DrawLine(New Pen(Color.White, 1), w2 - ww * 4, -ww, w2 - ww * 3, 0)



    g.ResetTransform()
    g.ResetClip()
    path = New Drawing2D.GraphicsPath()
    path.AddPie(New Rectangle(ww * 3, ww * 3, Me.Width - ww * 6, Me.Height - ww * 6), 0, 360)
    g.SetClip(path)

    g.TranslateTransform(Me.Width / 2, Me.Height / 2)
    g.RotateTransform(roll_angle)
    g.TranslateTransform(0, pitch_to_pix(pitch_angle))
    For i As Integer = -80 To 80 Step 10
        drawpitchline(g, i)
    Next i

End Sub

Private Sub drawpitchline(ByVal g As Graphics, ByVal pitch As Double)
    Dim w As Single = Me.Width / 8
    g.DrawLine(Pens.White, -w, pitch_to_pix(-pitch + 5), w, pitch_to_pix(-pitch + 5))
    g.DrawLine(Pens.White, -w * 5 / 3, pitch_to_pix(-pitch), w * 5 / 3, pitch_to_pix(-pitch))
    g.DrawString(pitch, Me.Font, Brushes.White, -w * 75 / 30, pitch_to_pix(-pitch) - 5)
    g.DrawString(pitch, Me.Font, Brushes.White, w * 2, pitch_to_pix(-pitch) - 5)
End Sub

Private Sub drawrollline(ByVal g As Graphics, ByVal a As Single)
    Dim w2 As Single = Me.Width / 2


    g.RotateTransform(a + 90)
    g.TranslateTransform(-w2 + 10, 0)
    g.DrawLine(Pens.White, 0, 0, 20, 0)
    g.TranslateTransform(10, 5)
    g.RotateTransform(-a - 90)
    g.DrawString("" & (a) & "°", New System.Drawing.Font("sans-serif", 9), Brushes.White, 0, 0)
    g.RotateTransform(+90 + a)
    g.TranslateTransform(-10, -5)
    g.TranslateTransform(+w2 - 10, 0)
    g.RotateTransform(-a - 90)
End Sub
然后我添加了一个按钮来提供一些测试读数。代码如下

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    AH.roll_angle = 45
    AH.pitch_angle = 10
End Sub
单击按钮时,GUI应根据输入旋转。然而,它仍然保持静止(顺便说一句,代码能够调试,并且图形确实显示出来)。我相信这是因为我在画作中没有提到AH变量。有人能给我一个指导吗


提前谢谢

我可能有一些东西可以使用,我创建了这个来显示抛物面天线的方位航向

  • 标题是用简单的钢笔写的
  • 叠加在圆形图像的顶部
  • 笔将根据移动的程度旋转
  • 具有3主要功能

  • 画天线()-->画笔/线
  • FindPointOnCircle()-->查找笔的端点(起点始终为圆心)
  • RotateAntenna()-->这将旋转画笔并相应地更新图像
  • 从函数中调用RotateAntenna(例如TextChanged),并将更改角度传递给RotateAntenna
  • 一个关键的事情是每当角度改变时对图像进行刷新,这将提供旋转效果
  • 我建议在不同的线程上进行此操作,以避免陷入主线程,因为通常azm/elv/roll的数据采集将持续进行
  • 下至代码:

    Private Function FindPointOnCircle(ByVal originPoint As Point, ByVal radius As Double, ByVal angleDegrees As Double) As Point
            Dim x As Double = radius * Math.Cos(Math.PI * angleDegrees / 180.0) + originPoint.X
            Dim y As Double = radius * Math.Sin(Math.PI * angleDegrees / 180.0) + originPoint.Y
            Return New Point(x, y)
     End Function
    





    希望这有帮助!!。谢谢。

    我不确定什么是AH,但是如果你在按钮1中用我替换这个变量,单击它就会工作(至少,整个旋转,然后它开始超出帧)。AH代表人造地平线,我将其命名为变量。是的,它成功了。但在我停止调试后,按钮消失了,我尝试再次执行相同的操作,这次没有显示任何按钮。我可以知道为什么吗?您的代码有点复杂,绘制事件可能会变得有点棘手。但通常涉及的问题与工件或表单冻结有关。通常情况下,如果它工作一次,它应该工作更多(和它的工作与我很好)。我找到按钮的地方没有发现任何问题,但请记住,您正在更改主窗体的整个布局,因此这就是使按钮消失的原因。设置断点并跟踪这些值,以了解它在哪里开始出错(在我的例子中,是在第一次完整旋转之后)。PS:请记住,我测试了与主窗体相关的代码(=在其初始类中这样编写);所以我建议你给我换个啊,问什么是啊,;我猜这是一个控件,就像一个面板。
    Private Sub DrawAntenna(ByVal originPoint As Point, ByVal endPoint As Point, ByVal g As Graphics, Optional ByVal aPen As Pen = Nothing)
            If aPen Is Nothing Then
                Using BluePen = New Pen(Color.Blue)
                    BluePen.Width = 2
                    g.DrawLine(BluePen, originPoint.X, originPoint.Y, endPoint.X, endPoint.Y)
                End Using
            Else
                g.DrawLine(aPen, originPoint.X, originPoint.Y, endPoint.X, endPoint.Y)
            End If
        End Sub
    
    Private Sub RotateAntenna(ByVal ang As Double, ByVal g As Graphics, ByVal typeOfDisplay As Integer)
    
            ' Radius of 95% of half the width of the panel
            Dim radius As Double = (Me.picDestTop.Width / 2) * 0.95
    
            ' Origin half of width and height of panel
            Dim origin As New Point(Me.picDestTop.Width / 2, Me.picDestTop.Height / 2)
            Select Case typeOfDisplay
    
                Case displayAntenna.DisplayTop
                    'rotate start at 270deg, pointing up
    
                    Dim antennaDegrees As Double = ang + 170
                    'find point
                    Dim secondsPoint As Point = FindPointOnCircle(origin, radius, antennaDegrees)
                    Using p As New Pen(Color.Red)
                        p.Width = 5
                        DrawAntenna(origin, secondsPoint, g, p)
                    End Using
    
    
                Case displayAntenna.DisplaySide
                   'display elevation
    
                Case displayAntenna.DisplayRoll
                   'display pitch
    
    
            End Select
    
            g.Dispose()
    
        End Sub
    
    'azimuth
        Private Sub lblEncCurrent_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblEncCurrent.TextChanged
            Dim iint As Double
            Me.picDestTop.Refresh()
            iint = CDbl(lblEncCurrent.Text)
            If iint >= 360 Then
                iint = iint - 360
            End If
            RotateAntenna(iint, Me.picDestTop.CreateGraphics, displayAntenna.DisplayTop)
        End Sub