根据框大小绘制复选框勾选?

时间:2015-05-04 00:24:12

标签: .net vb.net winforms user-controls gdi+

简介

我正在编写一个简单的复选框用户控件,它将以自定义颜色绘制背景和刻度。

要绘制勾号,我这样做:

    ''' <summary>
    ''' Draws the checkbox tick.
    ''' </summary>
    ''' <param name="g">The drawing surface.</param>
    ''' <param name="rect">The box rectangle.</param>
    ''' <param name="checkState">The checkbox state.</param>
    Private Sub DrawTick(ByVal g As Graphics,
                         ByVal rect As Rectangle,
                         ByVal checkState As CheckState)

        Select Case checkState

            Case checkState.Checked

                Dim points As PointF() =
                    New PointF() {
                        New PointF(rect.X + 3, rect.Y + 5),
                        New PointF(rect.X + 5, rect.Y + 7),
                        New PointF(rect.X + 9, rect.Y + 3),
                        New PointF(rect.X + 9, rect.Y + 4),
                        New PointF(rect.X + 5, rect.Y + 8),
                        New PointF(rect.X + 3, rect.Y + 6),
                        New PointF(rect.X + 3, rect.Y + 7),
                        New PointF(rect.X + 5, rect.Y + 9),
                        New PointF(rect.X + 9, rect.Y + 5)
                    }

                Using checkPen As New Pen(Me.TickColor, 1)
                    g.DrawLines(checkPen, points)
                End Using

            Case checkState.Indeterminate

                Using checkBrush As New SolidBrush(Me.TickColor)

                    g.FillRectangle(checkBrush, New Rectangle(rect.X + 3,
                                                              rect.Y + 3,
                                                              rect.Width - 5,
                                                              rect.Height - 5))
                End Using

            Case checkState.Unchecked
                ' Do Nothing.

        End Select

    End Sub

结果如下:

enter image description here

问题

问题是,勾选的大小/点是根据默认的复选框大小指定的,我从本文中得到了主要想法:

True Transparency support for the .NET CheckBox control

所以,让我说我增加复选框的框(上面代码的rect var的宽度和高度),然后它将绘制一个像这样的小刻度:

enter image description here

问题

如何动态修改我使用DrawTick方法中的Points进行的算法来修复其他框尺寸的刻度尺寸?

1 个答案:

答案 0 :(得分:2)

您需要一个比例因子来确定每个“x”和“y”坐标增加多少。这是一个例子(假设默认复选框宽度为12像素,这只是猜测):

    Dim scaleFactor As Single = CType(rect.Width, Single) / 12.0
    Dim points As PointF() =
        New PointF() {
            New PointF(rect.X + scaleFactor * 3, rect.Y + scaleFactor * 5),
            New PointF(rect.X + scaleFactor * 5, rect.Y + scaleFactor * 7),
            New PointF(rect.X + scaleFactor * 9, rect.Y + scaleFactor * 3),
            New PointF(rect.X + scaleFactor * 9, rect.Y + scaleFactor * 4),
            New PointF(rect.X + scaleFactor * 5, rect.Y + scaleFactor * 8),
            New PointF(rect.X + scaleFactor * 3, rect.Y + scaleFactor * 6),
            New PointF(rect.X + scaleFactor * 3, rect.Y + scaleFactor * 7),
            New PointF(rect.X + scaleFactor * 5, rect.Y + scaleFactor * 9),
            New PointF(rect.X + scaleFactor * 9, rect.Y + scaleFactor * 5)
        }