how to change the check image on a checkbox

后端 未结 4 1851
耶瑟儿~
耶瑟儿~ 2020-12-06 17:59

it has text, an image, and then the checkbox,

I want to use a better image for the check, but cannot find a way to change the checked and unchecked images



        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-06 18:17

    If you are looking for how to do this in Winforms, the simple answer is to create a new checkbox class that derives from CheckBox, then override the OnPaint method.

    Here is an example on how to create custom looking checkboxes by overriding the OnPaint method:

    public class CustomCheckBox : CheckBox
    {
        public CustomCheckBox()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        }
    
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            if (this.Checked)
            {
                pevent.Graphics.FillRectangle(new SolidBrush(Color.Blue), new Rectangle(0, 0, 16, 16));
            }
            else
            {
                pevent.Graphics.FillRectangle(new SolidBrush(Color.Red), new Rectangle(0, 0, 16, 16));
            }
        }
    }
    

    It's very simple, but it gives you the basic idea.

提交回复
热议问题