none
I need the CheckBox portion of the CheckBox control to be smaller, say 2/3 the size - How can I do that

    Question

  • Hello all,

     

    I need a smaller CheckBox glyph, say 2/3 the size of the current one drawn with a CheckBox. It is independent of the font. How would I go about doing something like this? I assume that I would have to subclass the CheckBox and handle the OnPaint event somehow, but I am not sure how to go about getting started. If someone could point me or maybe post some sample code to get me started, it would be much appreciated!

    Thursday, December 20, 2007 12:08 AM

Answers

  • Here is the work-around I used.

     

    I could find no way to alter the size of the glyph returned by CheckBoxRenderer.DrawCheckBox, it always returned a square glyph 13x13. The work-around is to draw to a bitmap in memory and then use the DrawCheckBox Image overload in conjunction with the OnMouseUp event to determine if the image was clicked. Note that when actually drawing the CheckBox with e.Graphics the actual glyph drawn is ignored by setting the glyph point to negative values, the text rectangle to Rectangle.Empty

     

    Code Block

     

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

     

        ' Match glyph size returned by CheckBoxRenderer.DrawCheckBox.

        Dim buffer As New BitMap(13, 13)

        ' The image rectangle will be scaled from the buffer bit map.

        Dim imageRectangle As New Rectangle(0, 0, 9, 9(

        Dim g As Graphics = Graphics.FromImage(buffer)

     

        ' Draw CheckBox glyph on the buffer.

        CheckBoxRenderer.DrawCheckBox(g, New Point(0, 0), CheckBoxState.CheckedNormal)

     

        g.Dispose()

     

        ' Now draw buffer as an image on the form in place of the check box glpyh which was moved off the screen.

        CheckBoxRenderer.DrawCheckBox(e.Graphics, New Point(-100, -100), Rectangle.Empty, Nothing, Me.Font, buffer, imageRectangle, False, CheckBoxState.CheckedNormal)

     

    End Sub 'OnPaint

     

    If VisualStyles is not enabled, the system will revert to using ControlPaint in the background, if I am reading the docs correctly.

     

     

    Tuesday, December 25, 2007 5:47 AM
  •  

    Hi j2associates,

    As far as I know, if you want to change the size of the CheckBox glyph, you need to custom draw all the stall of your CheckBox. You can create a custom CheckBox and override the OnPaint method. Do not call Onpaint method of the base class; you should paint the staff yourself. Check the following thread for a sample.

    ·         http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1645767&SiteID=1

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Tuesday, December 25, 2007 7:29 AM

All replies

  • Hello all,

    I need to draw a check box at around 2/3 the size of the standard windows forms checkbox glyph. Changing the font has no impact on the check box itself. Can I use GDI some how to draw the check box glyph at a different size? Thanks in advance for any ideas and/or suggestions!
    Sunday, December 23, 2007 5:24 PM
  • Here is the work-around I used.

     

    I could find no way to alter the size of the glyph returned by CheckBoxRenderer.DrawCheckBox, it always returned a square glyph 13x13. The work-around is to draw to a bitmap in memory and then use the DrawCheckBox Image overload in conjunction with the OnMouseUp event to determine if the image was clicked. Note that when actually drawing the CheckBox with e.Graphics the actual glyph drawn is ignored by setting the glyph point to negative values, the text rectangle to Rectangle.Empty

     

    Code Block

     

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

     

        ' Match glyph size returned by CheckBoxRenderer.DrawCheckBox.

        Dim buffer As New BitMap(13, 13)

        ' The image rectangle will be scaled from the buffer bit map.

        Dim imageRectangle As New Rectangle(0, 0, 9, 9(

        Dim g As Graphics = Graphics.FromImage(buffer)

     

        ' Draw CheckBox glyph on the buffer.

        CheckBoxRenderer.DrawCheckBox(g, New Point(0, 0), CheckBoxState.CheckedNormal)

     

        g.Dispose()

     

        ' Now draw buffer as an image on the form in place of the check box glpyh which was moved off the screen.

        CheckBoxRenderer.DrawCheckBox(e.Graphics, New Point(-100, -100), Rectangle.Empty, Nothing, Me.Font, buffer, imageRectangle, False, CheckBoxState.CheckedNormal)

     

    End Sub 'OnPaint

     

    If VisualStyles is not enabled, the system will revert to using ControlPaint in the background, if I am reading the docs correctly.

     

     

    Tuesday, December 25, 2007 5:47 AM
  •  

    Hi j2associates,

    As far as I know, if you want to change the size of the CheckBox glyph, you need to custom draw all the stall of your CheckBox. You can create a custom CheckBox and override the OnPaint method. Do not call Onpaint method of the base class; you should paint the staff yourself. Check the following thread for a sample.

    ·         http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1645767&SiteID=1

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Tuesday, December 25, 2007 7:29 AM