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
how to change the check image on a checkbox
提问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 OnPaint
method:
这是一个关于如何通过覆盖该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 的任何人,有一个替代解决方案:
- Add an
ImageList
control and fill it with images to reflect checked/unchecked states. - Set your
Checkbox
control'sAppearance
property toButton
(to get rid of standard CheckBox icon) - Set its'
FlatStyle
property toFlat
(so that the control doesn't really look like a button).
Note: You may want to check its'FlatAppearance
group of properties too. NamelyCheckedBackColor
,MouseDownBackColor
,MouseOverBackColor
, i.e. set them all toControl
value. - Set
Checkbox
control'sImageList
property to the name of yourImageList
control. - Set
Checkbox
control'sImageindex
andImageAlign
properties to reflect its' current state. - Last and most importantly set
Checkbox
control'sTextImageRelation
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.
- 添加一个
ImageList
控件并用图像填充它以反映选中/未选中状态。 - 将您的
Checkbox
控件的Appearance
属性设置为Button
(以摆脱标准的 CheckBox 图标) - 将它的 '
FlatStyle
属性设置为Flat
(以便控件看起来不像按钮)。
注意:您可能还想检查它FlatAppearance
的属性组。即CheckedBackColor
,MouseDownBackColor
,MouseOverBackColor
, 即将它们全部设置为Control
值。 - 将
Checkbox
控件的ImageList
属性设置为控件的名称ImageList
。 - 设置
Checkbox
控件Imageindex
和ImageAlign
属性以反映其当前状态。 - 最后也是最重要的设置
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;
}