问题
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?
回答1:
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.
回答2:
For anyone who'd prefer not to override OnPaint, there's an alternative solution:
- Add an
ImageListcontrol and fill it with images to reflect checked/unchecked states. - Set your
Checkboxcontrol'sAppearanceproperty toButton(to get rid of standard CheckBox icon) - Set its'
FlatStyleproperty toFlat(so that the control doesn't really look like a button).
Note: You may want to check its'FlatAppearancegroup of properties too. NamelyCheckedBackColor,MouseDownBackColor,MouseOverBackColor, i.e. set them all toControlvalue. - Set
Checkboxcontrol'sImageListproperty to the name of yourImageListcontrol. - Set
Checkboxcontrol'sImageindexandImageAlignproperties to reflect its' current state. - Last and most importantly set
Checkboxcontrol'sTextImageRelationproperty (this value won't let the text and image overlap unless you want them to). I.e.ImageBeforetextvalue represents common CheckBox icon location.
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:
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.
回答4:
A simple one :
overrides check box OnPaint(PaintEventArgs e) as below:
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
来源:https://stackoverflow.com/questions/15813869/how-to-change-the-check-image-on-a-checkbox