如何更改复选框上的检查图像

时间:2021-02-03 10:04:28

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?

谁知道一个不需要我自己控制的人呢?

4 个解决方案

#1


8  

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.

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

Here is a complete tutorial on how to create custom looking checkboxes.

这是一个关于如何创建自定义复选框的完整教程。

#2


5  

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

对于那些不想重写OnPaint的人来说,还有另一种解决方案:

  1. Add an ImageList control and fill it with images to reflect checked/unchecked states.
  2. 添加ImageList控件并用图像填充它以反映已检查/未检查的状态。
  3. Set your Checkbox control's Appearance property to Button (to get rid of standard CheckBox icon)
  4. 将Checkbox控件的Appearance属性设置为Button(以摆脱标准的CheckBox图标)
  5. Set its' FlatStyle property to Flat (so that the control doesn't really look like a button).
    Note: You may want to check its' FlatAppearance group of properties too. Namely CheckedBackColor, MouseDownBackColor, MouseOverBackColor, i.e. set them all to Control value.
  6. 将其FlatStyle属性设置为Flat(以便控件看起来不像按钮)。注意:您可能还想检查其'FlatAppearance属性组。即CheckedBackColor,MouseDownBackColor,MouseOverBackColor,即将它们全部设置为Control值。
  7. Set Checkbox control's ImageList property to the name of your ImageList control.
  8. 将Checkbox控件的ImageList属性设置为ImageList控件的名称。
  9. Set Checkbox control's Imageindex and ImageAlign properties to reflect its' current state.
  10. 设置Checkbox控件的Imageindex和ImageAlign属性以反映其当前状态。
  11. Last and most importantly set Checkbox control's TextImageRelation property (this value won't let the text and image overlap unless you want them to). I.e. ImageBeforetext value represents common CheckBox icon location.
  12. 最后也是最重要的是设置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;
    }

#3


0  

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所描述的那样,正确的解决方案。

#4


-1  

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示例

#1


8  

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.

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

Here is a complete tutorial on how to create custom looking checkboxes.

这是一个关于如何创建自定义复选框的完整教程。

#2


5  

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

对于那些不想重写OnPaint的人来说,还有另一种解决方案:

  1. Add an ImageList control and fill it with images to reflect checked/unchecked states.
  2. 添加ImageList控件并用图像填充它以反映已检查/未检查的状态。
  3. Set your Checkbox control's Appearance property to Button (to get rid of standard CheckBox icon)
  4. 将Checkbox控件的Appearance属性设置为Button(以摆脱标准的CheckBox图标)
  5. Set its' FlatStyle property to Flat (so that the control doesn't really look like a button).
    Note: You may want to check its' FlatAppearance group of properties too. Namely CheckedBackColor, MouseDownBackColor, MouseOverBackColor, i.e. set them all to Control value.
  6. 将其FlatStyle属性设置为Flat(以便控件看起来不像按钮)。注意:您可能还想检查其'FlatAppearance属性组。即CheckedBackColor,MouseDownBackColor,MouseOverBackColor,即将它们全部设置为Control值。
  7. Set Checkbox control's ImageList property to the name of your ImageList control.
  8. 将Checkbox控件的ImageList属性设置为ImageList控件的名称。
  9. Set Checkbox control's Imageindex and ImageAlign properties to reflect its' current state.
  10. 设置Checkbox控件的Imageindex和ImageAlign属性以反映其当前状态。
  11. Last and most importantly set Checkbox control's TextImageRelation property (this value won't let the text and image overlap unless you want them to). I.e. ImageBeforetext value represents common CheckBox icon location.
  12. 最后也是最重要的是设置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;
    }

#3


0  

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所描述的那样,正确的解决方案。

#4


-1  

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示例