【问题标题】:Brushes to Colors画笔颜色
【发布时间】:2015-02-26 11:44:54
【问题描述】:

嘿,我正在做一个 VisualBasic Windows 窗体控件项目,但遇到了一个小问题:

这是我的 OnPaint 方法:

 Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        Dim g As Graphics
        g = e.Graphics
        Dim ancho As Integer = Me.Width / 2
        Dim alto As Integer = Me.Height / 2
        _posiciox = 5
        Percentatge(_maxim,_minim)
        For Index As Double = 0.1 To 10.0
            If Index <= _percent Then
                If Index >= 9 Then
                    g.FillRectangle(_color4, _posiciox, alto - 10, 40, 20)
                ElseIf Index >= 8 Then
                    g.FillRectangle(_color3, _posiciox, alto - 10, 40, 20)
                ElseIf Index >= 6 Then

                    g.FillRectangle(_color2, _posiciox, alto - 10, 40, 20)
                Else
                    g.FillRectangle(_color1, _posiciox, alto - 10, 40, 20)
                End If
            End If
            g.DrawRectangle(Pens.Black, _posiciox, alto - 10, 40, 20)
            _posiciox = _posiciox + 45
        Next
    End Sub

填充颜色的地方是 Brushes.Color 参数。我也想让用户选择这个颜色。

我尝试过这样的公共财产:

Public Property ColorBaix() As Color
        Get
            Return Color.Coral
        End Get
        Set(ByVal value As Color)
        End Set
    End Property

但我无法将 Brushes.Color 转换为 Color.Color:

我找到了一些示例Colors to Brush,但由于过载问题,我无法在 OnPaint 上使用“新参数”。

  • 这是唯一的方法还是可能有其他解决方案?

已解决:

我调整了我的项目:

<Description("Color Primari")>
    Public Property ColorBaix() As Color
        Get
            Return color1
        End Get
        Set(ByVal value As Color)
            color1 = value
            _color1 = New SolidBrush(value)
            Invalidate()
        End Set
    End Property

【问题讨论】:

    标签: vb.net visual-studio-2010 vba


    【解决方案1】:

    你有点找错树了。

    您不想将画笔变成颜色,而是想将现有画笔的颜色属性设置为新值。

    所以当你第一次制作画笔时,你做了这样的事情:

            Dim mybrush As New SolidBrush(Color.Aqua)
    

    然后你要设置颜色

        mybrush.Color = Color.Azure
    

    如果你想获得画笔的颜色,那么你可以这样做:

           Dim myColour As New Color
    
        myColour = mybrush.Color
    

    【讨论】:

    • 感谢您对我的帮助很大,我根据您发布的内容调整了我的项目。
    猜你喜欢
    • 2011-02-23
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多