3路复选框(切换)

时间:2015-04-24 22:41:03

标签: vb.net winforms

我有一个自定义'切换'(使用ButterscotchTheme),我正在尝试添加第三个选项。

目前的情况如下:(由于是新的,因此无法发布图片,但这里是图片的链接)

enter image description here

enter image description here

我想要第三种选择。我检查了主题代码,它是由Checkbox规则完成的。我知道Tricheckbox是一个东西,我只是不确定如何让它工作。我做了一些研究,在这种情况下无法弄明白。

以下是所有主题切换代码:

Public Class ButterscotchToggle : Inherits Control

Private _check As Boolean
Public Property Checked As Boolean
    Get
        Return _check
    End Get
    Set(ByVal value As Boolean)
        _check = value
        Invalidate()
    End Set
End Property

Sub New()
    MyBase.New()
    SetStyle(ControlStyles.UserPaint Or ControlStyles.ResizeRedraw Or ControlStyles.SupportsTransparentBackColor, True)
    DoubleBuffered = True
    BackColor = Color.Transparent
    Size = New Size(80, 25)
End Sub

Protected Overrides Sub OnClick(ByVal e As EventArgs)
    Checked = Not Checked
    MyBase.OnClick(e)
End Sub

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    Dim b As Bitmap = New Bitmap(Width, Height)
    Dim g As Graphics = Graphics.FromImage(b)
    Dim outerrect As Rectangle = New Rectangle(0, 0, Width - 1, Height - 1)
    Dim maininnerrect As Rectangle = New Rectangle(7, 7, Width - 15, Height - 15)
    Dim buttonrect As New LinearGradientBrush(outerrect, Color.FromArgb(100, 90, 80), Color.FromArgb(48, 43, 39), 90S)
    MyBase.OnPaint(e)
    g.Clear(BackColor)
    g.SmoothingMode = SmoothingMode.HighQuality
    g.InterpolationMode = InterpolationMode.HighQualityBicubic
    g.FillPath(New SolidBrush(Color.FromArgb(40, 37, 33)), RoundRect(outerrect, 5))
    g.DrawPath(New Pen(Color.FromArgb(0, 0, 0)), RoundRect(outerrect, 5))
    g.FillPath(New SolidBrush(Color.FromArgb(26, 25, 21)), RoundRect(maininnerrect, 3))
    g.DrawPath(New Pen(Color.FromArgb(0, 0, 0)), RoundRect(maininnerrect, 3))
    If Checked Then
        g.FillPath(buttonrect, RoundRect(New Rectangle(3, 3, CInt((Width / 2) - 3), Height - 7), 7))
        g.DrawString("ON", New Font("Segoe UI", 10, FontStyle.Bold), New SolidBrush(Color.FromArgb(246, 180, 12)), New Rectangle(2, 2, CInt((Width / 2) - 1), Height - 5), New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
    Else
        g.FillPath(buttonrect, RoundRect(New Rectangle(CInt((Width / 2) - 3), 3, CInt((Width / 2) - 3), Height - 7), 7))
        g.DrawString("OFF", New Font("Segoe UI", 10, FontStyle.Bold), New SolidBrush(Color.FromArgb(246, 180, 12)), New Rectangle(CInt((Width / 2) - 2), 2, CInt((Width / 2) - 1), Height - 5), New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
    End If
    e.Graphics.DrawImage(b, New Point(0, 0))
    g.Dispose() : b.Dispose()
End Sub
End Class

任何帮助将不胜感激!

Butterscotch Theme GDI+

下载的主题

1 个答案:

答案 0 :(得分:0)

您必须将该布尔属性更改为整数(甚至更好,创建枚举):

Private _check As Integer

Public Property Checked As Integer
  Get
    Return _check
  End Get
  Set(ByVal value As Integer)
    _check = value
    Invalidate()
  End Set
End Property

更改OnClick行为:

Protected Overrides Sub OnClick(ByVal e As EventArgs)
  If Checked + 1 > 2 Then
    Checked = 0
  Else
    Checked += 1
  End If
  MyBase.OnClick(e)
End Sub

然后相应地调整图纸(将第三个选项放在中间我猜):

Select Case Checked
  Case 0
    g.FillPath(buttonrect, RoundRect(New Rectangle(CInt((Width / 2) - 3), 3, CInt((Width / 2) - 3), Height - 7), 7))
    g.DrawString("OFF", New Font("Segoe UI", 10, FontStyle.Bold), New SolidBrush(Color.FromArgb(246, 180, 12)), New Rectangle(CInt((Width / 2) - 2), 2, CInt((Width / 2) - 1), Height - 5), New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
  Case 1
    g.FillPath(buttonrect, RoundRect(New Rectangle(3, 3, CInt((Width / 2) - 3), Height - 7), 7))
    g.DrawString("ON", New Font("Segoe UI", 10, FontStyle.Bold), New SolidBrush(Color.FromArgb(246, 180, 12)), New Rectangle(2, 2, CInt((Width / 2) - 1), Height - 5), New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
  Case 2
    g.FillPath(buttonrect, RoundRect(New Rectangle((Width / 2) - (Width / 4), 3, CInt((Width / 2) - 3), Height - 7), 7))
    g.DrawString("???", New Font("Segoe UI", 10, FontStyle.Bold), New SolidBrush(Color.FromArgb(246, 180, 12)), New Rectangle((Width / 2) - (Width / 4), 2, CInt((Width / 2) - 1), Height - 5), New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
End Select