dimanche 3 mai 2015

Draw checkbox tick according to the box size?

Introduction

I'm writting a simple checkbox that will draw a background and a tick in a custom color.

To draw the tick, I do this:

    ''' <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

This is the result:

enter image description here


Problem

The problem is that the size/points of the tick are specified according to the default checkbox size, so, lets say I increase the box of the checkbox (the width and height of the rect var of the code above) then it will draw a tiny tick like this:

enter image description here


Question

How I could dynamically modify the arithmetic that I do with the Points in the DrawTick method to fix the tick size for other box sizes?




Aucun commentaire:

Enregistrer un commentaire