如何更改复选框上的支票图像 [英] how to change the check image on a checkbox

查看:22
本文介绍了如何更改复选框上的支票图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它有文本、图像,然后是复选框,

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

this.checkBox1.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
this.checkBox1.Checked = true;
this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBox1.Image = global::ClientExam.Properties.Resources.action32;
this.checkBox1.Location = new System.Drawing.Point(145, 140);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(273, 127);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "checkBox1";
this.checkBox1.TextImageRelation = System.Windows.Forms.TextImageRelation.TextBeforeImage;
this.checkBox1.UseVisualStyleBackColor = true;

有人知道一种不需要我编写自己的控件的吗?

anybody know of one that doesn't require me to write my own control?

推荐答案

如果您正在寻找如何在 Winforms 中执行此操作,简单的答案是创建一个派生自 CheckBox 的新复选框类,然后覆盖 OnPaint 方法.

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.

以下是如何通过覆盖 OnPaint 方法来创建自定义外观复选框的示例:

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.

这篇关于如何更改复选框上的支票图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆