C# 如何更改复选框上的支票图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15813869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:02:07  来源:igfitidea点击:

how to change the check image on a checkbox

c#.netwinformscheckbox

提问by f1wade

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?

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

采纳答案by Icemanind

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 OnPaintmethod.

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

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

这是一个关于如何通过覆盖该OnPaint方法来创建自定义外观复选框的示例:

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.

这很简单,但它为您提供了基本概念。

回答by KF2

A simple one :

一个简单的:

overrides check box OnPaint(PaintEventArgs e)as below:

覆盖复选框OnPaint(PaintEventArgs e)如下:

Graphics g = e.Graphics;

        base.OnPaint(e);
        //// Fill the background
        //SetControlSizes();

        // Paint the outer rounded rectangle
        g.SmoothingMode = SmoothingMode.AntiAlias;
        using (GraphicsPath outerPath = GeneralUtilities.RoundedRectangle(mLabelRect, 1, 0))
        {
            using (LinearGradientBrush outerBrush = new LinearGradientBrush(mLabelRect,
                   mGradientTop, mGradientBottom, LinearGradientMode.Vertical))
            {
                g.FillPath(outerBrush, outerPath);
            }
            using (Pen outlinePen = new Pen(mGradientTop, mRectOutlineWidth))
            {
                outlinePen.Alignment = PenAlignment.Inset;
                g.DrawPath(outlinePen, outerPath);
            }
        }

        //// Paint the gel highlight
        using (GraphicsPath innerPath = GeneralUtilities.RoundedRectangle(mHighlightRect, mRectCornerRadius, mHighlightRectOffset))
        {
            using (LinearGradientBrush innerBrush = new LinearGradientBrush(mHighlightRect,
                   Color.FromArgb(mHighlightAlphaTop, Color.White),
                   Color.FromArgb(mHighlightAlphaBottom, Color.White), LinearGradientMode.Vertical))
            {
                g.FillPath(innerBrush, innerPath);
            }
        }
        // Paint the text
        TextRenderer.DrawText(g, Text, Font, mLabelRect, Color.White, Color.Transparent,
        TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);

But if you want to have a good one you have to use wpf CheckBox ControlTemplate Example

但是如果你想有一个好的,你必须使用wpf CheckBox ControlTemplate Example

回答by f1wade

I have got around this a different way, I use the background image and center it, then change the main image whenever checked is changed. this appears like I want it to.

我以不同的方式解决了这个问题,我使用背景图像并将其居中,然后在更改选中时更改主图像。这似乎是我想要的。

There is a problem with this, the background image if an inappropriate size with underlap the check image, and so it would not look right.

这有一个问题,背景图像如果尺寸不合适,会与支票图像重叠,因此看起来不正确。

the correct solution is as icemanind describes.

正确的解决方案是如 icemanind 所描述的。

回答by Korli

For anyone who'd prefer not to override OnPaint, there's an alternative solution:

对于不想覆盖 OnPaint 的任何人,有一个替代解决方案:

  1. Add an ImageListcontrol and fill it with images to reflect checked/unchecked states.
  2. Set your Checkboxcontrol's Appearanceproperty to Button(to get rid of standard CheckBox icon)
  3. Set its' FlatStyleproperty to Flat(so that the control doesn't really look like a button).
    Note: You may want to check its' FlatAppearancegroup of properties too. Namely CheckedBackColor, MouseDownBackColor, MouseOverBackColor, i.e. set them all to Controlvalue.
  4. Set Checkboxcontrol's ImageListproperty to the name of your ImageListcontrol.
  5. Set Checkboxcontrol's Imageindexand ImageAlignproperties to reflect its' current state.
  6. Last and most importantly set Checkboxcontrol's TextImageRelationproperty (this value won't let the text and image overlap unless you want them to). I.e. ImageBeforetextvalue represents common CheckBox icon location.
  1. 添加一个ImageList控件并用图像填充它以反映选中/未选中状态。
  2. 将您的Checkbox控件的Appearance属性设置为Button(以摆脱标准的 CheckBox 图标)
  3. 将它的 'FlatStyle属性设置为Flat(以便控件看起来不像按钮)。
    注意:您可能还想检查它FlatAppearance的属性组。即CheckedBackColor, MouseDownBackColor, MouseOverBackColor, 即将它们全部设置为Control值。
  4. Checkbox控件的ImageList属性设置为控件的名称ImageList
  5. 设置Checkbox控件ImageindexImageAlign属性以反映其当前状态。
  6. 最后也是最重要的设置Checkbox控件的TextImageRelation属性(该值不会让文本和图像重叠,除非您希望它们重叠)。即ImageBeforetext值表示常见的 CheckBox 图标位置。

Now the only thing left to do is to change the image when the state is changed, smth like this:

现在唯一要做的就是在状态改变时改变图像,像这样:

    private void chkMyCheckBoxWithAnImage_CheckedChanged(object sender, EventArgs e)
    {
        if (chkMyCheckBoxWithAnImage.Checked)
            chkMyCheckBoxWithAnImage.ImageIndex = 1;
        else
            chkMyCheckBoxWithAnImage.ImageIndex = 0;
    }